本次结对编程作业由我和2152701一起完成
1.源代码
#include <cstdio> #include <cstdlib> #include <time.h> using namespace std; char operation(int operation); float createEquation(); float calculate(int number1, int number2, int operation); const int plus = 1, subtract = 2, multiplication = 3, division = 4; int main () { int num, i; printf("欢迎使用四则运算系统(专为小学生服务)\n1.请输入题目个数:"); scanf("%d", &num); for (i = 0; i < num; i++) { createEquation(); } } float createEquation() { int number1, number2, number3, operation1, operation2; float input_answer, answer = 0; do { srand(time(0)); number1 = rand()%100+1; number2 = rand()%100+1; number3 = rand()%100+1; operation1 = rand()%4+1; operation2 = rand()%4+1; if(operation1 == multiplication || operation1 == division) { answer = calculate(calculate(number1, number2, operation1), number3, operation2 ); } else { if(operation2 == multiplication || operation2 == division) { answer = calculate(number1 ,calculate(number2, number3, operation1), operation2 ); } else { answer = calculate(calculate(number1, number2, operation1), number3, operation2 ); } } }while(answer < 0||answer>100); printf("%d %c %d %c %d =",number1, operation(operation1),number2, operation(operation2), number3); scanf("%f", &input_answer); if(answer == input_answer) { printf("答案正确"); } else { printf("答案错误,正确答案是%f", answer); } printf("\n"); } char operation(int operation) { char c; switch(operation) { case plus: c = '+'; break; case subtract: c = '-'; break; case multiplication: c = '*'; break; case division: c = '/'; break; } return c; } float calculate(int number1, int number2, int operation) { switch(operation) { case plus: return (float)number1 + number2; case subtract: return (float)number1 - number2; case multiplication: return (float)number1 * number2; case division: return (float)number1 / number2; } }
2.运行结果
3.心得体会
这次结对编程四则运算题目我们利用c++进行开发。在本次代码编写的过程中,最重要的两个问题是随机数字符号的生成以及优先级的确定,在第一次编写代码的时候我们遇到了一个问题——算术结果不正确。经过计算和研究,我们发现是因为优先级不正确导致运算结果出错误,因此在进行二次改写的时候,我们进行了改正。除此以外,在代码的编写过程中,我们两个人也出现了一些分歧,例如switch语句的举例,但是最终我们在商量下共同完成了本次作业,这让我们意识到了合作的重要性以及团队精神的重要性,一个好的项目必然离不开队友之间的讨论与相互帮助。
标签:结对,float,int,编程,number2,number1,answer,operation From: https://www.cnblogs.com/2115414hkx/p/17299583.html