diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..06513d7 --- /dev/null +++ b/src/Makefile @@ -0,0 +1,6 @@ +app: app.o data.o + gcc -o app app.o data.o +app.o: app.c data.h + gcc -c app.c +data.o: data.c data.h + gcc -c data.c \ No newline at end of file diff --git a/src/data.c b/src/data.c index d807850..489655a 100644 --- a/src/data.c +++ b/src/data.c @@ -1,2 +1,270 @@ #include -#include \ No newline at end of file +#include + + + +// 函数定义 +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("学生已存在"); + } +} + +//修改 +void update_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) + { + printf("学生不存在"); + } + else + { + printf("输入学生学号:"); + scanf("%d", &stu[num].number); + printf("输入学生姓名:"); + scanf("%s", stu[num].name); + printf("输入学生成绩:"); + scanf("%d", &stu[num].score); + printf("学生 %d 已更新", num); + } +} + +//删除 +void delete_data(void) +{ + int num; + printf("输入学生学号:"); + scanf("%d", &num); + int f = 0, i, j; + for (i = 0; i < num_parts; i++) + { + if (num == stu[i].number) + { + f = 1; + break; + } + } + if (f == 0) + { + printf("学生不存在"); + } + else + { + for (j = i; j < num_parts - 1; j++) + { + stu[j] = stu[j + 1]; + } + num_parts--; + printf("删除成功"); + } +} + +//读取 +void read_data(void) +{ + puts("读取数据"); + printf("请输入文件名"); + char filename[256]; + scanf("%s", filename); + + FILE *fp = fopen(filename, "r"); + if (fp == NULL) + { + perror(filename); + return; + } + int i = 0; + while (fscanf(fp, "%d,%[^,],%d", &stu[i].number, stu[i].name, &stu[i].score) == 3) + { + i++; + } + fclose(fp); + 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'; // 返回确认结果 +} diff --git a/src/data.h b/src/data.h index c5e364f..4e73be7 100644 --- a/src/data.h +++ b/src/data.h @@ -1,2 +1,40 @@ #define NAME_LEN 31 -#define MAX_PARTS 100 \ No newline at end of file +#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; + +//定义常量 +#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); \ No newline at end of file