Advertisement
Leeen

lab for Gaidel' №4 var 4

Nov 19th, 2018
365
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.41 KB | None | 0 0
  1. // Лабораторная работа для Гайделя №4 вариант 4
  2.  
  3. #include<iostream>
  4. #include<fstream>
  5. #include<string>
  6. #include<map>
  7.  
  8. using namespace std;
  9.  
  10.  
  11. bool Prod()
  12. {
  13.     cout << "Continue Y/N" << endl;
  14.     char yn;
  15.     cin >> yn;
  16.     cin.clear();
  17.     if (yn == 'Y' && cin.get() == '\n') return true;
  18.     else if (yn == 'N' && cin.get() == '\n') return false;
  19.     else
  20.     {
  21.         while (cin.get() != '\n');
  22.         cout << "Error. Try again" << endl;
  23.         Prod();
  24.     }
  25. }
  26.  
  27. int main()
  28. {
  29.     setlocale(LC_ALL, ".1251");
  30.  
  31.     string fileName;
  32.  
  33.     do {
  34.         ifstream file;
  35.         do {
  36.             cout << "Input a filename > ";
  37.             cin >> fileName;
  38.             file.open(fileName);
  39.             if (file.good())
  40.                 break;
  41.             cout << "Error: File not found" << endl;
  42.         } while (true);
  43.  
  44.         map <char, int> chars;
  45.         map <char, int>::iterator begin, at, end;
  46.  
  47.         string str;
  48.         if (file.is_open())
  49.         {
  50.             while (file)
  51.             {
  52.                 getline(file, str);
  53.                 for (int i = 0; i < str.length(); i++) {
  54.                     chars[str[i]]++;// считаем каждый символ
  55.                 }
  56.                 str = "";
  57.             };
  58.             file.close();
  59.  
  60.             begin = chars.begin();
  61.             end = chars.end();
  62.  
  63.             for (at = begin; at != end; at++)
  64.             {
  65.                 int i = 0;
  66.                 double coef = (double)at->second / chars.size();
  67.                 cout << at->first << " >" << at->second << " times >" << coef << endl; // выводим результат
  68.                 i++;
  69.             }
  70.         }
  71.     } while (Prod());
  72.  
  73.     system("pause");
  74.     return 0;
  75. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement