D-Gj

Rastojanie megju zborovi

Mar 3rd, 2013
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. // Da se najde rastojanie megju dva zbora vo tekst.
  2.  
  3. #include <iostream>
  4. #include <cstring>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.     char text[500], firstWord[100], secondWord[100], words[50][100];
  11.     int m = 0, n = 0, p = 0, firstWordLength = 0, secondWordLength = 0, firstIndex[50], secondIndex[50], firstIndex_n = 0, secondIndex_n = 0;
  12.  
  13.     cout << "Vnesi tekst: ";
  14.     cin.getline(text, 500);
  15.     cout << "Vnesi zbor: ";
  16.     cin >> firstWord;
  17.     cout << "Vnesi zbor: ";
  18.     cin >> secondWord;
  19.     cout << endl;
  20.  
  21.     if (strcmp(firstWord, secondWord) == 0)
  22.     {
  23.         cout << "Zborovite ne mozhe da bidat isti!" << endl;
  24.         return 1;
  25.     }
  26.  
  27.     firstIndex[0] = secondIndex[0] = -1;
  28.  
  29.     do {
  30.         firstWordLength++;
  31.     } while (firstWord[firstWordLength] != '\0');
  32.  
  33.     do {
  34.         secondWordLength++;
  35.     } while (secondWord[secondWordLength] != '\0');
  36.  
  37.     for (int i = 0;;++i)
  38.     {
  39.         if (text[i] == '\0')
  40.         {
  41.             words[m][n] = '\0';
  42.  
  43.             if (strcmp(firstWord, words[m]) == 0)
  44.             {
  45.                 firstIndex[firstIndex_n++] = i + 1 - p - firstWordLength;
  46.             }
  47.             else if (strcmp(secondWord, words[m]) == 0)
  48.             {
  49.                 secondIndex[secondIndex_n++] = i + 1 - p - secondWordLength;
  50.             }
  51.  
  52.             break;
  53.         }
  54.         else if (!(text[i] == ',' || text[i] == '.' || text[i] == ';' || text[i] == ':' || text[i] == '!' || text[i] == '?'))
  55.         {
  56.             if (text[i] == ' ')
  57.             {
  58.                 words[m][n] = '\0';
  59.  
  60.                 if (strcmp(firstWord, words[m]) == 0)
  61.                 {
  62.                     firstIndex[firstIndex_n++] = i + 1 - p - firstWordLength;
  63.                 }
  64.                 else if (strcmp(secondWord, words[m]) == 0)
  65.                 {
  66.                     secondIndex[secondIndex_n++] = i + 1 - p - secondWordLength;
  67.                 }
  68.  
  69.                 m++;
  70.                 n = 0;
  71.             }
  72.             else
  73.             {
  74.                 words[m][n++] = text[i];
  75.                 p = 0;
  76.             }
  77.         }
  78.         else
  79.         {
  80.             p++;
  81.         }
  82.     }
  83.  
  84.     if (firstIndex[0] == -1 || secondIndex[0] == -1)
  85.     {
  86.         cout << "Nekoj od zborovite (ili dvata) ne se pojavuva vo tekstot!" << endl;
  87.         return 0;
  88.     }
  89.  
  90.     for (int i = 0; i < firstIndex_n; ++i)
  91.     {
  92.         for (int j = 0; j < secondIndex_n; ++j)
  93.         {
  94.             if (firstIndex[i] > secondIndex[j])
  95.             {
  96.                 cout << "Rastojanieto megju prviot i vtoriot zbor e " << firstIndex[i] - (secondIndex[j] + secondWordLength) << endl;
  97.             }
  98.             else
  99.             {
  100.                 cout << "Rastojanieto megju prviot i vtoriot zbor e " << secondIndex[j] - (firstIndex[i] + firstWordLength) << endl;
  101.             }
  102.         }
  103.     }
  104.  
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment