这代码写完,我差不多就已经不认识了。
题目:写一个图书管理程序,要求可以增加记录,读取记录,删除记录,修改记录。我从11点半写到1点半,然后越写越觉得这代码写的可真蠢。
程序代码:
#include <stdio.h> #include <stdlib.h> typedef struct { char title[50]; char author[50]; float value; int del_tag; }BOOKS; void menu(void); void f_read(void); void data_add(void); void data_delete(void); FILE * f_open(void); void f_write(BOOKS a[], int arrays); long int datas = 0; int main(void) { typedef void(*P_FUN)(void); P_FUN fun_M[3] = { f_read,data_delete,data_add }; int option = 0; FILE *p_file; p_file = f_open(); fclose(p_file); while (menu(), scanf("%d", &option) == 1) { if (option >= 3) printf("该选项不存在\n"); else if (option < 0) printf("该选项不存在\n"); else if (!datas && option != 2) printf("文件中没有记录。\n"); else fun_M[option](); } return 0; } void menu(void) { printf("0) 读取文件\n"); printf("1) 删除/修改记录\n"); printf("2) 增加记录\n"); printf("q) 退出\n"); } void f_read(void) { FILE * p_file; long int data_in = 0; int st_size = sizeof(BOOKS); BOOKS book; p_file = f_open(); for (data_in = 0; data_in < datas; data_in += st_size) { fseek(p_file, data_in, SEEK_SET); fread(&book, sizeof(BOOKS), 1, p_file); printf("%-25s %s ¥%.2f\n", book.title, book.author, book.value); } fseek(p_file, 0, SEEK_END); datas = ftell(p_file); fclose(p_file); } void data_add(void) { BOOKS a; FILE * p_file; p_file = f_open(); while (getchar() != '\n'); while (printf("输入书名(空行结束):"), gets(a.title) != NULL && a.title[0] != '\0') { while (printf("输入作者:"), gets(a.author) == NULL || a.author[0] == '\0'); printf("输入单价:"); scanf("%f", &a.value); a.del_tag = 1; while (getchar() != '\n'); fwrite(&a, sizeof(BOOKS), 1, p_file); } fseek(p_file, 0, SEEK_END); datas = ftell(p_file); fclose(p_file); } void data_delete(void) { int arrays = datas / sizeof(BOOKS); BOOKS a[arrays]; int n, array_in; FILE * p_file; p_file = f_open(); for (n = 0, array_in = 0; n < datas && array_in < arrays; array_in++, n += sizeof(BOOKS)) { fseek(p_file, n, SEEK_SET); fread(&a[array_in], sizeof(BOOKS), 1, p_file); } fclose(p_file); while (printf("0.删除\n1.修改\n2.返回主菜单\n"), scanf("%d", &n) == 1 && n != 2) { if (!n) { for (array_in = 0; array_in < arrays; array_in++) printf("%d %s %s: %.2f\n", array_in, a[array_in].title, a[array_in].author, a[array_in].value); while (printf("输入要删除的记录的编号,可选范围0-%d(q退出):", arrays - 1), scanf("%d", &array_in) == 1) { if (array_in > arrays) printf("记录不存在.\n"); else if (!a[array_in].del_tag) printf("记录不存在.\n"); else a[array_in].del_tag = 0; } while (getchar() != '\n'); } else if (n == 1) { for (array_in = 0; array_in < arrays; array_in++) printf("%d %s %s: %.2f\n", array_in, a[array_in].title, a[array_in].author, a[array_in].value); while (printf("输入要修改的记录的编号,可选范围0-%d(q退出):", arrays - 1), scanf("%d", &array_in) == 1) { if (array_in > arrays) printf("记录不存在\n"); else if (!a[array_in].del_tag) printf("该记录已经被删除\n"); else { while (getchar() != '\n'); while (printf("输入新的书名:"), gets(a[array_in].title) == NULL || a[array_in].title[0] == '\0'); while (printf("输入新的作者名:"), gets(a[array_in].author) == NULL || a[array_in].author[0] == '\0'); printf("输入新的价格:"); scanf("%f", &a[array_in].value); } } while (getchar() != '\n'); } } f_write(a, arrays); } FILE * f_open(void) { FILE *p_file; if ((p_file = fopen("books_s.dat", "a+b")) == NULL) { fprintf(stderr, "无法打开文件"); exit(EXIT_FAILURE); } fseek(p_file, 0, SEEK_END); datas = ftell(p_file); return p_file; } void f_write(BOOKS a[], int arrays) { FILE * p_file; int array_in; if ((p_file = fopen("books_s.dat", "w+b")) == NULL) { fprintf(stderr, "无法打开文件"); exit(EXIT_FAILURE); } for (array_in = 0; array_in < arrays; array_in++) if (a[array_in].del_tag) fwrite(&a[array_in], sizeof(BOOKS), 1, p_file); datas = ftell(p_file); fclose(p_file); }
[此贴子已经被作者于2017-1-31 15:20编辑过]