D-Gj

Rastojanie megju zborovi

Mar 8th, 2014
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 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], currentWord[100];
  11.     int 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.             currentWord[n] = '\0';
  42.  
  43.             if (strcmp(firstWord, currentWord) == 0)
  44.             {
  45.                 firstIndex[firstIndex_n++] = i + 1 - p - firstWordLength;
  46.             }
  47.             else if (strcmp(secondWord, currentWord) == 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.                 currentWord[n] = '\0';
  59.  
  60.                 if (strcmp(firstWord, currentWord) == 0)
  61.                 {
  62.                     firstIndex[firstIndex_n++] = i + 1 - p - firstWordLength;
  63.                 }
  64.                 else if (strcmp(secondWord, currentWord) == 0)
  65.                 {
  66.                     secondIndex[secondIndex_n++] = i + 1 - p - secondWordLength;
  67.                 }
  68.  
  69.                 n = 0;
  70.             }
  71.             else
  72.             {
  73.                 currentWord[n++] = text[i];
  74.                 p = 0;
  75.             }
  76.         }
  77.         else
  78.         {
  79.             p++;
  80.         }
  81.     }
  82.  
  83.     if (firstIndex[0] == -1 || secondIndex[0] == -1)
  84.     {
  85.         cout << "Nekoj od zborovite (ili dvata) ne se pojavuva vo tekstot!" << endl;
  86.         return 0;
  87.     }
  88.  
  89.     for (int i = 0; i < firstIndex_n; ++i)
  90.     {
  91.         for (int j = 0; j < secondIndex_n; ++j)
  92.         {
  93.             if (firstIndex[i] > secondIndex[j])
  94.             {
  95.                 cout << "Rastojanieto megju prviot i vtoriot zbor e " << firstIndex[i] - (secondIndex[j] + secondWordLength) << endl;
  96.             }
  97.             else
  98.             {
  99.                 cout << "Rastojanieto megju prviot i vtoriot zbor e " << secondIndex[j] - (firstIndex[i] + firstWordLength) << endl;
  100.             }
  101.         }
  102.     }
  103.  
  104.     return 0;
  105. }
Advertisement
Add Comment
Please, Sign In to add comment