Advertisement
Gilgamesh858

stringhe_Concatenati_Uguali

Dec 17th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <ctime>
  3. #include <string>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. /*
  9.  * ESERCIZIO
  10.  * Scrivere un metodo che prenda in input una matrice A ed un array B entrambi di stringhe e
  11.  * restituisca un boolean che indichi se esiste in A una coppia di stringhe la cui concatenazione,
  12.  * privata delle occorrenze del carattere 'a', sia presente in B.
  13.  *
  14.  * By Trupia Ludovico
  15.  */
  16.  
  17. const int COLONNE = 7 ;
  18.  
  19. void alloca_Matrice_Stringa ( string matrice[][COLONNE] , int righe );
  20. void stampa_Matrice ( string matrice[][COLONNE] , int righe );
  21. bool conc_Verifica ( string matrice[][COLONNE] , int righe , string vettore[] );
  22. void stampa_Vettore ( string vettore[] );
  23. void alloca_Vettore ( string vettore[] );
  24.  
  25. int main()
  26. {
  27.     srand( time ( NULL ));
  28.  
  29.     int righe ;
  30.  
  31.     cout << endl << " Che lunghezza ha la matrice A? \n [RIGHE]// ";
  32.     cin >> righe;
  33.  
  34.     string A[righe][COLONNE], B[COLONNE] ;
  35.  
  36.     cout << endl;
  37.  
  38.     alloca_Matrice_Stringa ( A , righe );
  39.  
  40.     stampa_Matrice ( A , righe );
  41.  
  42.     cout << endl << "//////////////////////////////////////////" << endl ;
  43.  
  44.     alloca_Vettore ( B );
  45.  
  46.     stampa_Vettore ( B );
  47.  
  48.     cout << endl << "//////////////////////////////////////////" << endl << endl ;
  49.  
  50.     if ( conc_Verifica ( A , righe , B ) )
  51.     {
  52.         cout << endl << endl << " !!!!!Esiste una concatenazione uguale!!!!! ";
  53.     }
  54.     else
  55.     {
  56.         cout << endl << endl << " !!!!!NON Esiste una concatenazione uguale!!!!! ";
  57.     }
  58.  
  59.     return 0;
  60. }
  61.  
  62. void alloca_Matrice_Stringa ( string matrice[][COLONNE] , int righe )
  63. {
  64.     int n = 0;
  65.     char consonanti[16] = {'b','c','d','f','g','h','l','m','n','p','q','r','s','t','v','z'};
  66.     char vocali[5] = {'a','e','i','o','u'};
  67.  
  68.     for( int j = 0; j < righe; j++)
  69.     {
  70.         for ( int k = 0 ; k < COLONNE ; k++)
  71.         {
  72.             n = ((rand() % 4) + 2) * 2;
  73.  
  74.             for( int i = 0; i < n; i++)
  75.             {
  76.                 if( i % 2 == 0 )
  77.                 {
  78.                     matrice[j][k] += consonanti[rand()%16];
  79.                    // cout << consonanti[rand()%16];
  80.                 }
  81.                 else
  82.                 {
  83.                     matrice[j][k] += vocali[rand()%5];
  84.                     //cout << vocali[rand()%5];
  85.                 }
  86.             }
  87.         }
  88.     }
  89. }
  90.  
  91. void stampa_Matrice ( string matrice[][COLONNE] , int righe )
  92. {
  93.     for ( int i = 0 ; i < righe ; i++ )
  94.     {
  95.         cout << endl;
  96.         for ( int j = 0 ; j < COLONNE ; j++ )
  97.         {
  98.             cout << matrice[i][j] << " " ;
  99.         }
  100.         cout << endl;
  101.     }
  102. }
  103.  
  104.  
  105. void alloca_Vettore ( string vettore[] )
  106. {
  107.     int n = 0;
  108.     char consonanti[16] = {'b','c','d','f','g','h','l','m','n','p','q','r','s','t','v','z'};
  109.     char vocali[4] = {'e','i','o','u'};
  110.  
  111.     for( int j = 0; j < COLONNE; j++)
  112.     {
  113.             n = ((rand() % 4) + 2) * 2;
  114.  
  115.             for( int i = 0; i < n; i++)
  116.             {
  117.                 if( i % 2 == 0 )
  118.                 {
  119.                     vettore[j] += consonanti[rand()%16];
  120.                    // cout << consonanti[rand()%16];
  121.                 }
  122.                 else
  123.                 {
  124.                     vettore[j] += vocali[rand()%5];
  125.                     //cout << vocali[rand()%5];
  126.                 }
  127.             }
  128.     }
  129. }
  130.  
  131. void stampa_Vettore ( string vettore [])
  132. {
  133.     cout << endl;
  134.     for ( int i = 0 ; i < COLONNE ; i++ )
  135.     {
  136.         cout << vettore[i] << " ";
  137.     }
  138.     cout << endl;
  139. }
  140.  
  141. bool conc_Verifica ( string matrice[][COLONNE] , int righe , string vettore[] )
  142. {
  143.     string concatenazione = "";
  144.  
  145.     for ( int i = 0 ; i < righe ; i ++ )
  146.     {
  147.         for ( int j = 0 ; j < COLONNE-1 ; j++ )
  148.         {
  149.             for ( int k = j+1 ; k < COLONNE ; k++ )
  150.             {
  151.                 cout << endl << endl << "########## j - " << j << " | k - " << k << endl << endl;
  152.  
  153.                 for ( int f = 0 ; f < matrice[i][j].length() ; f++ )
  154.                 {
  155.                     if ( matrice[i][j].at(f) != 'a' )
  156.                     {
  157.                         concatenazione += matrice[i][j].at(f);
  158.                         cout << concatenazione << "  ";
  159.                     }
  160.                 }
  161.  
  162.                 for ( int f = 0 ; f < matrice[i][k].length() ; f++ )
  163.                 {
  164.                     if ( matrice[i][k].at(f) != 'a' )
  165.                     {
  166.                         concatenazione += matrice[i][k].at(f);
  167.                         cout << concatenazione << "  ";
  168.                     }
  169.                 }
  170.  
  171.                 for ( int f = 0 ; f < COLONNE ; f++ )
  172.                 {
  173.                     int v = 0;
  174.  
  175.                     for ( int l = 0 ; l < vettore[f].length() ; l++ )
  176.                     {
  177.                         if ( concatenazione.at(v) == vettore[f].at(l) )
  178.                         {
  179.                             v++;
  180.                         }
  181.                         else
  182.                         {
  183.                             v = 0;
  184.                         }
  185.  
  186.                         if ( v == concatenazione.length() )
  187.                         {
  188.                             return true;
  189.                         }
  190.                     }
  191.                 }
  192.  
  193.                 concatenazione = "";
  194.             }
  195.         }
  196.     }
  197.  
  198.     return false;
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement