Advertisement
Guest User

Untitled

a guest
May 25th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <conio.h>
  5. #include <windows.h>
  6. #include <time.h>
  7. #include <string>
  8. using namespace std;
  9. float liter(int n, string name)
  10. {
  11.     clock_t start, end;
  12.     int t, key;
  13.     char k;
  14.     float tm = 0;
  15.     for (int i=0; i < n; i++)
  16.     {
  17.         system("cls");
  18.         cout << "* Игра \"Нажми клавишу\" *\nИграет: " << name;
  19.         cout << "\nОсталось букв: " << n - i << endl;
  20.         t = 192 + rand() % 32;
  21.         k = (char)t;
  22.         cout << "\n Нажмите букву: " << k << endl;
  23.         start = clock();
  24.  
  25.         while (1)
  26.         {
  27.             key = _getch();
  28.             if (key == t || key - 32 == t)
  29.             {
  30.                 end = clock();
  31.                 tm += (float(end - start)) / CLOCKS_PER_SEC;
  32.                 break;
  33.             }
  34.         }
  35.     }
  36.     return tm;
  37. }
  38.  
  39. int main()
  40. {
  41.     SetConsoleOutputCP(1251);
  42.     SetConsoleCP(1251);
  43.     srand(time(0));
  44.  
  45.     int i, j, n;
  46.     float tm = 0, top[5];
  47.     string name, top_name[5];
  48.  
  49.     do{
  50.         system("cls");
  51.         cout << " Игра \"Нажми клавишу\"";
  52.  
  53.         ifstream in("res.txt");
  54.         i = 0;
  55.         cout << "\n\nНаилучшие результаты:\n\n<Имя> <Результат>\n  " << endl;
  56.         while (!in.eof() && i < 5)
  57.         {
  58.             in >> top_name[i] >> top[i];
  59.             cout << top_name[i] << "\t" << top[i] << endl;
  60.             i++;
  61.         }
  62.  
  63.         in.close();
  64.  
  65.         cout << "\n\nВведите имя: ";
  66.         cin >> name;
  67.        
  68.         do{
  69.             cout << "Количество букв (10-50): ";
  70.             cin >> n;
  71.         } while (n<10 || n>50);
  72.  
  73.  
  74.         tm = liter(n, name);
  75.         system("cls");
  76.         cout << "* Игра \"Нажми клавишу\" *\nИграет: " << name;
  77.         cout << "\n\nИгра окончена\nВаше среднее время: " << tm / n << endl;
  78.  
  79.  
  80.         for (i = 0; i < 5; i++)
  81.             if (top[i] > tm / n) break;
  82.         if (i < 5)
  83.         {
  84.             for (j = 4; j>i; j--)
  85.             {
  86.                 top[j] = top[j - 1];
  87.                 top_name[j] = top_name[j - 1];
  88.             }
  89.             top[i] = tm / n;
  90.             top_name[i] = name;
  91.         }
  92.         ofstream file("res.txt");
  93.         file.clear();
  94.         cout << "\n\nНаилучшие результаты:\n\n<Имя> <Результат>\n" << endl;
  95.         for (i = 0; i < 5; i++)
  96.         {
  97.             file << top_name[i] << ' ' << top[i] << endl;
  98.             cout << top_name[i] << "\t" << top[i] << endl;
  99.         }
  100.         file.close();
  101.  
  102.         cout << "Сыграть еще раз нажмите SPACE";
  103.         n = _getch();
  104.     } while (n == 32);
  105.     system("pause");
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement