Advertisement
BlackCoffeeCoding

lab 2_1

Mar 21st, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3.  
  4. using namespace std;
  5.  
  6. #define inputfile "input.txt"//Расположение входного файла
  7. #define outputfile "output.txt"//Расположение выходного файла
  8.  
  9. int main()
  10. {
  11.     ifstream input(inputfile);//Файл ввода
  12.     ofstream output(outputfile);//Файл ввода
  13.  
  14.     int n = 100;//Длина массива
  15.     char* s = new char[n]; //Массив длиной n
  16.     s[n] = '\0';//Обозначаем конец строки
  17.     int i, j=0, count;//Счетчики
  18.     int Max=0; 
  19.     int len = 0; //Для подсчета длины строки
  20.     char* substr = new char[n];//Здесь хранится фраза из строки
  21.     substr[n] = '\0';
  22.     char* substr2 = new char[n];//самая длинная фраза
  23.     substr[n] = '\0';
  24.     //Чтение строки из файла и запись в переменную 's'
  25.     //Проверка на существование файла
  26.     if (!input)
  27.     {
  28.         cout << "\nNo input file" << endl;
  29.         exit(1);
  30.     }
  31.  
  32.     //Првоерка на пустой файл
  33.     if (input.peek() == EOF)
  34.     {
  35.         cout << "\nFile is empty" << endl;
  36.         exit(2);
  37.     }
  38.  
  39.     //Чтение строки длиной n из файла
  40.     input.getline(s, n);
  41.  
  42.     //Вывод прочитанной строки
  43.     cout << "\nInput str: " << s << endl;
  44.  
  45.     input.close();//Закрываем файл
  46.  
  47.    
  48.    
  49.     //Процесс разбиения
  50.     i = 0;
  51.     count = 0;
  52.     //Находим длину строки
  53.     len = 0;
  54.     while (s[len] != '\0') //Пока не конец строки
  55.     {
  56.         len++;
  57.     }
  58.     while (i < len - 1)
  59.     {
  60.         //j = 0;
  61.         if (s[i] == ',' or s[i] == '.')
  62.         {
  63.             if(s[i] != '\0') {
  64.                
  65.                 while ((s[i] != ',') or (s[i] != '.'))
  66.                 {
  67.                     substr[j] = s[i];//Добавление символа к фразе
  68.                     j++;
  69.                     i++;
  70.                     count++;
  71.                 }
  72.                 substr[j] = '\0';
  73.             }
  74.         }
  75.         if (count > Max)
  76.         {
  77.             substr2 = substr;
  78.             Max = count;
  79.         }
  80.         count = 0;
  81.         i++;
  82.     }
  83.     cout << substr2 << endl;
  84.    
  85.     cout << endl;
  86.     output.close();
  87.     system("pause");
  88.     return  0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement