Advertisement
J00ker

Untitled

Jan 14th, 2015
199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. #include <cstdlib>
  5.  
  6. using namespace std;
  7.  
  8. //1.
  9. //a.
  10. int CitMem(char* txt, char tabl[][15])
  11. {
  12.     ifstream fin("date.in");
  13.     fin.getline(txt, 999);
  14.     fin.close();
  15.     cout << "text.in: " << txt << "\n\n";
  16.  
  17.     char sep[] = " ,.;:!?", *p;
  18.     int i = 0;
  19.     p = strtok(txt, sep);
  20.     while(p != NULL)
  21.     {
  22.         strcpy(tabl[i], p);
  23.         i++;
  24.         p = strtok(NULL, sep);
  25.     }
  26.     return i;
  27. }
  28.  
  29.  
  30. //b.
  31. void AparCuv(char tabl[][15], int nrcuv, char* cuv)
  32. {
  33.     int nr = 0;
  34.     for(int i = 0; i < nrcuv; i++)
  35.         if(strcmp(tabl[i], cuv) == 0) nr++;
  36.     cout << "b.Aparitii \"" << cuv << "\" : " << nr << "\n\n";
  37. }
  38.  
  39. //c.
  40. void SubCuv(char tabl[][15], int nrcuv, char* subcuv)
  41. {
  42.     cout << "c.Cuvinte cu \"" << subcuv << "\" :\n";
  43.     for(int i = 0; i < nrcuv; i++)
  44.         if(strstr(tabl[i], subcuv) != NULL)
  45.             cout << tabl[i] << "\n";
  46.     cout << "\n";
  47. }
  48.  
  49. //d.
  50. void MicCuv(char tabl[][15], int nrcuv)
  51. {
  52.     int i;
  53.     char* cuv = tabl[0];
  54.     for(i = 1; i < nrcuv; i++)
  55.         if(strcmp(tabl[i], cuv) < 0)
  56.             cuv = tabl[i];
  57.     cout << "d.Cel mai mic cuvant: " << cuv << "\n\n";
  58. }
  59.  
  60. //e.
  61. void InvCuv(char tabl[][15], int nrcuv)
  62. {
  63.     cout << "e.Cuvintele inversate :\n";
  64.     int i, j, lg;
  65.     for(i = 0; i < nrcuv; i++)
  66.     {
  67.         lg = strlen(tabl[i]) - 1;
  68.         for(j = lg; j >= 0; j--)
  69.             cout << tabl[i][j];
  70.         cout << " ";
  71.     }
  72.     cout << "\n\n";
  73. }
  74.  
  75. //f.
  76. void TermLit(char tabl[][15], int nrcuv, char lit)
  77. {
  78.     cout << "f.Cuvintele cu \'" << lit << "\' :\n";
  79.     int i, lg;
  80.     for(i = 0; i < nrcuv; i++)
  81.     {
  82.         lg = strlen(tabl[i]) - 1;
  83.         if(tabl[i][lg] == lit)
  84.             cout << tabl[i] << "\n";
  85.     }
  86.     cout << "\n";
  87. }
  88.  
  89. //g.
  90. inline void Mutare(char tabl[][15], int nrcuv, int n)
  91. {
  92.     for(int i = n; i < nrcuv-1; i++)
  93.         strcpy(tabl[i], tabl[i+1]);
  94.     tabl[nrcuv-1][0] = 0;
  95.  
  96. }
  97. int ElimCuv(char tabl[][15], int nrcuv, int nrlit)
  98. {
  99.     for(int i = 0; i < nrcuv; i++)
  100.         if(strlen(tabl[i]) <= nrlit)
  101.         {
  102.             Mutare(tabl, nrcuv, i);
  103.             nrcuv--;
  104.             i--;
  105.         }
  106.     return nrcuv;
  107. }
  108.  
  109. //h.
  110. void OrdAlfab(char tabl[][15], int nrcuv)
  111. {
  112.     int i, j;
  113.     char aux[15];
  114.     for(i = 0; i+1 < nrcuv; i++)
  115.         for(j = i+1; j < nrcuv; j++)
  116.         {
  117.             if(strcmp(tabl[i], tabl[j]) > 0)
  118.             {
  119.                 strcpy(aux, tabl[i]);
  120.                 strcpy(tabl[i], tabl[j]);
  121.                 strcpy(tabl[j], aux);
  122.             }
  123.         }
  124. }
  125.  
  126.  
  127. //2.
  128. int Identic(char* s, char* t)
  129. {
  130.     char *p;
  131.     int slg, tlg;
  132.     slg = strlen(s);
  133.     tlg = strlen(t);
  134.  
  135.     if(slg != tlg) return 0;
  136.  
  137.     while(tlg >= 0)
  138.     {
  139.         p = strchr(s, t[0]);
  140.         if(p != NULL)
  141.         {
  142.             strcpy(p, p+1);
  143.             strcpy(t, t+1);
  144.             tlg--;
  145.         }
  146.         else return 0;
  147.     }
  148.     return 1;
  149. }
  150.  
  151. //3.
  152. void Suma(char* expr)
  153. {
  154.     int s = 0;
  155.     char *p, sep[] = "x=+ ";
  156.     p = strtok(expr, sep);
  157.     while(p != NULL)
  158.     {
  159.         s += atoi(p);
  160.         p = strtok(NULL, sep);
  161.     }
  162.     cout << "x = " << s;
  163. }
  164.  
  165.  
  166. int main()
  167. {
  168.     //1.
  169.     char str[1000], cuv[100][15];
  170.  
  171.     //a.
  172.     int nrcuv = CitMem(str, cuv);
  173.     //b.
  174.     AparCuv(cuv, nrcuv, "calculator");
  175.  
  176.     //c.
  177.     SubCuv(cuv, nrcuv, "ini");
  178.  
  179.     //d.
  180.     MicCuv(cuv, nrcuv);
  181.  
  182.     //e.
  183.     InvCuv(cuv, nrcuv);
  184.  
  185.     //f.
  186.     TermLit(cuv, nrcuv, 'a');
  187.  
  188.     //g.
  189.     nrcuv = ElimCuv(cuv, nrcuv, 3);
  190.  
  191.     //h.
  192.     OrdAlfab(cuv, nrcuv);
  193.  
  194.  
  195.     //2.
  196.     char s[99], t[99];
  197.     cout << "2.s: "; cin.getline(s, 99);
  198.     cout << "  t: "; cin.getline(t, 99);
  199.     if(Identic(s, t) == 1)
  200.         cout << "s si t sunt identice.\n\n";
  201.     else
  202.         cout << "s si t nu sunt identice.\n\n";
  203.  
  204.  
  205.     //3.
  206.     cout << "3.x=a+b: ";cin.getline(s,99);
  207.     Suma(s);
  208.  
  209.     //cout << s;
  210.     //cout << nrcuv << "\n";
  211.     //for(int i = 0; i < nrcuv; i++)
  212.     //    cout << cuv[i] << "\n";
  213.     return 0;
  214. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement