|
发表于 2022-11-3 13:25:26
|
显示全部楼层
本楼为最佳答案
 - #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- using namespace std;
- struct equation{
- int a;
- int b;
- char fuhao;
- int ans;
- int answer;
- bool is_correct;
- };
- equation equations[10];
- int len = 0;
- void memory_equation(int a, int b, char fuhao, int ans, int answer, bool is_correct) {
- equation temp;
- temp.a = a; temp.b = b; temp.fuhao = fuhao; temp.ans = ans; temp.answer = answer; temp.is_correct = is_correct;
- if(len == 10) {
- for(int i=0; i<9; ++i) equations[i] = equations[i+1];
- equations[9] = temp;
- }
- else equations[len++] = temp;
- }
- void ask() {
- printf("*********\n\
- 请选择运算符:\n\
- 加法运算,请按1;\n\
- 减法运算,请按2;\n\
- 乘法运算,请按3;\n\
- 除法运算,请按4;\n\
- *****");
- int n;
- scanf("%d", &n);
- int a = rand(), b = rand(), answer, ans;
- char fuhao;
- switch(n) {
- case 1: fuhao = '+'; answer = a+b; break;
- case 2: fuhao = '-'; answer = a-b; break;
- case 3: fuhao = '*'; answer = a*b; break;
- case 4: fuhao = '/'; answer = a/b; break;
- }
- printf("%d%c%d=", a, fuhao, b);
- scanf("%d", &ans);
- if(ans == answer) printf("你很棒,加油!\n");
- else printf("很遗憾!\n");
- memory_equation(a, b, fuhao, ans, answer, ans == answer);
- }
- void print()
- {
- for(int i=0; i<len; ++i) {
- equation x = equations[i];
- printf("%d%c%d=%d", x.a, x.fuhao, x.b, x.ans);
- if(x.is_correct) printf("正确\n");
- else printf("错误\n");
- }
- }
- int main()
- {
- srand((unsigned int)time(0));
- while(true){
- ask();
- char yn;
- printf("是否要查询以前的答题情况(Y/N):");
- fflush(stdin);
- fflush(stdin);
- scanf("%c", &yn);
- if(yn == 'Y' || yn == 'y') print();
- printf("是否需要继续答题(Y/N):");
- fflush(stdin);
- scanf("%c", &yn);
- if(yn == 'N' || yn == 'n') break;
- }
- return 0;
- }
复制代码 |
|