结对编程——随机数四则运算
作业要求:

注:本次作业(结对编程)由我(2352228)和学号为2352234的同学结对完成
首先以下为实现随机生成300道1000以内加减乘除计算的代码:
点击查看代码
#include <iostream>
#include <random>
#include <vector>
#include <string>
#include <iomanip>
#include <cmath> // 用于fabs()
using namespace std;
struct Question {
string expression;
double answer;
};
// 全局随机引擎(整个程序共用)
static mt19937 e(random_device{}());
Question generateQuestion() {
uniform_int_distribution<int> num_dist(0, 100);
uniform_int_distribution<int> op_dist(1, 4); // 1:+, 2:-, 3:*, 4:/
const int MAX_ATTEMPTS = 100;
int attempts = 0;
while (attempts < MAX_ATTEMPTS) {
int num1 = num_dist(e);
int num2 = num_dist(e);
int num3 = num_dist(e);
int op1 = op_dist(e);
int op2 = op_dist(e);
// 计算第一个运算
double step1;
char op1_char;
switch (op1) {
case 1: op1_char = '+'; step1 = num1 + num2; break;
case 2: op1_char = '-'; step1 = num1 - num2; break;
case 3: op1_char = '*'; step1 = num1 * num2; break;
case 4:
op1_char = '/';
if (num2 == 0) num2 = 1; // 避免除零
step1 = static_cast<double>(num1) / num2;
break;
}
// 计算第二个运算
double final_answer;
char op2_char;
switch (op2) {
case 1: op2_char = '+'; final_answer = step1 + num3; break;
case 2: op2_char = '-'; final_answer = step1 - num3; break;
case 3: op2_char = '*'; final_answer = step1 * num3; break;
case 4:
op2_char = '/';
if (num3 == 0) num3 = 1; // 避免除零
final_answer = step1 / num3;
break;
}
// 答案必须在0-1000之间
if (final_answer >= 0 && final_answer <= 1000) {
string expr = to_string(num1) + " " + op1_char + " "
+ to_string(num2) + " " + op2_char + " "
+ to_string(num3) + " = ";
return { expr, final_answer };
}
attempts++;
}
// 保底题目
return { "10 + 10 + 10 = ", 30 };
}
int main() {
const int TOTAL_QUESTIONS = 300;
const int QUESTIONS_PER_LINE = 5;
const int EXPRESSION_WIDTH = 25;
// 阶段1:生成所有题目(固定顺序)
vector<Question> question_bank;
cout << "生成题目中..." << endl;
for (int i = 0; i < TOTAL_QUESTIONS; i++) {
question_bank.push_back(generateQuestion());
}
// 阶段2:显示所有题目
cout << "\n=== 题目列表 ===" << endl;
for (int i = 0; i < TOTAL_QUESTIONS; i++) {
cout << left << setw(EXPRESSION_WIDTH) << question_bank[i].expression;
if ((i + 1) % QUESTIONS_PER_LINE == 0) cout << endl;
}
// 阶段3:答题模式(严格按生成顺序)
cout << "\n=== 答题模式 ===" << endl;
cout << "提示:请按上方题目顺序作答,输入答案后按回车\n\n";
int correct_count = 0;
for (int i = 0; i < TOTAL_QUESTIONS; i++) {
double user_answer;
cout << "第" << i + 1 << "题: " << question_bank[i].expression;
cin >> user_answer;
// 允许0.01的浮点误差
if (fabs(user_answer - question_bank[i].answer) < 0.01) {
cout << "✔ 正确\n";
correct_count++;
}
else {
cout << "✖ 错误 (正确答案: " << question_bank[i].answer << ")\n";
}
// 每10题显示进度
if ((i + 1) % 10 == 0) {
cout << "[进度] " << i + 1 << "/300 | 正确率: "
<< correct_count << "/" << i + 1 << " ("
<< (correct_count * 100 / (i + 1)) << "%)\n";
}
}
// 最终结果
cout << "\n=== 最终结果 ===" << endl;
cout << "总正确率: " << correct_count << "/" << TOTAL_QUESTIONS << " ("
<< (correct_count * 100 / TOTAL_QUESTIONS) << "%)\n";
return 0;
}
其次,为完成本次任务,我们途中遇到的问题包括但不限于随机数的生成、数组溢出、输出结果偏离预期、无法判断用户输入正确性等,篇幅有限所以不在赘述。
特色部分:
1.为了能够生成足够随机的0100以内的数字,我们首先尝试了rand()函数并限定范围为0100和1~100(除数不能为0),但测试的过程中发现生成的数字无法满足需求

rand()生成的随机数其实存在一定的规律,我们无法避免,所以只好采用更加随机的随机数引擎mt19937

这样就能保证我们生成的数字足够随机且不重复
2.限定答案范围0~1000
点击查看代码
// 答案必须在0-1000之间
if (final_answer >= 0 && final_answer <= 1000) {
string expr = to_string(num1) + " " + op1_char + " "
+ to_string(num2) + " " + op2_char + " "
+ to_string(num3) + " = ";
return { expr, final_answer };
}
点击查看代码
const int TOTAL_QUESTIONS = 300;//题目总数
const int QUESTIONS_PER_LINE = 5;//每行5题
const int EXPRESSION_WIDTH = 25;//每个式子所占的长度
4.运行结果展示


以上就是我们此次结对编程的成果展示

浙公网安备 33010602011771号