Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.02 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3. #include<locale.h>
  4.  
  5. #define INPUT "input.txt"
  6. #define OUTPUT "output.txt"
  7.  
  8. using namespace std;
  9.  
  10. struct Word
  11. {
  12.     char ch = 0;
  13.     Word* next;
  14. };
  15.  
  16. struct Line
  17. {
  18.     int count = 0;
  19.     Word* word;
  20.     Line* next;
  21. };
  22.  
  23. void search(Line* line, int find)
  24. {
  25.     Word* wordNow;
  26.     wordNow = line->word->next;
  27.     while (wordNow != NULL)
  28.         {
  29.             if (((int(wordNow->ch))%2)==find)
  30.                 line->count++;
  31.             cout << wordNow->ch;
  32.             wordNow = wordNow->next;
  33.         }
  34.         cout << "<-" << "\n";
  35.         cout << line->count << "\n";
  36. }
  37.  
  38.  
  39. void main()
  40. {
  41.     setlocale(LC_ALL, "Russian");
  42.     char ch = 0;
  43.     int find;
  44.     bool point = false;
  45.  
  46.     fstream file;
  47.     file.open(INPUT);
  48.  
  49.     if (file.is_open())
  50.     {
  51.  
  52.         cout << "Четное или нечетное: ";
  53.         cin >> find;
  54.         find = find % 2;
  55.  
  56.         Line lineFirst;
  57.         Line* lineNow;
  58.         lineNow = &lineFirst;
  59.         lineFirst.word = new Word;
  60.         Word* wordNow = lineFirst.word;
  61.  
  62.         while ((!file.eof()) && (ch != '.'))
  63.         {
  64.             ch = file.get();
  65.             if (ch == '.') point = true;
  66.             if ((ch != ' ') && (ch != '.'))
  67.             {
  68.                 wordNow->next = new Word;
  69.                 wordNow = wordNow->next;
  70.                 wordNow->ch = ch;
  71.             }
  72.             else if (wordNow->ch != 0)
  73.             {
  74.                 lineNow->next = new Line;
  75.                 lineNow = lineNow->next;
  76.                 lineNow->word = new Word;
  77.                 wordNow->next = NULL;
  78.                 wordNow = lineNow->word;
  79.             }
  80.         }
  81.         file.close();
  82.         wordNow->next = NULL;
  83.         lineNow->next = NULL;
  84.         if (point == true)
  85.         {
  86.             file.open(OUTPUT, ios_base::out | ios_base::trunc);
  87.             if (file.is_open())
  88.             {
  89.                 if (lineFirst.word->next != NULL)
  90.                 {
  91.                     lineNow = &lineFirst;
  92.                     while (lineNow->next != NULL)
  93.                     {
  94.                         search(lineNow, find);
  95.                         file << lineNow->count << " ";
  96.                         lineNow = lineNow->next;
  97.                     }
  98.                 }
  99.                 else
  100.                 {
  101.                     cout << "File is empty!\n";
  102.                 }
  103.  
  104.                 file.close();
  105.  
  106.             }
  107.             else
  108.             {
  109.  
  110.                 cout << "Output file not found!\n";
  111.  
  112.             }
  113.  
  114.         }
  115.         else
  116.         {
  117.             cout << "Point not found!\n";
  118.         }
  119.  
  120.  
  121.     }
  122.     else {
  123.  
  124.         cout << "Input file not found!\n";
  125.  
  126.     }
  127.     cin >> ch;
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement