一、实验要求
二柱子将做好的项目交给了老师,几天后又得到新的需求:
1、实现在线答题
2、答题结束后,可以判断对错
3、并将错题的结果保存起来
二、设计思路
让程序执行五个随机生成的加减乘除算式,把自己的计算结果与标准答案比较,错误则输出错误处及错题数,并可在错题本查阅错题
三、源程序代码
#include <stdio.h> #include <stdlib.h> #include <time.h> int main() { FILE *fp; int answer[5]; int myanswer[5]; int x, y, num, z, num1, num2, scc; srand(time(0)); char sc[] = {'+', '-', '*', '/'}; char cnt = 0; char c; int str[5][2]; printf("欢迎使用二柱子答题系统:\n\n"); for(x=0; x<5; x++) { z = 1; for(y=0; y<z; y++) { num1 = rand() % 10 + 1; } num2 = rand() % 10 + 1; scc = rand() % 4; if(scc == 3 && num1 % num2 != 0) { x--; continue; } str[x][0] = num1; str[x][1] = (int)sc[scc]; str[x][2] = num2; if(scc == 0) answer[cnt++] = num1 + num2; if(scc == 1) answer[cnt++] = num1 - num2; if(scc == 2) answer[cnt++] = num1 * num2; if(scc == 3) answer[cnt++] = num1 / num2; printf("%d %c %d =\n", num1, sc[scc], num2); } printf("\n\n请输入你的答案:"); cnt = 0; fp = fopen("错题本", "a"); scanf("%d%d%d%d%d", &myanswer[0], &myanswer[1], &myanswer[2], &myanswer[3], &myanswer[4]); for(x=0; x<5; x++) { if(answer[x] != myanswer[x]) { cnt++; fprintf(fp, "%d %c %d = %d\n", str[x][0], (char)str[x][1], str[x][2], myanswer[x]); printf("%d %c %d = %d\n", str[x][0], (char)str[x][1], str[x][2], myanswer[x]); } } fclose(fp); if(cnt > 0) printf("做错了%d道题,请查阅错题本及时复习", cnt); system("pause"); return 0; }
四、运行结果截图