啊啊啊空空 发表于 2022-11-3 12:42:06

求大佬用c语言的数组做一下,教教我,谢谢

题目二:先做一菜单,菜单格式如下:
*********
请选择运算符:
加法运算,请按1;
减法运算,请按2;
乘法运算,请按3;
除法运算,请按4;
*****
程序要求:先显示菜单,客户选择1-4中任意一个数字,就做相应的计算。然后要求客户输入两个数,运算出结果。比如客户选择了...
在题目二的基础上进行改进。程序要求:先显示菜单,客户选择了数字,就做相应的计算。然后系统随机产生两个整数,列出式子,要求客户写出答案,最后系统判断客户答题是否正确,正确显示“你很棒,加油!”,错误显示“很遗憾!”。比如客户选择了2,系统就随机产生一个式子(例23-12=),然后要求客户做减法。                                                    在题目三的基础上进行改进。客户做完一题后,询问客户是否继续答题(继续请输入Y,退出请输入N)。如客户选择继续答题,就重复显题目四,否则退出。
即用户可以循环做答,直到其输入N.
在题目四的基础上进行改进。客户做完多道题目后,可以查询最近十道题的答题情况(如没有做到十道题,就显示全部的题目)。查询显示结果的格式如下:
2*3=6正确
2*4=7错误
提示:要用到数组

啊啊啊空空 发表于 2022-11-3 12:43:07

要用到c语言的数组,尽量简化,跪求大佬写一下,教教我

tommyyu 发表于 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;
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 = equations;
                equations = temp;
        }
        else equations = 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;
                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;
}

啊啊啊空空 发表于 2022-11-3 17:21:27

求大佬联系方式,教教小弟怎么学编程

啊啊啊空空 发表于 2022-11-3 20:04:42

大佬,怎么控制随机数在100以内啊

tommyyu 发表于 2022-11-4 07:26:26

本帖最后由 tommyyu 于 2022-11-4 07:28 编辑

啊啊啊空空 发表于 2022-11-3 20:04
大佬,怎么控制随机数在100以内啊

把第34行改为int a = rand()%100, b = rand()%100, answer, ans;

tommyyu 发表于 2022-11-4 07:27:04

啊啊啊空空 发表于 2022-11-3 17:21
求大佬联系方式,教教小弟怎么学编程

我编程能力也不行,如果你有问题可以发到这个论坛里,一般都会有人解答的

啊啊啊空空 发表于 2022-11-4 17:24:48

大佬,这个还是不能完成查看做过的题啊,会出错

tommyyu 发表于 2022-11-5 07:53:11

啊啊啊空空 发表于 2022-11-4 17:24
大佬,这个还是不能完成查看做过的题啊,会出错

我改一下{:10_324:}

tommyyu 发表于 2022-11-5 07:56:27

啊啊啊空空 发表于 2022-11-4 17:24
大佬,这个还是不能完成查看做过的题啊,会出错

我这里运行没有什么问题,你能把你出问题的图片发一下么

啊啊啊空空 发表于 2022-11-5 17:06:20

tommyyu 发表于 2022-11-5 07:56
我这里运行没有什么问题,你能把你出问题的图片发一下么

点Y或者N他不能继续运行,会跳出来,显示错误

啊啊啊空空 发表于 2022-11-5 17:17:19

tommyyu 发表于 2022-11-4 07:26
把第34行改为

不行啊,加上之后会出现更多错误了

啊啊啊空空 发表于 2022-11-5 17:18:12

我用的是vs2022

tommyyu 发表于 2022-11-5 17:19:09

啊啊啊空空 发表于 2022-11-5 17:18
我用的是vs2022

能发一下截图么

tommyyu 发表于 2022-11-5 17:21:49

我这里没有什么问题
*********
请选择运算符:
加法运算,请按1;
减法运算,请按2;
乘法运算,请按3;
除法运算,请按4;
*****1
70+28=98
你很棒,加油!
是否要查询以前的答题情况(Y/N):Y
70+28=98正确
是否需要继续答题(Y/N):Y
*********
请选择运算符:
加法运算,请按1;
减法运算,请按2;
乘法运算,请按3;
除法运算,请按4;
*****

啊啊啊空空 发表于 2022-11-5 17:23:09

啊啊啊空空 发表于 2022-11-5 17:06
点Y或者N他不能继续运行,会跳出来,显示错误

拍不了啊。点Y或者N之后会跳出来,有一个小长方形,左上角会写:已引发异常,显示调用堆栈等

tommyyu 发表于 2022-11-5 17:27:26

啊啊啊空空 发表于 2022-11-5 17:23
拍不了啊。点Y或者N之后会跳出来,有一个小长方形,左上角会写:已引发异常,显示调用堆栈等

好像是 vscode 不支持 fflush 函数,我改一下

人造人 发表于 2022-11-5 17:27:26

tommyyu 发表于 2022-11-3 13:25


fflush(stdin);

In all other cases, the behavior depends on the specific library implementation. In some implementations, flushing a stream open for reading causes its input buffer to be cleared (but this is not portable expected behavior).

https://cplusplus.com/reference/cstdio/fflush/

tommyyu 发表于 2022-11-5 17:28:12

你试试这个
#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;
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 = equations;
                equations = temp;
      }
      else equations = temp;
}
void ask() {
      printf("*********\n\
请选择运算符:\n\
加法运算,请按1;\n\
减法运算,请按2;\n\
乘法运算,请按3;\n\
除法运算,请按4;\n\
*****");
      int n;
      scanf("%d", &n);
      int a = rand()%100, b = rand()%100, 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;
                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):");
                getchar();
                scanf("%c", &yn);
                if(yn == 'Y' || yn == 'y') print();
                printf("是否需要继续答题(Y/N):");
                getchar();
                scanf("%c", &yn);
                if(yn == 'N' || yn == 'n') break;
      }
      return 0;
}

啊啊啊空空 发表于 2022-11-5 17:58:21

tommyyu 发表于 2022-11-5 17:28
你试试这个

大佬,可以了,真心求一个联系方式
页: [1]
查看完整版本: 求大佬用c语言的数组做一下,教教我,谢谢