#include<iostream>
#include<iomanip>
#include<cstdlib>
#include"formatNumber.h"
using namespace std;
int main()
{
int choice;
int num1, num2, num3, num4, num5, num6;
int problem;
unsigned seed = time(0);
srand(seed);
cout << "This program will help you learn addition, subtraction,\n";
cout << "multiplication, and division. Press any key to continue." << endl;
cin.get();
do
{
cout << "Choose which math you would like to work on." << endl;
cout << "--------------------------------------------" << endl;
cout << "1. Addition\n";
cout << "2. Subtraction\n";
cout << "3. Multiplication\n";
cout << "4. Quit program\n";
cout << endl;
cout << "Choose 1,2,3,or 4: ";
cin >> choice;
switch(choice)
{
case 1: problem = 0;
num1 = rand() % 11;
num2 = rand() % 11;
while (problem != 999)
{
cout << "How much is " << num1 << " plus " << num2 << " (999 to exit)? ";
cin >> problem;
if (problem == 999)
break;
if (problem == (num1 + num2))
{
switch(rand() % 4)
{
case 0: cout << "Very good!\n";
break;
case 1: cout << "Nice work!\n";
break;
case 2: cout << "Excellent!\n";
break;
case 3: cout << "Keep up the good work!\n";
break;
}
num1 = rand() % 11;
num2 = rand() % 11;
}
else
{
switch(rand() % 4)
{
case 0: cout << "No. Please try again\n";
break;
case 1: cout << "Sorry. That's not it\n";
break;
case 2: cout << "No. Don't give up\n";
break;
case 3: cout << "Not yet. Keep trying\n";
break;
cout << "How much is " << num1 << " plus " << num2 << "(999 to exit)? ";
cin >> problem;
}
}
}
break;
case 2: problem = 0;
num3 = rand() % 11;
num4 = rand() % 11;
while (problem != 999)
{
cout << "How much is " << num3 << " minus " << num4 << " (999 to exit)? ";
cin >> problem;
if (problem == 999)
break;
if (problem == (num3 - num4))
{
switch(rand() % 4)
{
case 0: cout << "Very good!\n";
break;
case 1: cout << "Nice work!\n";
break;
case 2: cout << "Excellent!\n";
break;
case 3: cout << "Keep up the good work!\n";
break;
}
num3 = rand() % 11;
num4 = rand() % 11;
}
else
{
switch(rand() % 4)
{
case 0: cout << "No. Please try again\n";
break;
case 1: cout << "Sorry. That's not it\n";
break;
case 2: cout << "No. Don't give up\n";
break;
case 3: cout << "Not yet. Keep trying\n";
break;
cout << "How much is " << num1 << " plus " << num2 << "(999 to exit)? ";
cin >> problem;
}
}
}
break;
case 3: problem = 0;
num5 = rand() % 11;
num6 = rand() % 11;
while (problem != 999)
{
cout << "How much is " << num5 << " times " << num6 << " (999 to exit)? ";
cin >> problem;
if (problem == 999)
break;
if (problem == (num5 * num6))
{
switch(rand() % 4)
{
case 0: cout << "Very good!\n";
break;
case 1: cout << "Nice work!\n";
break;
case 2: cout << "Excellent!\n";
break;
case 3: cout << "Keep up the good work!\n";
break;
}
num5 = rand() % 11;
num6 = rand() % 11;
}
else
{
switch(rand() % 4)
{
case 0: cout << "No. Please try again\n";
break;
case 1: cout << "Sorry. That's not it\n";
break;
case 2: cout << "No. Don't give up\n";
break;
case 3: cout << "Not yet. Keep trying\n";
break;
cout << "How much is " << num1 << " plus " << num2 << "(999 to exit)? ";
cin >> problem;
}
}
}
break;
case 4: cout << "Thank you and have a good day!\n";
break;
default : cout << "Invalid response! please choose (1,2,3,4,or 5)." << endl;
cin >> choice;
}
}while (choice != 4);
cin.get();
return 0;
}
Эта программа позволяет пользователю решить, какую математику они хотят делать. остальное - список случайных чисел, сгенерированных для математических задач. Кто-нибудь знает, как всегда делать num3 больше или равно num4 в случае вычитания?, имеющий проблему в случае вычитания (делая num3 всегда больше, чем num4 в случайном генераторе). кто знает, как
В качестве альтернативы вы можете просто добавить предложение 'if', чтобы проверить большее число, и если' num4' больше, чем 'num3', поменяйте их на – Tacocat
. Вы должны прочитать [Минимальные, полные и проверенные примеры] (https : //stackoverflow.com/help/mcve). Но, как сказал Такокат, самой простой задачей было бы создать два числа и поменять их по мере необходимости. –