Я создал эту программу, которая в значительной степени угадывает число, о котором должен думать пользователь, но в некоторых пределах c. Затем программа находит ответ, но в двоичных числах (1,0), и я должен преобразовать его в десятичную. Я создал массив, который сохраняет двоичное число, которое я должен конвертировать. Я также создал двоичный преобразователь. Дело в том, что я не могу придумать способ поместить массив в реальную функцию, чтобы преобразовать его в десятичное число. Мне действительно нужна помощь эксперта.
//Binary converter
int BinaryToDecimal(int n) {
int decimalNumber = 0;
int base = 1;
int temp = n;
while (temp) {
int lastDigit = temp % 10;
temp = temp / 10;
decimalNumber += lastDigit * base;
base = base * 2;
}
cout << "Decimal form of " << n << " is " << decimalNumber << endl;
return n;
}
case 6:
cout << ", think of a number between 1 and 63 and i will guess it..." << endl;
cout << "1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 " << endl << "Is your number in the above table?(y/n): ";
cin >> choice;
if (choice == 'y' && 'Y') { answer[1] = 1; }
else { answer[1] = 0; }
cout << "2 3 6 7 10 11 14 15 18 19 22 23 26 27 30 31 34 35 38 39 42 43 46 47 50 51 54 55 58 59 62 63 " << endl << "Is your number in the above table?(y/n): ";
cin >> choice;
if (choice == 'y' && 'Y') { answer[2] = 1; }
else { answer[2] = 0; }
cout << "4 5 6 7 12 13 14 15 20 21 22 23 28 29 30 31 36 37 38 39 44 45 46 47 52 53 54 55 60 61 62 63" << endl << "Is your number in the above table?(y/n): ";
cin >> choice;
if (choice == 'y' && 'Y') { answer[3] = 1; }
else { answer[3] = 0; }
cout << "8 9 10 11 12 13 14 15 24 25 26 27 28 29 30 31 40 41 42 43 44 45 46 47 56 57 58 59 60 61 62 63 " << endl << "Is your number in the above table?(y/n): ";
cin >> choice;
if (choice == 'y' && 'Y') { answer[4] = 1; }
else { answer[4] = 0; }
cout << "16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63" << endl << "Is your number in the above table?(y/n): ";
cin >> choice;
if (choice == 'y' && 'Y') { answer[5] = 1; }
else { answer[5] = 0; }
cout << "32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 " << endl << "Is your number in the above table?(y/n): ";
cin >> choice;
if (choice == 'y' && 'Y') { answer[6] = 1; }
else { answer[6] = 0; }
for (int i = size; i > 0; i--) { cout << answer[i]; }
cout << BinaryToDecimal(); **//My problem lies here. How can i put answer[] into this fucntion to convert it.**
break;
}
РЕДАКТИРОВАТЬ: мне удалось сделать эту работу !! Большое спасибо!
case 6:
cout << ", think of a number between 1 and 63 and i will guess it..." << endl;
cout << "1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 " << endl << "Is your number in the above table?(y/n): ";
cin >> choice;
if ((choice == 'y') || (choice == 'Y')) { answer[0] = 1; }
else { answer[0] = 0; }
cout << "2 3 6 7 10 11 14 15 18 19 22 23 26 27 30 31 34 35 38 39 42 43 46 47 50 51 54 55 58 59 62 63 " << endl << "Is your number in the above table?(y/n): ";
cin >> choice;
if ((choice == 'y') || (choice == 'Y')) { answer[1] = 1; }
else { answer[1] = 0; }
cout << "4 5 6 7 12 13 14 15 20 21 22 23 28 29 30 31 36 37 38 39 44 45 46 47 52 53 54 55 60 61 62 63" << endl << "Is your number in the above table?(y/n): ";
cin >> choice;
if ((choice == 'y') || (choice == 'Y')) { answer[2] = 1; }
else { answer[2] = 0; }
cout << "8 9 10 11 12 13 14 15 24 25 26 27 28 29 30 31 40 41 42 43 44 45 46 47 56 57 58 59 60 61 62 63 " << endl << "Is your number in the above table?(y/n): ";
cin >> choice;
if ((choice == 'y') || (choice == 'Y')) { answer[3] = 1; }
else { answer[3] = 0; }
cout << "16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63" << endl << "Is your number in the above table?(y/n): ";
cin >> choice;
if ((choice == 'y') || (choice == 'Y')) { answer[4] = 1; }
else { answer[4] = 0; }
cout << "32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 " << endl << "Is your number in the above table?(y/n): ";
cin >> choice;
if ((choice == 'y') || (choice == 'Y')) { answer[5] = 1; }
else {
answer[5] = 0;
}
break;
}
//reverses(answer, n);
int b = 0;
for (int i = 0; i < size; i++)
b += answer[i] * pow(2, i);
cout << "The number your are thinking is hmm, " << b << " hehe." << endl;
system("pause");
return 0;
}