P174 5.55 简易计算器(加减乘除)

     

//小型加减乘除计算器
#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值