From ff1876e40035d6e61a8cbdb52cdef81ac3b4a7b8 Mon Sep 17 00:00:00 2001 From: p3crvf5xo <1414005050@qq.com> Date: Fri, 28 Apr 2023 14:11:43 +0800 Subject: [PATCH 1/4] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index f89a337..33af98c 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,6 @@ | ------ | ------ | ------ | ------ | | 25 | 25 | 25 | 25 | - >>>>>>> >>>>>> >>>>> From 5ffaa7c70fb931cd475884c9774a512f0969046b Mon Sep 17 00:00:00 2001 From: wangkaixuan <@ bzu.edu.cn> Date: Fri, 28 Apr 2023 14:16:04 +0800 Subject: [PATCH 2/4] 1 --- daima | 264 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 264 insertions(+) diff --git a/daima b/daima index 8f337db..6538439 100644 --- a/daima +++ b/daima @@ -1,4 +1,184 @@ +#define NAME_LEN 31 +#define MAX_PARTS 100 +//保存学生成绩信息的结构 +struct students +{ + int number; + char name[NAME_LEN + 1]; + int score; +}; +static struct students stu[MAX_PARTS]; +static int num_parts = 0; +#include +#include + +//定义常量 +#define CMD_QUIT 0 +#define CMD_QUERY 4 +#define CMD_INSERT 5 +#define CMD_UPDATE 6 +#define CMD_DELETE 7 +#define CMD_READ 1 +#define CMD_SAVE 2 +#define CMD_PRINT 3 +#define CMD_SORT 8 +#define CMD_CHART 9 +// 菜单命令的编号 + +// 函数声明 +void init(void); +void quit(void); +void display_menu(void); +int make_choice(void); +void read_data(void); // 读取 +void save_data(void); // 保存 +void print_data(void); // 打印 +void query_data(void); // 查询 +void add_data(void); // 添加 +void update_data(void); // 修改 +void delete_data(void); // 删除 +void sort_data(void); // 排序 +void make_chart(void); // 图表 +int confirm(const char *msg); + +// 主程序 +int main(void) +{ + init(); // 程序启动 + while (1) + { + display_menu(); + int c = make_choice(); + switch (c) + { + case CMD_QUERY: + query_data(); + break; + case CMD_INSERT: + add_data(); + break; + case CMD_UPDATE: + update_data(); + break; + case CMD_DELETE: + delete_data(); + break; + case CMD_READ: + read_data(); + break; + case CMD_SAVE: + save_data(); + break; + case CMD_PRINT: + print_data(); + break; + case CMD_SORT: + sort_data(); + break; + case CMD_CHART: + make_chart(); + break; + default: + puts("命令错误,请重新选择"); + break; + case CMD_QUIT: + if (confirm("确定要退出吗?")) + quit(); // 仅在确认后退出 + break; + } + } + return 0; +} + +// 函数定义 +void init(void) +{ + puts("程序启动"); +} +void quit(void) +{ + puts("程序退出"); + exit(EXIT_SUCCESS); } +void display_menu(void) +{ + printf("\n%d 读取 | %d 保存 | %d 打印 | %d 查询 | %d 添加\n%d 修改 | %d 删除 | %d 排序 | %d 图表 | %d 退出\n\n", CMD_READ, CMD_SAVE, CMD_PRINT, CMD_QUERY, CMD_INSERT, CMD_UPDATE, CMD_DELETE, CMD_SORT, CMD_CHART, CMD_QUIT); +} +int make_choice(void) +{ + int c; // 用户输入 + int n = 0; // 正确读入的数据项个数 + while (n == 0) + { + printf("请选择:"); + n = scanf("%d", &c); // 尝试读入整数 c + scanf("%*[^\n]"); // 跳过一行中剩余的字符 + } + return c; +} + +//查询 +void query_data(void) +{ + int num; + printf("输入学生学号: "); + scanf("%d", &num); + int f = -1; + for (int i = 0; i < num_parts; i++) + { + if (num == stu[i].number) + { + f = i; + break; + } + } + if (f == -1) + { + printf("学生不存在"); + } + else + { + printf("学生学号:%d\n", stu[f].number); + printf("学生姓名:%s\n", stu[f].name); + printf("学生成绩:%d\n", stu[f].score); + } +} + +//打印 +void print_data(void) +{ + printf("NUMBER | NAME score\n"); + for (int i = 0; i < num_parts; i++) + { + printf("%d | %s %d\n", stu[i].number, stu[i].name, stu[i].score); + } +} + +//添加 +void add_data(void) +{ + int num; + printf("输入学生学号:"); + scanf("%d", &num); + int f = 0; + for (int i = 0; i < num_parts; i++) + { + if (num == stu[i].number) + { + f = 1; + break; + } + } + if (f == 0) + { + stu[num_parts].number = num; + printf("输入学生姓名:"); + scanf("%s", &stu[num_parts].name); + printf("输入学生成绩:"); + scanf("%d", &stu[num_parts].score); + num_parts++; + printf("学生 %d 添加成功\n", num); + } else { printf("学生已存在"); @@ -88,3 +268,87 @@ void read_data(void) num_parts = i; printf("读取 %d 记录\n", num_parts); } + +void save_data(void) +{ + puts("保存数据"); + printf(" 请输入文件名"); + char filename[256]; + scanf("%s", filename); + FILE *fp = fopen(filename, "r"); + if (fp == NULL) + { + perror(filename); + return; + } + for (int i = 0; i < num_parts; i++) + { + fprintf(fp, "%d,%s,%d\n", stu[i].number, stu[i].name, stu[i].score); + } + fclose(fp); + printf("保存%d 成功", num_parts); +} + +//排序 +void sort_data(void) +{ + printf("排序\n"); + for (int i = 0; i < num_parts; i++) + { + int change = 0; + static struct students t; + for (int j = 0; j < num_parts - i - 1; j++) + { + if (stu[j].number > stu[j + 1].number) + { + t = stu[j + 1]; + stu[j + 1] = stu[j]; + stu[j] = t; + change = 1; + } + } + if (!change) + break; + } + for (int i = 0; i < num_parts; i++) + { + int change = 0; + static struct students t; + for (int j = 0; j < num_parts - i - 1; j++) + { + if (stu[j].score > stu[j + 1].score) + { + t = stu[j + 1]; + stu[j + 1] = stu[j]; + stu[j] = t; + change = 1; + } + } + if (!change) + break; + } + printf("排序已完成"); +} + +void make_chart(void) +{ + printf("NUMBER | NAME score\n"); + for (int i = 0; i < num_parts; i++) + { + printf("%d | %s ", stu[i].number, stu[i].name); + for (int j = 0; j < stu[i].score; j++) + { + printf("*"); + } + printf("\n"); + } + printf("图表\n"); +} + +int confirm(const char *msg) +{ + char c = 'n'; // 默认选择是 no + printf("%s(Y/N): ", msg); // 提示输入 yes/no 进行确认 + scanf(" %c%*[^\n]", &c); // 读取第一个字符,忽略剩余字符 + return c == 'y' || c == 'Y'; // 返回确认结果 +} \ No newline at end of file From eb1e1593492f8d027ce38daeb7774938c5f20271 Mon Sep 17 00:00:00 2001 From: wangkaixuan <@ bzu.edu.cn> Date: Fri, 28 Apr 2023 14:16:26 +0800 Subject: [PATCH 3/4] 1 --- daima.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 daima.c diff --git a/daima.c b/daima.c new file mode 100644 index 0000000..bcac5c1 --- /dev/null +++ b/daima.c @@ -0,0 +1,83 @@ +void save_data(void) +{ + puts("保存数据"); + printf(" 请输入文件名"); + char filename[256]; + scanf("%s", filename); + FILE *fp = fopen(filename, "r"); + if (fp == NULL) + { + perror(filename); + return; + } + for (int i = 0; i < num_parts; i++) + { + fprintf(fp, "%d,%s,%d\n", stu[i].number, stu[i].name, stu[i].score); + } + fclose(fp); + printf("保存%d 成功", num_parts); +} + +//排序 +void sort_data(void) +{ + printf("排序\n"); + for (int i = 0; i < num_parts; i++) + { + int change = 0; + static struct students t; + for (int j = 0; j < num_parts - i - 1; j++) + { + if (stu[j].number > stu[j + 1].number) + { + t = stu[j + 1]; + stu[j + 1] = stu[j]; + stu[j] = t; + change = 1; + } + } + if (!change) + break; + } + for (int i = 0; i < num_parts; i++) + { + int change = 0; + static struct students t; + for (int j = 0; j < num_parts - i - 1; j++) + { + if (stu[j].score > stu[j + 1].score) + { + t = stu[j + 1]; + stu[j + 1] = stu[j]; + stu[j] = t; + change = 1; + } + } + if (!change) + break; + } + printf("排序已完成"); +} + +void make_chart(void) +{ + printf("NUMBER | NAME score\n"); + for (int i = 0; i < num_parts; i++) + { + printf("%d | %s ", stu[i].number, stu[i].name); + for (int j = 0; j < stu[i].score; j++) + { + printf("*"); + } + printf("\n"); + } + printf("图表\n"); +} + +int confirm(const char *msg) +{ + char c = 'n'; // 默认选择是 no + printf("%s(Y/N): ", msg); // 提示输入 yes/no 进行确认 + scanf(" %c%*[^\n]", &c); // 读取第一个字符,忽略剩余字符 + return c == 'y' || c == 'Y'; // 返回确认结果 +} \ No newline at end of file From ad11bacb9e5e62e3d66d89b598e042d9c52b72df Mon Sep 17 00:00:00 2001 From: p3crvf5xo <1414005050@qq.com> Date: Fri, 28 Apr 2023 14:23:45 +0800 Subject: [PATCH 4/4] Update README.md --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index 33af98c..bf7a5da 100644 --- a/README.md +++ b/README.md @@ -36,7 +36,6 @@ >>>> >>> >> - ## 关于零件库存管理系统 设计一个库存零件管理系统,要求采用命令行菜单界面进行交互,具备读取、保存、打印、查询、修改、插入、删除和排序等基本功能,能够以表格和图表形式展示数据,采用 CSV 格式保存数据。