鱼C论坛

 找回密码
 立即注册
查看: 2078|回复: 5

求大佬写一下第七题,谢谢

[复制链接]
发表于 2022-11-22 11:07:53 From FishC Mobile | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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

使用道具 举报

 楼主| 发表于 2022-11-22 11:08:49 From FishC Mobile | 显示全部楼层
大佬,尽量简化一下,谢谢了,用c语言做,真的太感谢了,求大佬解答
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-11-22 14:03:54 | 显示全部楼层
sh-5.1$ cat main.c
#include "list.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

typedef struct {
    char ch;
    int (*func)(int a, int b);
    size_t index;
} op_t;

typedef struct {
    int a, b, c;
    op_t op;
} exp_t;

int add(int a, int b) {return a + b;}
int sub(int a, int b) {return a - b;}
int mul(int a, int b) {return a * b;}
int div_(int a, int b) {return a / b;}

op_t op_table[] = {{'+', add, 0}, {'-', sub, 1}, {'*', mul, 2}, {'/', div_, 3}};

void menu(void) {
    puts("*********");
    puts("请选择运算符:");
    puts("加法运算,请按1;");
    puts("减法运算,请按2;");
    puts("乘法运算,请按3;");
    puts("除法运算,请按4;");
    puts("历史记录,请按5;");
    puts("退出程序,请按0;");
    puts("*********");
}

void history(const list_t *list) {
    for(size_t i = 0; i < list_size(list); ++i) {
        exp_t e; list_get(list, i, &e, sizeof(e));
        printf("%d%c%d=%d", e.a, e.op.ch, e.b, e.c);
        if(e.c == e.op.func(e.a, e.b)) puts("正确");
        else puts("错误");
    }
}

list_t *read_history(const char *filename) {
    list_t *list = list_init();
    FILE *fp = fopen(filename, "rb");
    if(!fp) return list;
    size_t size; fread(&size, 1, sizeof(size), fp);
    for(size_t i = 0; i < size; ++i) {
        exp_t e; fread(&e, 1, sizeof(e), fp);
        e.op = op_table[e.op.index];
        list_append(list, &e, sizeof(e));
    }
    fclose(fp);
    return list;
}

void save_history(const list_t *list, const char *filename) {
    FILE *fp = fopen(filename, "wb");
    if(!fp) return;
    size_t size = list_size(list);
    fwrite(&size, 1, sizeof(size), fp);
    for(size_t i = 0; i < size; ++i) {
        exp_t e; list_get(list, i, &e, sizeof(e));
        fwrite(&e, 1, sizeof(e), fp);
    }
    fclose(fp);
}

int main(void) {
    srand(time(NULL));
    //list_t *list = list_init();
    list_t *list = read_history("history.dat");
    while(1) {
        menu();
        int command; scanf("%d", &command);
        if(command == 0) break;
        if(command == 5) {history(list); continue;}
        if(!(command >= 1 && command <= 4)) {puts("输入错误,请重新输入"); continue;}
        exp_t e = {.a = rand() % 99 + 1, .b = rand() % 99 + 1, .op = op_table[command - 1]};
        printf("%d%c%d=", e.a, e.op.ch, e.b);
        scanf("%d", &e.c);
        if(e.c == e.op.func(e.a, e.b)) puts("你很棒,加油!");
        else puts("很遗憾!");
        list_append(list, &e, sizeof(e));
        //if(list_size(list) > 10) list_delete(list, 0);
repeat: printf("是否继续答题?(Y/n) ");
        char q; getchar(); scanf("%c", &q);
        if(q == 'n' || q == 'N') break;
        if(!(q == 'y' || q == 'Y')) {puts("输入错误,请重新输入"); goto repeat;}
    }
    save_history(list, "history.dat");
    list_deinit(list);
    return 0;
}
sh-5.1$ cat list.h
#ifndef _LIST_H_
#define _LIST_H_

#include <stddef.h>
#include <stdbool.h>

struct list_node_tag {
    void *data; size_t size;
    struct list_node_tag *next;
};

typedef struct {
    struct list_node_tag *head;
    size_t size;
} list_t;

list_t *list_init(void);
void list_deinit(list_t *list);
bool list_clean(list_t *list);
bool list_insert(list_t *list, size_t index, const void *data, size_t size);
bool list_delete(list_t *list, size_t index);
bool list_get(const list_t *list, size_t index, void *data, size_t size);
bool list_set(list_t *list, size_t index, const void *data, size_t size);
bool list_append(list_t *list, const void *data, size_t size);
size_t list_size(const list_t *list);
bool list_empty(const list_t *list);

#endif
sh-5.1$ cat list.c
#include "list.h"
#include <stdlib.h>
#include <memory.h>

list_t *list_init(void) {
    list_t *list = malloc(sizeof(*list));
    if(!list) return NULL;
    list->head = NULL;
    list->size = 0;
    return list;
}

void list_deinit(list_t *list) {
    if(!list) return;
    list_clean(list);
    free(list);
}

bool list_clean(list_t *list) {
    if(!list) return false;
    while(!list_empty(list)) if(!list_delete(list, 0)) return false;
    return true;
}

bool list_insert(list_t *list, size_t index, const void *data, size_t size) {
    if(!list) return false;
    if(list_size(list) < index) return false;
    if(!data) return false;
    struct list_node_tag **current = &list->head;
    while(index--) current = &(*current)->next;
    struct list_node_tag *node = malloc(sizeof(*node));
    if(!node) return false;
    node->data = malloc(size);
    if(!node->data) {free(node); return false;}
    memcpy(node->data, data, size);
    node->size = size;
    node->next = *current;
    *current = node;
    ++list->size;
    return true;
}

bool list_delete(list_t *list, size_t index) {
    if(!list) return false;
    if(list_size(list) <= index) return false;
    struct list_node_tag **current = &list->head;
    while(index--) current = &(*current)->next;
    struct list_node_tag *temp = *current;
    *current = temp->next;
    free(temp->data); free(temp);
    --list->size;
    return true;
}

bool list_get(const list_t *list, size_t index, void *data, size_t size) {
    if(!list) return false;
    if(list_size(list) <= index) return false;
    if(!data) return false;
    struct list_node_tag *const *current = &list->head;
    while(index--) current = &(*current)->next;
    struct list_node_tag *temp = *current;
    if(temp->size > size) return false;
    memcpy(data, temp->data, temp->size);
    return true;
}

bool list_set(list_t *list, size_t index, const void *data, size_t size) {
    bool res = list_delete(list, index);
    return res ? list_insert(list, index, data, size) : res;
}

bool list_append(list_t *list, const void *data, size_t size) {
    if(!list) return false;
    return list_insert(list, list_size(list), data, size);
}

size_t list_size(const list_t *list) {
    if(!list) return 0;
    return list->size;
}

bool list_empty(const list_t *list) {
    if(!list) return true;
    return list_size(list) == 0;
}
sh-5.1$
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-22 17:03:38 From FishC Mobile | 显示全部楼层
大佬能不能写得再简单些啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-11-22 17:03:53 From FishC Mobile | 显示全部楼层
谢谢
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-11-22 17:06:53 | 显示全部楼层
啊啊啊空空 发表于 2022-11-22 17:03
大佬能不能写得再简单些啊

这代码哪里复杂了,你哪里看不懂?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-20 18:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表