算术游戏
#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;
}