Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<cstdlib>
- #include<Windows.h>
- using namespace std;
- int main() {
- SetConsoleCP(1251);
- SetConsoleOutputCP(1251);
- setlocale(LC_ALL, "Russian");
- const int num_length = 4;
- char* numStr = new char[num_length + 1];
- int num = 0;
- srand(time(0));
- int i = 0;
- while (i != num_length) {
- int rnum = rand() % 10;
- if (rnum == 0 && i == 0) {
- continue;
- }
- num = num * 10 + rnum;
- i++;
- }
- snprintf(numStr, num_length + 1, "%d", num);
- char* userNumStr = new char[1];
- char* eptr = new char[1];
- *eptr = '0';
- int userNum;
- int bulls = 0;
- int cows = 0;
- while (bulls != num_length) {
- while (*eptr != '\0' || strlen(userNumStr) != 4) {
- userNumStr = new char[10];
- cout << "Введите число: ";
- cin >> userNumStr;
- userNum = strtol(userNumStr, &eptr, 10);
- }
- *eptr = '0';
- bulls = 0;
- cows = 0;
- char* fn = new char[num_length + 1];
- for (int i = 0; i < num_length + 1; i++) {
- fn[i] = '\0';
- }
- int fni = 0;
- for (int i = 0; i < num_length; i++) {
- if (numStr[i] == userNumStr[i]) {
- bulls++;
- }
- else {
- fn[fni++] = numStr[i];
- }
- }
- for (int i = 0; i < num_length; i++) {
- if (fn[i] != '\0') {
- for (int j = 0; j < num_length; j++) {
- if (fn[i] == userNumStr[j]) {
- cows++;
- break;
- }
- }
- }
- }
- cout << "Количество быков: " << bulls << endl;
- cout << "Количество коров: " << cows << endl << endl;
- }
- cout << endl;
- cout << "Ты победил!" << endl;
- cout << "Число действительно равно " << num << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment