Форум » C/C++ для начинающих (C/C++ for beginners) » String increment » Ответить

String increment

ramees: I need help I have a string like this String i_data = "A0A0A0A0" ; i like to work this as an increment String like A0A0A0A1 all number will only increment upto 100 then stop All characters increment capital A to small z The end string look like this z100z100z100z100 How can i do this kind of customized increment

Ответов - 18

Сыроежка: Do you mean that all digits are incremented sequentially from 0 up to 100 from right to left? And when are you going to change 'A' to 'z'? What are the steps of the algorithm?

ramees: A0A0A0A0 A0A0A0A1 A0A0A0A2 .... so on A0A0A0A100 A0A0A0B0 A0A0A0B1 A0A0A0B2 ...so on A0A0A0z100 A0A0A1A0 A0A0A1A1 ...so on A0A0A1A100 A0A0A1B0 A0A0A1B1 ...so on A0A0A1z100 A0A0A2A0 A0A0A2A1 so on ... end --> z100z100z100z100

Сыроежка: I have not understood this transformation A0A0A0z100 A0A0A1A0 Should it be A0A0A0z100 A0A0A1z100 ?


ramees: A0A0A0z100 A0A0A1z100 this way is okay but A0A0A0z100 A0A0A1A0 this way i can get maximum increment combination Strings

Сыроежка: Hi, ramees. A general approach can look the following way. At first the string should be splitted into pairs { character, number }. Then the required pair is "increased". After that the string is built anew from the pairs. Here is a demonstrative program that shows the funcion that deals with pairs. I simplified the program. It processes only twp pairs with characters 'A', 'B', and 'C' and with the maximum value equal to 20. You should test it whether it does the right thing. You can add additional characters to the string literal pointed to by alpha and add more pairs that to see whether the function is correct. Take into account that if you add an additiona character for testing you need also to change the lambda expression in the call of std::find_if. [pre2] #include <iostream> #include <algorithm> #include <utility> #include <array> bool next_successor( std::array<std::pair<char, unsigned int>, 2> &values ) { const char *alpha = "ABC"; //DEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; const unsigned int N = 20; auto it = std::find_if( values.rbegin(), values.rend(), [=]( const auto &v ) { return v != std::pair<char, unsigned int>( 'C', N ); } ); if ( it == values.rend() ) return false; if ( it->second++ == N ) { it->second = 0; it->first = *( std::strchr( alpha, it->first ) + 1 ); } std::fill( values.rbegin(), it, std::pair<char, unsigned int>( *alpha, 0 ) ); return true; } int main() { // std::string s( "A0A0A0A0" ); std::array<std::pair<char, unsigned int>, 2> values = { { { 'A', 0 }, { 'A', 0 } } }; while( next_successor( values ) ) { for ( auto p : values ) std::cout << p.first << p.second; std::cout << std::endl; } } [/pre2]

Сыроежка: You can write a corresponding class. Here is an initial draft of the class [pre2] #include <iostream> #include <string> #include <cstring> #include <array> class Combination { public: typedef std::pair<char, unsigned int> value_type; Combination() : combination{ { { first_pair }, { first_pair } } } { } bool next_successor() { auto it = std::find_if( combination.rbegin(), combination.rend(), []( const value_type &value ) { return value != last_pair; } ); if ( it == combination.rend() ) return false; if ( it->second++ == max_value ) { it->second = 0; it->first = *( std::strchr( alpha, it->first ) + 1 ); } std::fill( combination.rbegin(), it, first_pair ); return true; } bool prev_successor() { auto it = std::find_if( combination.rbegin(), combination.rend(), []( const value_type &value ) { return value != first_pair; } ); if ( it == combination.rend() ) return false; if ( it->second-- == 0 ) { it->second = max_value; it->first = *( std::strchr( alpha, it->first ) - 1 ); } std::fill( combination.rbegin(), it, last_pair ); return true; } std::string to_string() const { std::string s; s.reserve( 8 ); for ( const value_type &value : combination ) { s += value.first; s += std::to_string( value.second ); } return s; } private: // static data members static const unsigned int max_value; static const char *alpha; static const size_t alpha_size; static const std::pair<char, unsigned int> first_pair; static const std::pair<char, unsigned int> last_pair; private: // non-static data members std::array<std::pair<char, unsigned int>, 2> combination; }; const unsigned int Combination::max_value = 2; const char * Combination::alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; const size_t Combination::alpha_size = std::strlen( alpha ); const std::pair<char, unsigned int> Combination::first_pair( alpha[0], 0 ); const std::pair<char, unsigned int> Combination::last_pair( alpha[alpha_size-1], max_value ); int main() { Combination c; for ( size_t i = 0; i < 30 && c.next_successor(); i++ ) { std::cout << c.to_string() << std::endl; } while ( c.prev_successor() ) { std::cout << c.to_string() << std::endl; } } [/pre2] The program output is [pre2] A0A1 A0A2 A0B0 A0B1 A0B2 A0C0 A0C1 A0C2 A0D0 A0D1 A0D2 A0E0 A0E1 A0E2 A0F0 A0F1 A0F2 A0G0 A0G1 A0G2 A0H0 A0H1 A0H2 A0I0 A0I1 A0I2 A0J0 A0J1 A0J2 A0K0 A0J2 A0J1 A0J0 A0I2 A0I1 A0I0 A0H2 A0H1 A0H0 A0G2 A0G1 A0G0 A0F2 A0F1 A0F0 A0E2 A0E1 A0E0 A0D2 A0D1 A0D0 A0C2 A0C1 A0C0 A0B2 A0B1 A0B0 A0A2 A0A1 A0A0 [/pre2] You can write the constructor such a way that it had parameters that specify the number of pairs in combinations and the maximum integer value. In other words now you have large possibilities for a creative work.

ramees: [Pre2] Error showing in my visual studio 2013 in the first code red underline in auto auto it = std::find_if( values.rbegin(), values.rend(), [=]( const auto &v ) { return v != std::pair<char, unsigned int>( 'C', N ); } ); [/pre2]

Сыроежка: It means that the compiler does not support C++ 2014. Substitute auto for std::pair<char, unsigned int>

ramees: This is the code i wrote pls review [pre2] #include <iostream> #include <string> #include <time.h> using namespace std; int main(){ clock_t tStart = clock(); string i_data = "A0A0A0A0"; int c1 = 0; int c2 = 0; int c3 = 0; int c4 = 0; int c5 = 0; int c6 = 0; int c7 = 0; int E1 = 2;// array index value int O1 = 2;//int value int runI = 3; //running index value int index_value = 0; // index counter int inL_value = 0; // index counter value reciver to recrusive int inR_value = 0; // incerment index value int SW = 0; // it is a SW for indexing //string A[52] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", // "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}; string A[3] = { "A", "B", "C" }; for (int i = 0; i <= runI; i++){ if (i == runI && c1 != E1){ c1++; i = 0; } if (c1 == E1 && i == runI && c2 != O1){ c2++; c1 = 0; i = 0; } if (c1 == E1 && c2 == O1 && i == runI && c3 != E1){ c3++; c2 = 0; c1 = 0; i = 0; } if (c1 == E1 && c2 == O1 && c3 == E1 && i == runI && c4 != O1){ c4++; c3 = 0; c2 = 0; c1 = 0; i = 0; } if (c1 == E1 && c2 == O1 && c3 == E1 && c4 == O1 && i == runI && c5 != E1){ c5++; c4 = 0; c3 = 0; c2 = 0; c1 = 0; i = 0; } if (c1 == E1 && c2 == O1 && c3 == E1 && c4 == O1 && c5 == E1 && i == runI && c6 != O1){ c6++; c5 = 0; c4 = 0; c3 = 0; c2 = 0; c1 = 0; i = 0; } if (c1 == E1 && c2 == O1 && c3 == E1 && c4 == O1 && c5 == E1 && c6 == O1 && i == runI && c7 != E1){ c7++; c6 = 0; c5 = 0; c4 = 0; c3 = 0; c2 = 0; c1 = 0; i = 0; } cout << A[c7] << c6 << A[c5] << c4 << A[c3] << c2 << A[c1] << i << endl; for (int j = 0; j < 2; j++){ } //////////// end line =================================> if (A[c7] == "C" && c6 == O1 && A[c5] == "C" && c4 == O1 && A[c3] == "C" && c2 == O1 && A[c1] == "C" && i == O1){ cout << "Simulation finished sucsessfully " << A[c7] << c6 << A[c5] << c4 << A[c3] << c2 << A[c1] << i << endl; i++; } } printf("Time taken : % 2fs \n", (double) (clock() - tStart) / CLOCKS_PER_SEC); return 0;[/pre2]

ramees: Above code run untill it reach C2C2C2C2 without displaying all sequences it take 0.031000s I like to know ur first code run time in 4 pair C2C2C2C2

Сыроежка: It is difficult to understand the code because it contains many "magic numbers". Or for example the control variable i is changed inside the ,loop. It is not a good idea. [pre2] for (int i = 0; i <= runI; i++){ if (i == runI && c1 != E1){ c1++; i = 0; ^^^^ } [/pre2]

ramees: Oky thanks [pre2] I incremented up 4 is this ok? #include <iostream> #include <algorithm> #include <utility> #include <array> #include <time.h> bool next_successor(std::array<std::pair<char, unsigned int>, 4> &values) { const char *alpha = "ABC"; //DEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; const unsigned int N = 2; auto it = std::find_if(values.rbegin(), values.rend(), [=](const std::pair<char,unsigned int> &v) { return v != std::pair<char, unsigned int>('C', N); }); if (it == values.rend()) return false; if (it->second++ == N) { it->second = 0; it->first = *(std::strchr(alpha, it->first) + 1); } std::fill(values.rbegin(), it, std::pair<char, unsigned int>(*alpha, 0)); return true; } int main() { clock_t tStart = clock(); // std::string s( "A0A0A0A0" ); std::array<std::pair<char, unsigned int>, 4> values = { { { 'A', 0 }, { 'A', 0 }, { 'A', 0 }, { 'A', 0 } } }; while (next_successor(values)) { for (auto p : values) /*std::cout << p.first << p.second*/; /*std::cout << std::endl;*/ } printf("Time taken : % 2fs \n", (double) (clock() - tStart) / CLOCKS_PER_SEC); } [/pre2]

Сыроежка: You should also comment entirely the inner loop. That is the main loop can look just like [pre2] while (next_successor(values)); [/pre2]

Сыроежка: I run the program with the class using an online compiler and I got the following result [pre2] A0A0A0A0 C2C2C2C2 0 [/pre2] The main looks the following way [pre2] { Combination c; clock_t tStart = clock(); std::cout << c.to_string() << std::endl; while ( c.next_successor() ); std::cout << c.to_string() << std::endl; std::cout << (clock() - tStart) / CLOCKS_PER_SEC << std::endl; } [/pre2]

Сыроежка: When I use all upper case letters then the result looks like [pre2] A0A0A0A0 Z2Z2Z2Z2 3 [/pre2] But if to increase the maximum integer value then the result is [pre2] A0A0A0A0 Z3Z3Z3Z3 10 [/pre2] Take into account that I am using an online compiler.

ramees: Is this code taking 3 sec or 3 msec I commented the loop because cout take lot's of time i can't get the actual time if its on

Сыроежка: I showed the code that I tested. Take into account that the total number of operations can be estimated like 52 * 100 * 52 * 100 * 52 * 100 * 52 * 100 Good luck to you!

ramees: Ok thank



полная версия страницы