算数(小猴编程)

算术游戏

#include <iostream>
#include <ctime>
#include <algorithm>
#include <cstdlib>
using namespace std;

int main()
{
    srand(time(0)); 
    int tsum = 0;
    int maxt = 0;
    int mint = 1e9;
    int wrongCount = 0, rightCount = 0;
    int n;
    cout << "请输入一个算式(+-*/)中两个数最大为几(小于21亿)" << endl;
    cin >> n;
    for (int i = 1; i <= 10 ; i++)  //请写出循环条件
    {
       	int a = rand() % n + 1;
        int b = rand() % n + 1;
        int op = rand() % 4 + 1;
        int res;
        if (op == 1) // 加法
        {
            cout << a << " + " << b << " = " << endl;
            res = a + b;
        }
        if (op == 2) // 减法
        {
            if (a < b)
            {
                int c = a;
                a = b;
                b = c;
            }
            cout << a << " - " << b << " = " << endl;
            res = a - b;
        }
        if (op == 3) // 乘法
        {
            cout << a << " * " << b << " = " << endl;
            res = a * b;
        }
        if (op == 4) // 除法
        {
            cout << a * b << " / " << a << " = " << endl;
            res = b;
        }
	    int ans, beg = time(0); // 开始时间
	    cout << "请输入你的计算结果:" << endl;
	    cin >> ans;
		if (ans == res)
        {
            cout << "(^-^)V 答对了!" << endl;
            rightCount++;
        }
        else
        {
            cout << "┭┮﹏┭┮ 答错了!" << endl;
            wrongCount++;
        }
        cout << endl; 
        int end = time(0); // 结束时间
		tsum += end - beg;
		maxt = max(maxt, end - beg);
		mint = min(mint, end - beg);
        cout << "本题用时:" << end - beg << " 秒" << endl << endl;
    }
    cout << "( ̄▽ ̄)~* 游戏结束!" << endl;
	cout << "总计:" << rightCount * 10 << " 分" <<endl;
    cout << "对:" << rightCount << " 道" << endl;
    cout << "错:" << wrongCount << " 道" << endl;
    cout << "总用时:" << tsum << " 秒" << endl;
    cout << "平均用时:" << tsum / 10.0 << " 秒" << endl;
    cout << "最慢用时:" << maxt << endl;
    cout << "最快用时:" << mint << endl;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值