Sc3ric

lab2

Dec 4th, 2022 (edited)
463
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include<iostream>
  2. #include<cstdlib>
  3. #include<Windows.h>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8.     SetConsoleCP(1251);
  9.     SetConsoleOutputCP(1251);
  10.     setlocale(LC_ALL, "Russian");
  11.     const int num_length = 4;
  12.  
  13.     char* numStr = new char[num_length + 1];
  14.     int num = 0;
  15.  
  16.     srand(time(0));
  17.  
  18.     int i = 0;
  19.  
  20.     while (i != num_length) {
  21.         int rnum = rand() % 10;
  22.  
  23.         if (rnum == 0 && i == 0) {
  24.             continue;
  25.         }
  26.  
  27.         num = num * 10 + rnum;
  28.         i++;
  29.     }
  30.     snprintf(numStr, num_length + 1, "%d", num);
  31.  
  32.     char* userNumStr = new char[1];
  33.     char* eptr = new char[1];
  34.     *eptr = '0';
  35.     int userNum;
  36.  
  37.     int bulls = 0;
  38.     int cows = 0;
  39.  
  40.     while (bulls != num_length) {
  41.         while (*eptr != '\0' || strlen(userNumStr) != 4) {
  42.             userNumStr = new char[10];
  43.             cout << "Введите число: ";
  44.             cin >> userNumStr;
  45.             userNum = strtol(userNumStr, &eptr, 10);
  46.         }
  47.         *eptr = '0';
  48.  
  49.  
  50.         bulls = 0;
  51.         cows = 0;
  52.         char* fn = new char[num_length + 1];
  53.         for (int i = 0; i < num_length + 1; i++) {
  54.             fn[i] = '\0';
  55.         }
  56.         int fni = 0;
  57.  
  58.         for (int i = 0; i < num_length; i++) {
  59.             if (numStr[i] == userNumStr[i]) {
  60.                 bulls++;
  61.             }
  62.             else {
  63.                 fn[fni++] = numStr[i];
  64.             }
  65.         }
  66.  
  67.         for (int i = 0; i < num_length; i++) {
  68.             if (fn[i] != '\0') {
  69.                 for (int j = 0; j < num_length; j++) {
  70.                     if (fn[i] == userNumStr[j]) {
  71.                         cows++;
  72.                         break;
  73.                     }
  74.                 }
  75.             }
  76.         }
  77.  
  78.         cout << "Количество быков: " << bulls << endl;
  79.         cout << "Количество коров: " << cows << endl << endl;
  80.     }
  81.  
  82.  
  83.     cout << endl;
  84.     cout << "Ты победил!" << endl;
  85.     cout << "Число действительно равно " << num << endl;
  86.  
  87.     return 0;
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment