结对编程 小学四则运算
程序代码
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<map>
#include<stack>
using namespace std;
int check(int s1, int s2, int s3, char c1, char c2) {
int num1;
int num2;
if (c2 == '*' || c2 == '/') {
if (c2 == '*')num2 = s2 * s3;
else if (c2 == '/') num2 = s2 / s3;
if (c1 == '+') num1 = s1 + num2;
else if (c1 == '-')num1 = s1 - num2;
}
else {
if (c1 == '+') num1 = s1 + s2;
else if (c1 == '-')num1 = s1 - s2;
if (c2 == '+') num1 += s3;
else if (c2 == '-')num1 -= s3;
}
if (num1 < 0 || num1>1000)return -1;
return num1;
}
int main() {
vector<char>ans = { '+','-','*','/' };
int n = 6;
while (n > 0)
{
for (int i = 0; i < 50; i++) {
int s1 = rand() % 100;
int s2 = rand() % 10 + 1;
int s3 = rand() % 100 + 1;
s2 = s2 * s3;
int rans1 = rand() % 2;
int rans2 = rand() % 4;
int res = check(s1, s2, s3, ans[rans1], ans[rans2]);
if (res == -1) {
i--;
continue;
}
cout << s1 << ans[rans1] << s2 << ans[rans2] << s3 << "=";
int result;
cin >> result;
if (result == res)cout << "yes" << endl;
else cout << "no" << endl;
}
cout << "已经完成50道题了 休息一下吧~" << endl;
n--;
cout << "是否继续?1:继续 0:退出" << endl;
int value = 0;
cin >> value;
if (value != 1)
{
break;
}
}
return 0;
}
运算结果
50题做完后可选择重新运行程序或退出
判断对错
博客体会
结对编程给我带来一种全新的心得体会,以前一直认为编程偏向于自我的思考,但结对编程过程中,和队友的交流也尤为重要。两个人同时对一个程序进行编写和修改,让我们的代码编写更加顺利,也犯了更少的错误。
结对队友:2252225