Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.40 KB | None | 0 0
  1.  
  2. #include "stdafx.h"
  3. #include <iostream>
  4. #include <clocale>
  5. #include <cstdio>
  6. #include <cstdlib>
  7. #include <memory.h>
  8. #include <cstring>
  9. #include <Windows.h>
  10. #include <crtdbg.h>
  11. #include <string.h>
  12. #include <conio.h>
  13.  
  14. using namespace std;
  15.  
  16. void PrintMenu();
  17. char* Create();
  18. int Count(char* s);
  19.  
  20. int main()
  21. {
  22.     SetConsoleCP(1251);
  23.     SetConsoleOutputCP(1251);
  24.  
  25.     int menu = 0;
  26.  
  27.     char* s = NULL;
  28.     int count = 0;
  29.     bool f = false;
  30.  
  31.     do
  32.     {
  33.         PrintMenu();
  34.         cin >> menu;
  35.         switch (menu)
  36.         {
  37.         case 1:
  38.         {
  39.             if (s != NULL)
  40.             {
  41.                 delete[] s;
  42.                 count = 0;
  43.                 cout << "Массив очищен\n";
  44.  
  45.             }
  46.        
  47.             s = Create();
  48.  
  49.             break;
  50.         }
  51.  
  52.         case 2:
  53.         {
  54.             if ((s == NULL) || (s[0] == 13))
  55.             {
  56.                 cout << "Сначала введите строку\n";
  57.             }
  58.             else
  59.             {
  60.                 count = Count(s);
  61.                 f = true;
  62.             }
  63.             break;
  64.         }
  65.  
  66.         case 3:
  67.         {
  68.             if (f == false)
  69.             {
  70.                 cout << "Сначала нужно обработать строку\n";
  71.             }
  72.             else
  73.             {
  74.                 if ((s == NULL) || (s[0] == 13))
  75.                 {
  76.                     cout << "Сначала введите строку\n";
  77.                 }
  78.                 else
  79.                 {
  80.                     cout << "количество четырехбуквенных слов = " << count << "\n";
  81.                     f = false;
  82.  
  83.                 }
  84.             }
  85.             break;
  86.         }
  87.         case 0:
  88.         {
  89.             break;
  90.         }
  91.  
  92.         default:
  93.             cout << "Такого варианта нет.\n";
  94.         }
  95.  
  96.     } while (menu != 0);
  97.  
  98.     delete[] s;
  99.     return 0;
  100. }
  101.  
  102. void PrintMenu()
  103. {
  104.     cout << "-------------------\n";
  105.     cout << "1.Ввод \n";
  106.     cout << "2.Обработка\n";
  107.     cout << "3.Вывод\n";
  108.     cout << "0.Выход\n";
  109.     cout << "-------------------\n";
  110. }
  111.  
  112. int Count(char* s)
  113. {
  114.     int count = 0;
  115.     int cur = 0;
  116.     int i = 0;
  117.     while (s[i] != '\0')
  118.     {
  119.         if ((s[i] != ' ') && (s[i] != ',') && (s[i] != '.') && (s[i] != '!') && (s[i] != '?')
  120.             && (s[i] != ':') && (s[i] != ';'))
  121.         {
  122.             cur++;
  123.         }
  124.         else
  125.         {
  126.             if (cur == 4)
  127.             {
  128.                 count++;
  129.             }
  130.             cur = 0;
  131.         }
  132.         i++;
  133.     }
  134.  
  135.     if (cur == 4)
  136.         count++;
  137.     return count;
  138. }
  139.  
  140. char* Create()
  141. {
  142.     int len = 0;
  143.     char* s = NULL;
  144.     cout << "Введите строку\n";
  145.  
  146.     s = new char[1];
  147.     s[0] = '\0';
  148.     char ch = 0;
  149.     while (true)
  150.     {
  151.         ch = getchar();
  152.        
  153.         if (ch == '\n')
  154.         {
  155.         break;
  156.         }
  157.         len = strlen(s);
  158.         char* tmp = new char[len + 2];
  159.         strcpy(tmp, s);
  160.         tmp[len] = ch;
  161.         tmp[len + 1] = '\0';
  162.         delete[] s;
  163.  
  164.         s = tmp;
  165.     }
  166.    
  167.     return s;
  168. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement