[C++] Porównywanie par tekstu
Ostatnio zmodyfikowano 2012-11-07 19:49
CoolPanda Temat założony przez niniejszego użytkownika |
[C++] Porównywanie par tekstu » 2012-11-07 19:49:38 kod ponizej tworzy pary z liter ABC (AA BA CB...) a ponizej musi porownac i wyswietlic odpowiedniki w ponizszej linijce... niestety mi to nie wychodzi, nie wiem co robie nie tak.. Program dziala i kompiluje ale odpowiedniki, a raczej tylko jeden odpowiednik do pierwszej pary tylko pasuje a potem sie powtarza.. #include<iostream> #include<cstdlib> #include<ctime>
using namespace std;
char getRandomChar( char array[], int size ) { int i; char randomChar; for( i = 0; i < size; i++ ) { int randChar = rand() % 3; randomChar = array[ randChar ]; } return randomChar; }
void comparables( char & n1, char & n2 ) { int tmp, tmp2; tmp = n1; n1 = tmp; tmp2 = n2; n2 = tmp; if(( n1 == 'A' && n2 == 'B' ) ||( n1 == 'B' && n2 == 'A' ) ) { cout << 'a' << 'b' << " "; } if(( n1 == 'B' && n2 == 'C' ) ||( n1 == 'C' && n2 == 'B' ) ) { cout << 'b' << 'c' << " "; } if(( n1 == 'A' && n2 == 'C' ) ||( n1 == 'C' && n2 == 'A' ) ) { cout << 'a' << 'c' << " "; } if( n1 == 'A' && n2 == 'A' ) { cout << 'a' << 'a' << " "; } if( n1 == 'B' && n2 == 'B' ) { cout << 'b' << 'b' << " "; } if( n1 == 'C' && n2 == 'C' ) { cout << 'c' << 'c' << " "; } }
main() { srand( time( NULL ) ); int number; char n1, n2; char array[] = { 'A', 'B', 'C' }; do { cout << "Enter number of pairs: "; cin >> number; if( number >= 1 && number <= 200 ) { for( int i = 0; i < number; i++ ) { n1 = getRandomChar( array, 2 ); n2 = getRandomChar( array, 2 ); cout << n1 << n2 << " "; } cout << endl; for( int j = 0; j < number; j++ ) { comparables( n1, n2 ); } cout << endl; } else { cout << "Number must be between 0 and 200.\n"; cout << "Try again..." << endl << endl; } } while(( number <= 0 ) ||( number > 200 ) ); return 0; } |
|
« 1 » |