Advertisement
Petro_zzz

lesson12_321

Aug 28th, 2023
1,387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.84 KB | None | 0 0
  1. // https://pastebin.com/7pB1mjmX
  2.  
  3. #include <iostream>
  4.  
  5. #define _USE_MATH_DEFINES
  6. #include <math.h>
  7.  
  8. using namespace std;
  9.  
  10. void calc_averege() {
  11.     cout << "Введите все оценки студента" << endl;
  12.     cout << "для прекращения ввода наберите 0." << endl;
  13.     int check, summ = 0, iter = 0;
  14.     cin >> check;
  15.     while (check > 0 && check <= 5) {
  16.         iter++;
  17.         summ += check;
  18.         cin >> check;
  19.     }
  20.     if (iter > 0)
  21.         cout << "Средний балл: " << (double)summ / iter << endl;
  22. }
  23.  
  24. double power(double x, int n) {
  25.     //double y = pow(x, n);
  26.     if (n == 0)
  27.         return 1;
  28.  
  29.     if (n < 0) {
  30.         n = -n;
  31.         x = 1.0 / x;
  32.     }
  33.  
  34.     double y = 1;
  35.     while (n > 0) {
  36.         n--;
  37.         y *= x;
  38.     }
  39.     return y;
  40. }
  41.  
  42. void test_pow() {
  43.     double x;
  44.     int n;
  45.     cin >> x >> n;
  46.     cout << power(2, -4) << endl;
  47.     cout << power(2, 4) << endl;
  48.     cout << power(x, n) << endl;
  49.     cout << power(M_PI, 2) << endl;
  50. }
  51.  
  52. void multy_table() {
  53.     // x\y | 2 3 4 ....
  54.     // --------------------------
  55.     //  2  | 4  6  8
  56.     //  3  | 6  9 12
  57.     //  4  | 8 12 16
  58.     //    ...
  59.     int x = 1, y = 1;
  60.  
  61.     while (x < 10) {
  62.         y = 1;
  63.         while (y < 10) {
  64.             if (y * x == 1)
  65.                 cout << "x\\y" << "\t";
  66.             else
  67.                 cout << y * x << "\t";
  68.             ++y;
  69.         }
  70.         x++;
  71.         cout << endl;
  72.     }
  73. }
  74.  
  75. int get_rand(int a, int b) {   
  76.     return rand() % (b - a + 1) + a;
  77. }
  78.  
  79. int get_onestep(int n) {
  80.     switch (n)
  81.     {
  82.     case 1: cout << "Первая "; break;
  83.     case 2: cout << "Вторая "; break;
  84.     case 3: cout << "Третья "; break;
  85.  
  86.     default: cout << n << "-ая "; break;
  87.     }
  88.     cout << "попытка. Введи твой ответ :";
  89.     int number;
  90.     cin >> number;
  91.     return number;
  92. }
  93.  
  94. bool run_game() {
  95.     /*
  96.          УГАДАЙ ЧИСЛО
  97.  
  98.     Я загадал число от 0 до 500. Попробуй его угадать.
  99.     У тебя будет 3 попытки. Если ввведёшь 0, то ты сдался.
  100.  
  101.     Певая попытка. Введи твой ответ: 26.
  102.     Неугадал. 26 меньше загаданного числа.
  103.  
  104.     Вторая попытка. Введи твой ответ: 467.
  105.     Неугадал. 467 больше загаданного числа.
  106.  
  107.     Третья попытка. Введи твой ответ: 365.
  108.     Я загадал 365. Поздравляю ты угадал число.
  109.  
  110.     Съиграем ещё? да - 1, нет - 0.
  111.     0
  112.  
  113.     Игр - 1, побед - 1, поражений - 0.
  114.     */
  115.  
  116.     cout << "          УГАДАЙ ЧИСЛО" << endl;
  117.     cout << "Я загадал число от 0 до 500. Попробуй его угадать." << endl;
  118.     cout << "У тебя будет 3 попытки.Если ввведёшь 0, то ты сдался." << endl;
  119.  
  120.     int ai_number =  get_rand(1, 500);
  121.     int step = 1;
  122.     int number;
  123.     while (true) {
  124.         number = get_onestep(step);
  125.         if (number == 0) {
  126.             break;
  127.         }
  128.         if (number == ai_number) {
  129.             break;
  130.         } // не будем рассматривать веткe else
  131.         cout << "Неугадал. " << number;
  132.         if (number > ai_number) {
  133.             cout << " больше";
  134.         }
  135.         else {
  136.             cout << " меньше";
  137.         }
  138.         cout << " загаданного числа." << endl;
  139.         step++;
  140.     }
  141.     if (number == ai_number) {
  142.         cout << "Выйгрыш" << endl;
  143.         return 1;
  144.     }
  145.     else {
  146.         cout << "Поражение" << endl;
  147.         return 0;
  148.     }
  149. }
  150.  
  151. void test_rand() {
  152.     int n = 0;
  153.     cout << "time:" << time(NULL) << endl;
  154.     /*
  155.     srand(time(NULL));
  156.     cout << rand() << endl;
  157.     cout << rand() << endl;
  158.     cout << rand() << endl;
  159.     cout << rand() << endl;
  160.     cout << rand() << endl;
  161.     */
  162.    
  163.     while (n < 50) {
  164.         cout << get_rand(-5, 5) << endl;
  165.         n++;
  166.     }  
  167. }
  168.  
  169. void test_get_onestep() {
  170.     int k = 1;
  171.     while (k < 5) {
  172.         int n = get_onestep(k);
  173.         cout << n << endl;
  174.         k++;
  175.     }
  176. }
  177.  
  178. void test_break_in_while() {
  179.     int k = 0;
  180.     while (k < 10) {
  181.         cout << k << " ";
  182.         k++;
  183.         if (k == 5) {
  184.             //;
  185.             break;
  186.         }
  187.     }
  188. }
  189.  
  190. double calc_poly(double x) {
  191.     double y = 3 * x * x + 2;
  192.     return y;
  193. }
  194.  
  195. void show_statistic(int game, int wins) {  
  196.     cout << "Побед/игр: " << wins
  197.         << "/" << game << endl;
  198. }
  199.  
  200. int main() {
  201.  
  202.     setlocale(LC_ALL, "ru");
  203.     srand(time(NULL));
  204.    
  205.    
  206.     /*
  207.     double x = 0;
  208.     while (x < 4) {
  209.         cout << calc_poly(x) << endl;
  210.         x += 0.5;
  211.     }
  212.     */ 
  213.     /*
  214.     cout << calc_poly(0) << endl;
  215.     cout << calc_poly(1) << endl;
  216.     cout << calc_poly(2) << endl;
  217.     cout << calc_poly(3) << endl;
  218.     */
  219.     //calc_averege();
  220.     //cout << M_1_PI << endl;
  221.     //multy_table();
  222.     int repeat = 1;
  223.     int game_num = 0;
  224.     int wins = 0;
  225.  
  226.     while (repeat) {
  227.         wins += run_game();
  228.         game_num++;
  229.         show_statistic(game_num, wins);
  230.         cout << "Съиграем ещё ? да - 1, нет - 0. ";    
  231.         cin >> repeat;
  232.         while (repeat < 0 || repeat > 1) {
  233.             cout << "Повторите ответ: ";
  234.             cin >> repeat;
  235.         }
  236.     }
  237.  
  238.     //test_rand();
  239.     //test_break_in_while();
  240.     //run_game();
  241.     system("pause");
  242.     return 0;
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement