c++编写随机四则运算的练习题
时间: 2024-05-12 09:12:57 浏览: 76
以下是一个简单的随机四则运算的练习题的C++代码:
```c++
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
srand(time(0)); // 设置随机数种子
int num1, num2, answer, user_answer;
char op;
for (int i = 1; i <= 10; i++) // 进行10次练习
{
num1 = rand() % 101; // 生成0-100之间的随机数
num2 = rand() % 101;
int op_index = rand() % 4; // 生成0-3之间的随机数,代表加减乘除
switch (op_index)
{
case 0:
op = '+';
answer = num1 + num2;
break;
case 1:
op = '-';
answer = num1 - num2;
break;
case 2:
op = '*';
answer = num1 * num2;
break;
case 3:
op = '/';
answer = num1 / num2;
break;
}
cout << "第" << i << "题:" << num1 << " " << op << " " << num2 << " = ";
cin >> user_answer;
if (user_answer == answer)
{
cout << "回答正确!" << endl;
}
else
{
cout << "回答错误!正确答案是:" << answer << endl;
}
}
return 0;
}
```
这个程序会生成10个随机的四则运算题目,每个题目的数值和运算符都是随机的,用户需要输入答案,程序会判断答案是否正确并输出相应的提示。
阅读全文