题目要求:
1.学生写的程序必须能判定用户的输入答案是否正确
2.程序必须能处理四种运算的混合算式
3.要求两人合作分析,单独编程,单独撰写博客
组员:朱子嘉 胡浩特(http://www.cnblogs.com/JYQ-hu/)
//四则运算3 //朱子嘉、胡浩特 2015/3/17 #include<iostream> using namespace std; #include<stdlib.h> #include<time.h> #include<math.h> int display(int number, int mul, int num, int neg, int remainder)//打印方式控制输出列数 { int *a = new int[number]; int *b = new int[number]; int *c = new int[number]; int *d = new int[number]; int *e = new int[number]; int m;//控制题目避免重复 int t;//中间变量 int n1 = 0;//保存正确题目数量 int n2 = 0;//保存错误题目数量 double result;//用户输入的题目结果 for (int i = 0; i<number; i++)//随机生成运算操作数 { m = 1;//初始化 a[i] = rand() % num; b[i] = rand() % num; d[i] = rand() % num; e[i] = rand() % num; if (mul == 0)//没有乘除法 { c[i] = rand() % 2;//随机生成0-1的数字,分别表示加减 } else if (mul == 1)//有乘除法 { c[i] =rand() % 8;//随机生成0-7的数字,分别表示不同运算符号组合 } for (int j = 0; j<i; j++) { if (a[j] == a[i] && b[j] == b[i] && c[j] == c[i] && d[j] == d[i] && e[j] == e[i])//比较新生成的操作数与原来的是否相同 { i = i - 1; m = 0; } } while (m)//若不同则输出 { switch (c[i]) { case 0: cout << a[i] << "+" << b[i] << "=" << endl; cout << "请输入结果:" << endl; cin >> result; if (result == a[i] + b[i]) { cout << "回答正确!" << endl; n1++; } else { cout << "回答错误!" << endl; cout << "正确结果为:" << a[i] + b[i] << endl; n2++; } break; case 1: if (neg == 0)//减法没有负数 { if (a[i]<b[i]) { t = a[i]; a[i] = b[i]; b[i] = t; } cout << a[i] << "-" << b[i] << "=" << endl; cout << "请输入结果:" << endl; cin >> result; if (result == a[i] - b[i]) { cout << "回答正确!" << endl; n1++; } else { cout << "回答错误!" << endl; cout << "正确结果为:" << a[i] - b[i] << endl; n2++; } break; } else if (neg == 1)//减法有负数 { cout << a[i] << "-" << b[i] << "=" << endl; cout << "请输入结果:" << endl; cin >> result; if (result == a[i] - b[i]) { cout << "回答正确!" << endl; n1++; } else { cout << "回答错误!" << endl; cout << "正确结果为:" << a[i] - b[i] << endl; n2++; } break; } case 2: cout << a[i] << "+" << b[i] << "*" << d[i] << "-" << e[i] << "=" << endl; cout << "请输入结果:" << endl; cin >> result; if (result == a[i] + b[i] * d[i] - e[i]) { cout << "回答正确!" << endl; n1++; } else { cout << "回答错误!" << endl; cout << "正确结果为:" << a[i] + b[i] * d[i] - e[i] << endl; n2++; } break; case 3: cout << a[i] << "+" << b[i] << "-" << d[i] << "-" << e[i] << "=" << endl; cout << "请输入结果:" << endl; cin >> result; if (result == a[i] + b[i] - d[i] - e[i]) { cout << "回答正确!" << endl; n1++; } else { cout << "回答错误!" << endl; cout << "正确结果为:" << a[i] + b[i] - d[i] - e[i] << endl; n2++; } break; case 4: cout << a[i] << "+" << b[i] << "+" << d[i] << "-" << e[i] << "=" << endl; cout << "请输入结果:" << endl; cin >> result; if (result == a[i] + b[i] + d[i] - e[i]) { cout << "回答正确!" << endl; n1++; } else { cout << "回答错误!" << endl; cout << "正确结果为:" << a[i] + b[i] + d[i] - e[i] << endl; n2++; } break; case 5: if (neg == 0)//结果没有负数 { if (a[i]<b[i]) { t = a[i]; a[i] = b[i]; b[i] = t; } cout << a[i] << "-" << b[i] << "*" << c[i] << "+" << d[i] << "/" << e[i] << "=" << endl; cout << "请输入结果:" << endl; cin >> result; if (result == a[i] - b[i] * c[i] + d[i] / e[i]) { cout << "回答正确!" << endl; n1++; } else { cout << "回答错误!" << endl; cout << "正确结果为:" << a[i] - b[i] * c[i] + d[i] / e[i] << endl; n2++; } break; } else if (neg == 1)//结果有负数 { cout << a[i] << "-" << b[i] << "*" << c[i] << "+" << d[i] << "/" << e[i] << "=" << endl; cout << "请输入结果:" << endl; cin >> result; if (result == a[i] - b[i] * c[i] + d[i] / e[i]) { cout << "回答正确!" << endl; n1++; } else { cout << "回答错误!" << endl; cout << "正确结果为:" << a[i] - b[i] * c[i] + d[i] / e[i] << endl; n2++; } break; } case 6: cout << a[i] << "*" << b[i] << "*" << d[i] << "/" << e[i] << "=" << endl; cout << "请输入结果:" << endl; cin >> result; if (result == a[i] * b[i] * d[i] / e[i]) { cout << "回答正确!" << endl; n1++; } else { cout << "回答错误!" << endl; cout << "正确结果为:" << a[i] * b[i] * d[i] / e[i] << endl; n2++; } break; case 7: if (b[i] == 0)//分母为零则不计入总数 { i = i - 1; break; } else if (remainder == 0)//除法没有余数 { if (a[i] % b[i] == 0) { cout << a[i] << "/" << b[i] << "=" << endl; cout << "请输入结果:" << endl; cin >> result; if (result == a[i] / b[i]) { cout << "回答正确!" << endl; n1++; } else { cout << "回答错误!" << endl; cout << "正确结果为:" << a[i] / b[i] << endl; n2++; } break; } else { i = i - 1; break; } } else if (remainder == 1)//除法有余数 { if (a[i] % b[i] != 0) { cout << a[i] << "/" << b[i] << "=" << endl; cout << "请输入结果:" << endl; cin >> result; if (result == (double)a[i] / b[i]) { cout << "回答正确!" << endl; n1++; } else { cout << "回答错误!" << endl; cout << "正确结果为:" << (double)a[i] / b[i] << endl; n2++; } break; } else { i = i - 1; break; } } } break;//跳出循环 } } cout << "您一共答对" << n1 << "道题目" << endl; cout << "您一共答错" << n2 << "道题目" << endl; return 0; } void main() { int number;//题目数量 int mul;//乘除法 int num;//数值范围 int neg;//负数 int remainder;//余数 int c;//循环变量 srand((unsigned)time(NULL));//调用随机函数发生器 cout << "---------------------------------" << endl; cout << "| 四则运算 |" << endl; cout << "---------------------------------" << endl; cout << "请输入要打印的题目数量:" << endl; cin >> number; while (number<0) { cout << "输入错误!" << endl; cout << "请输入要打印的题目数量:" << endl; cin >> number; } cout << "是否有乘除法(0表示没有;1表示有)" << endl; cin >> mul; while (mul != 0 && mul != 1) { cout << "输入错误,请重新输入!是否有乘除法(0表示没有;1表示有)" << endl; cin >> mul; } if (mul == 1) { cout << "除法有无余数(0表示没有;1表示有)" << endl; cin >> remainder; while (remainder != 0 && remainder != 1) { cout << "输入错误,请重新输入!除法有无余数(0表示没有;1表示有)" << endl; cin >> remainder; } } cout << "请输入正整数的数值范围(即最大数):" << endl; cin >> num; cout << "减法有无负数(0表示没有;1表示有)" << endl; cin >> neg; while (neg != 0 && neg != 1) { cout << "输入错误,请重新输入!" << endl; cout << "减法有无负数(0表示没有;1表示有)" << endl; cin >> neg; } display(number, mul, num, neg, remainder); cout << "继续请输入1,退出请输入0" << endl; cin >> c; }
设计思路:
本次实验的主要功是:能够判定用户的输入答案是否正确和能够处理四种运算的混合运算。这两个基本要求基本都可以实现,虽然实现的方法比较笨重,就是各自分别计算结构,然后再判断是否正确。这次实验没有实现括号的功能,本想运用递归的思想,把两个操作数的运算看成一个操作数,然后再参与运算,直到满足功能需求,但是这个想法没有实现,还有一个问题就是:括号问题,不知道怎么用合适的方法把括号添加到运算式中,最后也没有想到比较合适的方法。总之,这次实验还是很费力的,花费的时间也很长,但是自己也没有比较好的完成。
工作照片:
周活动总结表:
时间记录日志:
缺陷记录日志: