Petro_zzz

lesson11_322

Aug 25th, 2023
1,318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.94 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void calc_average() {
  5.     int b;
  6.     cout << "Ввоедите оценки для вычисления вседнего\n";
  7.     cout << "Для окончания введите 0\n";
  8.  
  9.     int sum = 0, n = 0;
  10.     //b = 1;
  11.     cin >> b;
  12.     while (b > 0 && b <= 5) {
  13.         sum += b;
  14.         n++;
  15.         cin >> b;      
  16.     }
  17.  
  18.     cout << "Средний балл: "
  19.          << (double)sum/n
  20.          << endl;
  21. }
  22.  
  23. double power(double x, int n) {
  24.     /*double x = ;
  25.     int n = ;*/
  26.     /*
  27.     if (n == 0)
  28.         return 1;
  29.         */
  30.     double y = 1;  
  31.     while (n < 0) {
  32.         y /= x;
  33.         n++;
  34.     }
  35.     while (n > 0) {
  36.         y *= x;
  37.         n--;
  38.     }
  39.     //return pow(x, n);
  40.     return y;
  41. }
  42.  
  43. double power2(double x, int n) {
  44.     double y = 1;
  45.     if (n < 0) {
  46.         n = -n;
  47.         x = 1 / x;  // x = x / 2  --> x /= 2
  48.     }
  49.     while (n > 0) {
  50.         y *= x;
  51.         n--;
  52.     }
  53.     //return pow(x, n);
  54.     return y;
  55. }
  56.  
  57. void table_mult() {
  58.     cout << "x\\y\t";
  59.     int x = 2, y = 1;
  60.  
  61.     while (y < 10) {       
  62.         while (x < 10) {
  63.             cout << x * y << '\t';
  64.             x++;
  65.         }
  66.         x = 1;
  67.         cout << endl;
  68.         y++;
  69.     }
  70. }
  71.  
  72. int get_rand(int a, int b) {
  73.     return rand() % (b - a + 1) + a;
  74. }
  75.  
  76. int one_humanstep(int n) {
  77.     int number;
  78.     switch (n)
  79.     {
  80.     case 1: cout << "Первая"; break;
  81.     case 2: cout << "Вторая"; break;
  82.     case 3: cout << "Третья"; break;
  83.     default: cout << n << "-ая"; break;
  84.     }
  85.     cout << " попытка. Введите число :";
  86.     cin >> number;
  87.     return number;
  88. }
  89.  
  90.  
  91. void run_game() {
  92.     /*
  93.            "УГАДАЙ ЧИСЛО"
  94.  
  95.         Ваша задача угадать число за
  96.         наименьшее число попыток. Если
  97.         Вы сдаётесь нажмите 0.
  98.  
  99.         Начнём игру? 1 - да, 0 - нет.
  100.         1
  101.         Я загадал число от 0 до 500. Угадывай.
  102.        
  103.         Первая попытка. Введите число: 250.
  104.  
  105.         250 больше числа, которое я загадал.
  106.  
  107.         Вторая попытка. Введите число: 120.
  108.         120 меньше числа, которое я загадал.
  109.  
  110.         Третья попытка. Введите число: 134.
  111.         Поздравляю, Вы угадали.
  112.     */
  113.  
  114.     cout << "      \"УГАДАЙ ЧИСЛО\"    " << endl;
  115.     cout << R"(
  116. Ваша задача угадать число за
  117. наименьшее число попыток.
  118. Если Вы сдаётесь нажмите 0.
  119. )" << endl;
  120.     cout << "Начнём игру? 1 - да, 0 - нет." << endl;
  121.     int is_start, ai_number, number;
  122.     int step = 0;
  123.     cin >> is_start;
  124.     if (is_start) {
  125.         cout << "Я загадал число от 0 до 500. Угадывай." << endl;
  126.         ai_number = get_rand(1, 500);      
  127.         ai_number = 55;
  128.         while (true) {
  129.             step++;
  130.             number = one_humanstep(step);
  131.             if (number <= 0 || number == ai_number) {
  132.                 break;
  133.             }
  134.         }
  135.         if (number == ai_number) {
  136.             cout << "Выйграли" << endl;
  137.         }
  138.         else {
  139.             cout << "Проиграли" << endl;
  140.         }
  141.        
  142.         //cout << ai_number << endl;
  143.         /*
  144.         cout << "Первая попытка.Введите число :";
  145.         cin >> number;
  146.         if (number != 0) {
  147.             if (number != ai_number) {
  148.                 cout << number << "больше числа, которое я загадал." << endl;
  149.                 cout << "Вторая попытка.Введите число :";
  150.                 cin >> number;
  151.                 if (number != 0) {
  152.                     if (number != ai_number) {
  153.             }
  154.         }
  155.         */
  156.  
  157.     }
  158. }
  159.  
  160.  
  161.  
  162. void test_random() {
  163.     cout << "time: " << time(NULL) << endl << endl;
  164.     int n = 0; 
  165.     while (n < 50) {
  166.         //int x = rand() % 500 + 1; /// [0, 499] --> [1, 500]
  167.         int x = get_rand(-5, 5);
  168.         cout << x << endl;
  169.         n++;
  170.     }
  171. }
  172.  
  173.  
  174.  
  175. int main() {
  176.     setlocale(LC_ALL, "ru");
  177.     srand(time(NULL));
  178.  
  179.     /*one_humanstep(1);
  180.     one_humanstep(2);
  181.     one_humanstep(3);
  182.     one_humanstep(400);
  183.     */
  184.  
  185.     //table_mult();
  186.     //calc_average();
  187.     //cout << power(2, -4) << endl;
  188.     /*cout << 2 * power(val, 4) << endl;
  189.     double y = power(3.0, 4) * sin(0.1);
  190.     cout << y << endl;*/
  191.     //test_random();
  192.     run_game();
  193. }
  194.  
Advertisement
Add Comment
Please, Sign In to add comment