On-line: гостей 0. Всего: 0 [подробнее..]
Программисты всех стран, объединяйтесь!

АвторСообщение



ссылка на сообщение  Отправлено: 12.09.15 10:31. Заголовок: String increment


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


Спасибо: 0 
ПрофильЦитата Ответить
Ответов - 18 [только новые]





ссылка на сообщение  Отправлено: 12.09.15 11:40. Заголовок: Do you mean that all..


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?

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 12.09.15 14:36. Заголовок: A0A0A0A0 A0A0A0A1 A0..


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

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 12.09.15 14:50. Заголовок: I have not understoo..


I have not understood this transformation

A0A0A0z100
A0A0A1A0

Should it be

A0A0A0z100
A0A0A1z100

?


Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 12.09.15 15:04. Заголовок: A0A0A0z100 A0A0A1z1..


A0A0A0z100
A0A0A1z100 this way is okay but

A0A0A0z100
A0A0A1A0 this way i can get maximum increment combination
Strings




Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 02.10.15 23:26. Заголовок: Hi, ramees. A gener..


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.

 
#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;
}
}



Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.10.15 11:10. Заголовок: You can write a corr..


You can write a corresponding class. Here is an initial draft of the class
 
#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;
}
}

The program output is
 
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


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.

Спасибо: 1 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.10.15 13:38. Заголовок: Error showing in ..


[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 ); } );
`


Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.10.15 13:49. Заголовок: It means that the co..


It means that the compiler does not support C++ 2014.
Substitute auto for std::pair<char, unsigned int>

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.10.15 14:12. Заголовок: This is the code i w..


This is the code i wrote pls review
 
#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;



Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.10.15 14:21. Заголовок: Above code run until..


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


Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.10.15 14:32. Заголовок: It is difficult to u..


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.
 
for (int i = 0; i <= runI; i++){

if (i == runI && c1 != E1){

c1++;
i = 0;
^^^^
}


Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.10.15 14:42. Заголовок: Oky thanks :sm36: ..


Oky thanks
 
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);
}


Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.10.15 15:02. Заголовок: You should also comm..


You should also comment entirely the inner loop. That is the main loop can look just like
 
while (next_successor(values));



Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.10.15 15:15. Заголовок: I run the program wi..


I run the program with the class using an online compiler
and I got the following result
 
A0A0A0A0
C2C2C2C2
0

The main looks the following way
 
{
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;
}


Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.10.15 15:18. Заголовок: When I use all upper..


When I use all upper case letters then the result looks like
 
A0A0A0A0
Z2Z2Z2Z2
3


But if to increase the maximum integer value then the result is
 
A0A0A0A0
Z3Z3Z3Z3
10

Take into account that I am using an online compiler.



Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.10.15 15:29. Заголовок: Is this code taking ..


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

Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.10.15 15:37. Заголовок: I showed the code th..


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!


Спасибо: 0 
ПрофильЦитата Ответить



ссылка на сообщение  Отправлено: 03.10.15 15:45. Заголовок: Ok thank :sm36: ..


Ok thank

Спасибо: 0 
ПрофильЦитата Ответить
Ответ:
1 2 3 4 5 6 7 8 9
большой шрифт малый шрифт надстрочный подстрочный заголовок большой заголовок видео с youtube.com картинка из интернета картинка с компьютера ссылка файл с компьютера русская клавиатура транслитератор  цитата  кавычки моноширинный шрифт моноширинный шрифт горизонтальная линия отступ точка LI бегущая строка оффтопик свернутый текст

показывать это сообщение только модераторам
не делать ссылки активными
Имя, пароль:      зарегистрироваться    
Тему читают:
- участник сейчас на форуме
- участник вне форума
Все даты в формате GMT  3 час. Хитов сегодня: 9
Права: смайлы да, картинки да, шрифты да, голосования нет
аватары да, автозамена ссылок вкл, премодерация откл, правка нет