Advertisement
Emanuele_Bruno

Esame 7

Dec 6th, 2015
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.29 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. const int COL=4,RIG=4;
  6. const string A[][COL]={{"xioux","aei","aei","aei"},
  7.                        {"iou","aei","iou","iou"},
  8.                        {"iou","aei","iou","iou"},
  9.                        {"aei","iou","aei","aei"}};
  10.  
  11. bool contenuto (const string A[][COL], const int RIG, string str);
  12. bool stringa_trovata(string a,string b);
  13.  
  14. int main()
  15. {
  16.     string str="iou";
  17.     cout << "Verifico che str e' contenuto per almeno il 50% in una matrice A." << endl;
  18.     cout << "Se il risultato e' 1 allora significa che e' vero :" << contenuto(A,RIG,str);
  19.     return 0;
  20. }
  21.  
  22. bool stringa_trovata(string a,string b)
  23. {   // stiamo cercando 'b' in 'a' e non viceversa
  24.     unsigned int n=0,lun_a=a.length(),lun_b=b.length();
  25.     if (lun_a>=lun_b)
  26.         while (n<=lun_a-lun_b)
  27.         {
  28.             if (a.substr(n,lun_b)==b) return true;
  29.             else n++;
  30.         }
  31.     return false;
  32. }
  33.  
  34. bool contenuto (const string A[][COL], const int RIG, string str)
  35. {
  36.     int i=0, k, contatore=0, meta=RIG*COL/2;
  37.     while (i<RIG)
  38.     {
  39.         k=0;
  40.         while (k<COL)
  41.         {
  42.             if (stringa_trovata(A[i][k],str)) contatore++;
  43.             k++;
  44.         }
  45.         i++;
  46.     }
  47.     if (contatore>=meta) return true;
  48.     else return false;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement