//小型加减乘除计算器
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
void menu()
{
cout << "Main menu" << endl;
cout << "1: Addition" << endl;
cout << "2: Subtraction" << endl;
cout << "3: Multiplication" << endl;
cout << "4: Division" << endl;
cout << "5: Exit" << endl;
}
void addition(int a,int b)
{
cout << "What is " << a << " + " << b << "? " ;
}
int addition_check(int n,int a,int b)
{
if(n==a+b)
{
cout << "correct" << endl;
}
else
{
cout << "wrong" << endl;
cout << "The correct answer is " << a+b << endl;
}
}
void subtraction(int a,int b)
{
if(a>=b)
{
cout << "What is " << a << " - " << b << "? ";
}
else
cout << "What is " << b << " - " << a << "? " ;
}
int subtraction_check(int n,int a,int b)
{
if(a>=b)
{
if(n==a-b)
{
cout << "correct" << endl;
}
else
{
cout << "wrong" << endl;
cout << "The correct answer is " << a-b << endl;
}
}
else
{
if(n==b-a)
{
cout << "correct" << endl;
}
else
{
cout << "wrong" << endl;
cout << "The correct answer is " << b-a << endl;
}
}
}
void multiplication(int a,int b)
{
cout << "What is " << a << " * " << b << "? ";
}
int multiplication_check(int n,int a,int b)
{
if(n==a*b)
{
cout << "correct" <<endl;
}
else
{
cout << "wrong" << endl;
cout << "The correct answer is " << a*b << endl;
}
}
void division(int a,int b)
{
if(b!=0)
{
cout << "What is " << a << " / " << b << "? ";
}
}
int division_check(int n,int a,int b)
{
if(n==a/b)
{
cout << "correct" <<endl;
}
else
{
cout << "wrong" << endl;
cout << "The correct answer is " << a/b << endl;
}
}
int main()
{
srand(time(0));
menu();
int c;
cout << "Enter a choice: ";
while(cin >> c)
{
int n;
int a=rand()%10;
int b=rand()%10;
switch(c)
{
case 1:
addition(a,b);
cin >> n;
addition_check(n,a,b);
menu();
cout << "Enter a choice: ";
break;
case 2:
subtraction(a,b);
cin >> n;
subtraction_check(n,a,b);
menu();
cout << "Enter a choice: ";
break;
case 3:
multiplication(a,b);
cin >> n;
multiplication_check(n,a,b);
menu();
cout << "Enter a choice: ";
break;
case 4:
division(a,b);
cin >> n;
division_check(n,a,b);
menu();
cout << "Enter a choice: ";
break;
case 5:
return 0;
default:
cout << "you input the wrong choice" << endl;
menu();
cout << "Enter a choice: ";
}
}
return 0;
}