• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
kaminoyagi
博客园    首页    新随笔    联系   管理    订阅  订阅

结对编程 小学四则运算

程序代码

#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

posted @ 2024-04-25 01:22  Kaminoyagi  阅读(16)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3