结对编程————四则运算
结对编程————四则运算
作者:2352827
伙伴:2352823
一、实验要求
小学老师要每周给同学出300道四则运算练习题。
两个运算符,100 以内的数字,不需要写答案。
需要检查答案是否正确,并且保证答案在 0..1000 之间
尽可能地多设置一些条件
请两位同学以结对编码(一个同学coding,另一个同学在旁边审核代码,之后再交换角色)的方式完成本次实验
二、实验环境
1、运行环境:
系统:Windows11 24H2
cpu:i7-12700
编译器:Dev c++ 5.11
2、完整代码
点击查看代码
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <string>
#include <limits>
using namespace std;
// 定义一个函数用于计算表达式的值,考虑运算符优先级
double calculate(double num1, char op1, double num2, char op2, double num3) {
double temp;
// 先处理第一个运算符是乘除的情况
if (op1 == '*' || op1 == '/') {
if (op1 == '*') {
temp = num1 * num2;
} else {
if (num2 == 0) {
return -1; // 避免除零错误
}
temp = num1 / num2;
}
// 再处理第二个运算符
if (op2 == '*') {
return temp * num3;
} else if (op2 == '/') {
if (num3 == 0) {
return -1; // 避免除零错误
}
return temp / num3;
} else if (op2 == '+') {
return temp + num3;
} else {
return temp - num3;
}
}
// 第一个运算符是加减,先处理第二个运算符是乘除的情况
if (op2 == '*' || op2 == '/') {
if (op2 == '*') {
temp = num2 * num3;
} else {
if (num3 == 0) {
return -1; // 避免除零错误
}
temp = num2 / num3;
}
if (op1 == '+') {
return num1 + temp;
} else {
return num1 - temp;
}
}
// 两个运算符都是加减的情况
if (op1 == '+') {
temp = num1 + num2;
} else {
temp = num1 - num2;
}
if (op2 == '+') {
return temp + num3;
} else {
return temp - num3;
}
}
int main() {
srand(static_cast<unsigned int>(time(nullptr)));
vector<string> exercises;
vector<double> answers;
char operators[] = {'+', '-', '*', '/'};
// 生成 300 道练习题
while (exercises.size() < 300) {
int num1 = rand() % 100 + 1;
int num2 = rand() % 100 + 1;
int num3 = rand() % 100 + 1;
char op1 = operators[rand() % 4];
char op2 = operators[rand() % 4];
double result = calculate(num1, op1, num2, op2, num3);
// 检查结果是否为整数且在 0 到 1000 之间
if (result >= 0 && result <= 1000 && static_cast<int>(result) == result) {
string expression = to_string(num1) + " " + string(1, op1) + " " + to_string(num2) + " " + string(1, op2) + " " + to_string(num3);
exercises.push_back(expression);
answers.push_back(result);
}
}
for (size_t i = 0; i < exercises.size(); i += 10) {
cout << "以下是十道四则运算题,请依次输入答案:" << endl;
for (size_t j = 0; j < 10 && i + j < exercises.size(); ++j) {
cout << "题目 " << j + 1 << ": " << exercises[i + j] << endl;
}
vector<double> userAnswers;
for (size_t j = 0; j < 10 && i + j < exercises.size(); ++j) {
double userAnswer;
while (true) {
cout << "请输入第 " << j + 1 << " 题的答案: ";
if (cin >> userAnswer) {
// 输入有效,清除错误标志并忽略多余输入
cin.ignore(numeric_limits<streamsize>::max(), '\n');
break;
} else {
// 输入无效,清除错误标志并忽略多余输入
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "输入无效,请输入一个有效的数字。" << endl;
}
}
userAnswers.push_back(userAnswer);
}
for (size_t j = 0; j < 10 && i + j < exercises.size(); ++j) {
if (userAnswers[j] == answers[i + j]) {
cout << "第 " << j + 1 << " 题回答正确!" << endl;
} else {
cout << "第 " << j + 1 << " 题回答错误,正确答案是: " << answers[i + j] << endl;
}
}
// 等待用户按下任意键继续
cout << "按任意键继续..." << endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.get();
// 清屏操作
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
return 0;
}
每次输出十道题并验证答案,直到300道题。

三、功能的实现与细节
1、calculate函数
点击查看代码
double calculate(double num1, char op1, double num2, char op2, double num3) {
double temp;
// 先处理第一个运算符是乘除的情况
if (op1 == '*' || op1 == '/') {
if (op1 == '*') {
temp = num1 * num2;
} else {
if (num2 == 0) {
return -1; // 避免除零错误
}
temp = num1 / num2;
}
// 再处理第二个运算符
if (op2 == '*') {
return temp * num3;
} else if (op2 == '/') {
if (num3 == 0) {
return -1; // 避免除零错误
}
return temp / num3;
} else if (op2 == '+') {
return temp + num3;
} else {
return temp - num3;
}
}
// 第一个运算符是加减,先处理第二个运算符是乘除的情况
if (op2 == '*' || op2 == '/') {
if (op2 == '*') {
temp = num2 * num3;
} else {
if (num3 == 0) {
return -1; // 避免除零错误
}
temp = num2 / num3;
}
if (op1 == '+') {
return num1 + temp;
} else {
return num1 - temp;
}
}
// 两个运算符都是加减的情况
if (op1 == '+') {
temp = num1 + num2;
} else {
temp = num1 - num2;
}
if (op2 == '+') {
return temp + num3;
} else {
return temp - num3;
}
}
功能:
此函数用于计算形如 num1 op1 num2 op2 num3 的表达式的值,同时考虑运算符的优先级。
实现细节:
①先处理 op1 为乘除的情况,算出中间结果 temp,再结合 op2 计算最终结果。
②若 op1 是加减,就先处理 op2 为乘除的情况。
③若两个运算符都是加减,就按从左到右的顺序计算。
④遇到除零错误时返回 -1。
2、main函数
2.1初始化与随机数种子设置
点击查看代码
srand(static_cast<unsigned int>(time(nullptr)));
vector<string> exercises;
vector<double> answers;
char operators[] = {'+', '-', '*', '/'};
②exercises:用于存储生成的练习题表达式。
③answers:用于存储练习题的正确答案。
④operators:存储四则运算符。
2.2生成 300 道练习题
点击查看代码
while (exercises.size() < 300) {
int num1 = rand() % 100 + 1;
int num2 = rand() % 100 + 1;
int num3 = rand() % 100 + 1;
char op1 = operators[rand() % 4];
char op2 = operators[rand() % 4];
double result = calculate(num1, op1, num2, op2, num3);
// 检查结果是否为整数且在 0 到 1000 之间
if (result >= 0 && result <= 1000 && static_cast<int>(result) == result) {
string expression = to_string(num1) + " " + string(1, op1) + " " + to_string(num2) + " " + string(1, op2) + " " + to_string(num3);
exercises.push_back(expression);
answers.push_back(result);
}
}
①借助 rand() 函数随机生成三个 1 到 100 之间的整数和两个四则运算符。
②调用 calculate 函数计算表达式的值。
③仅当结果是整数且在 0 到 1000 之间时,才将表达式和答案分别存入 exercises 和 answers 向量。
2.3循环显示练习题并获取用户答案
点击查看代码
for (size_t i = 0; i < exercises.size(); i += 10) {
cout << "以下是十道四则运算题,请依次输入答案:" << endl;
for (size_t j = 0; j < 10 && i + j < exercises.size(); ++j) {
cout << "题目 " << j + 1 << ": " << exercises[i + j] << endl;
}
vector<double> userAnswers;
for (size_t j = 0; j < 10 && i + j < exercises.size(); ++j) {
double userAnswer;
while (true) {
cout << "请输入第 " << j + 1 << " 题的答案: ";
if (cin >> userAnswer) {
// 输入有效,清除错误标志并忽略多余输入
cin.ignore(numeric_limits<streamsize>::max(), '\n');
break;
} else {
// 输入无效,清除错误标志并忽略多余输入
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "输入无效,请输入一个有效的数字。" << endl;
}
}
userAnswers.push_back(userAnswer);
}
for (size_t j = 0; j < 10 && i + j < exercises.size(); ++j) {
if (userAnswers[j] == answers[i + j]) {
cout << "第 " << j + 1 << " 题回答正确!" << endl;
} else {
cout << "第 " << j + 1 << " 题回答错误,正确答案是: " << answers[i + j] << endl;
}
}
// 等待用户按下任意键继续
cout << "按任意键继续..." << endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.get();
// 清屏操作
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
②针对每道题,会循环提示用户输入答案,同时检查输入是否有效。
③把用户输入的答案和正确答案进行比较,输出答题结果。
④等待用户按下任意键继续,然后清屏。
四、优化建议及可拓展方向
1、减少重复计算:
当前代码在生成练习题时,每次都调用 calculate 函数计算表达式的值。
2、输入提示增强:
在用户输入答案时,除了提示输入无效,还可以提供更多的输入示例,帮助用户正确输入。
3、答题统计:
记录用户的答题时间、正确率、错误率等信息,并在程序结束后显示统计结果。
4、错题记录:
将用户答错的题目记录下来,供用户后续复习。
五、总结
1、分工与互补
双方基于各自优势分工:我主导算法设计与逻辑实现(如calculate函数的优先级处理),伙伴聚焦用户交互与代码规范性(如输入验证、界面提示)。
2、沟通与决策
采用高频短会同步进度,针对分歧(如随机数生成方式、清屏跨平台实现)通过代码实验对比方案,最终选择rand()+ 时间种子生成随机数,并使用#ifdef实现跨平台清屏。
3、效率提升
相对于独自开发,结对编程完成度和bug很少。
浙公网安备 33010602011771号