前言
我也不知道为啥我学食品的也要学C语言,甚至下学期还要学大物绝了。
写这篇文章的原因也很简单,我见了太多来问我的同学了所以我干脆将我的代码给大家,如果你会一些C语言的话这篇文章的东西你可以的代码可以改改交上去给你省一些事情,如果你不会C语言的话,我估计也没办法了,建议改改我的菜单然后就交吧,混个及格分应该还是够的(如果要求差别不大的话)
不多说了我直接把代码贴上来也不分文件啥的了。
我实现的要求
注:博主也偷懒了比如我们的时间是直接用字符串的比较实现的虽然知道不对但是我实在懒得改了,因为我感觉我的老师也不会看有这时间不如看看C++的知识🤦♂️。
代码(直接复制到你的VS内即可)
#define _CRT_SECURE_NO_WARNINGS 1 #include<stdio.h> #include<stdlib.h> #include<assert.h> #include<string.h> #define NAMEAUTHOR_MAX 30//作者名 #define NAMEBOOK_MAX 30//书名 #define PRESS_MAX 20//出版社名 #define TIME_MAX 30 #define PEOPLE_MAX 5 #define CAPACITY_SZ 5 #define ADD_SZ 3 #define CODE_MAX 30//条形码 //姓名、性别、年龄、电话、住址 typedef struct Book { char name_book[NAMEBOOK_MAX];//书名 char name_author[NAMEAUTHOR_MAX];//作者名 char barcode[CODE_MAX];//条形码 char press[PRESS_MAX];//出版社 char time[TIME_MAX];//出版日期 int price;//价格 int inventory;//库存 }Book;//存放一个人的结构体 typedef struct contact { Book* date;//动态管理需要使用指针 int capacity;//用于存放容量 int sz;//个数 }contact; void InitContact(contact* pc);//初始化 void AddContact(contact* pc);//增加图书功能 void ShowContact(contact* pc);//展示所有图书功能 void SortContact(contact* pc);//排序功能的实现--包含所有排序 void DelContact(contact* pc);//删除图书功能的实现 void Clear_Every(contact* pc);//清除所有图书的实现 void DestoryContact(contact* pc);//对开辟空间进行销毁 void SearchContact(contact* pc);//查找图书 void ModifyContact(contact* pc);//改变图书信息 void SaveContact(contact* pc);//保存数据到文件中 void CheckContact(contact* pc);//内部函数用于扩容时的内存检查 void Statistics(contact* pc);//用于检索特殊情况图书 void menu() { printf("*********************************\n"); printf("*****1.ADD 2.DEL 3.SEARCH ***\n"); printf("*****4.MODIFY 5.SHOW 6.SORT *****\n"); printf("** 7.CLEAR_EVERY 8.Statistical *\n"); printf("************ 0.EXit ***********\n"); printf("*********************************\n"); printf("*********************************\n"); } enum Opinion { EXIT, ADD, DEL, SEARCH, MODIFY, SHOW, SORT, CLEAR_EVERY, Statistical, }; int main() { contact con = { 0 }; //结构体初始化 InitContact(&con); //功能实现 int input = 0; do { menu(); printf("请输入想要的功能:>"); scanf("%d", &input); switch (input)//通讯录功能 { case CLEAR_EVERY: Clear_Every(&con); case ADD: AddContact(&con); break; case SORT: SortContact(&con); break; case DEL: DelContact(&con); break; case SHOW: ShowContact(&con); break; case MODIFY: ModifyContact(&con); break; case SEARCH: SearchContact(&con); break; case Statistical: Statistics(&con); break; case EXIT: SaveContact(&con);//将信息保存到文件中,关闭文件并将文件指针NULL化 DestoryContact(&con);//动态指针NULL化free动态内存 printf("退出成功"); break; default: printf("输入错误\n"); break; } } while (input); return 0; } void LoadContact(contact* pc) { FILE* p = fopen("book.dat", "r"); if (p == NULL) { perror("LoadContact"); return; } Book tmp = { 0 }; while (fread(&(pc->date[pc->sz]), sizeof(Book), 1, p)) { pc->sz++; CheckContact(pc); } fclose(p); p = NULL; } void InitContact(contact* pc) { assert(pc); pc->date = (Book*)calloc(CAPACITY_SZ, sizeof(Book)); if (pc->date == NULL) { perror("InitContact"); return; } pc->capacity = CAPACITY_SZ; pc->sz = 0; LoadContact(pc); } void CheckContact(contact* pc) { if (pc->sz == pc->capacity) { Book* ptr = realloc(pc->date, sizeof(Book) * (pc->capacity + ADD_SZ)); if (ptr != NULL) { pc->date = ptr; printf("增容成功\n"); pc->capacity += ADD_SZ; } else { printf("增容失败"); perror("CheckContact"); } } } void AddContact(contact* pc)//增加人数 { CheckContact(pc);//检查通讯录是否已被占满并实现占满增加 printf("请输入书姓名:>\n"); scanf("%s", pc->date[pc->sz].name_book); printf("请输入作者名\n"); scanf("%s", pc->date[pc->sz].name_author); printf("请输入序列号:>\n"); scanf("%s", pc->date[pc->sz].barcode); printf("请输入出版社>:\n"); scanf("%s", pc->date[pc->sz].press); printf("请输入出版时间>:\n"); scanf("%s", pc->date[pc->sz].time); printf("请输入价格>:\n"); scanf("%d", &pc->date[pc->sz].price); printf("请输入库存>:\n"); scanf("%d", &pc->date[pc->sz].inventory); (pc->sz)++; printf("增加成功\n"); } void ShowContact(contact* pc) { int i = 0; //打印标题 printf("%-10s\t%-5s\t%-5s\t%-20s\t%-5s\t%-5s\t%-10s\n", "书名", "作者名", "条形码", "出版社", "出版日期", "定价", "馆藏数量"); //打印数据 for (i = 0; i < pc->sz; i++) { printf("%-10s\t%-5s\t%-5s\t%-20s\t%-10s\t%-5d\t%-10d\n", pc->date[i].name_book, pc->date[i].name_author, pc->date[i].barcode, pc->date[i].press, pc->date[i].time, pc->date[i].price, pc->date[i].inventory); } } int cmp_code(const void* p1, const void* p2) { return (strcmp(((Book*)p1)->barcode, ((Book*)p2)->barcode)); }int cmp_time(const void* p1, const void* p2) { return (strcmp(((Book*)p1)->time, ((Book*)p2)->time)); }int cmp_nums(const void* p1, const void* p2) { return (((Book*)p1)->inventory) - (((Book*)p2)->inventory); } void SortContact(contact* pc) { printf("按照条形码排序输入>:1\n根据数量排序输入>:2\n根据出版时间排序输入>:3\n"); int input = -1; scanf("%d", &input); if (input == 1) { qsort(pc->date, pc->sz, sizeof(pc->date[0]), cmp_code); } else if (input == 2) { qsort(pc->date, pc->sz, sizeof(pc->date[0]), cmp_nums); } else if (input == 3) { qsort(pc->date, pc->sz, sizeof(pc->date[0]), cmp_time); } printf("排序成功\n"); ShowContact(pc); } int FindContact_name(contact* pc, char* name) { for (int i = 0; i < pc->sz; i++) { if (strcmp(pc->date[i].name_book, name) == 0) { return i; } } return -1; } int FindContact_code(contact* pc, char* code) { for (int i = 0; i < pc->sz; i++) { if (strcmp(pc->date[i].barcode, code) == 0) { return i; } } return -1; } void DelContact(contact* pc) { assert(pc); if (pc->sz == 0) { printf("系统内容为空"); return; } printf("根据书名删除选>:1\n根据条形码删除请选>:2\n"); int input = 0; scanf("%d", &input); if (input == 1) { char name[NAMEBOOK_MAX] = { 0 }; printf("请输入要删除书籍的姓名>:"); scanf("%s", name); int pos = FindContact_name(pc, name); if (-1 == pos) { printf("书籍不存在\n"); return; } int i = 0; for (i = pos; i < pc->sz; i++) { pc->date[i] = pc->date[i + 1]; } pc->sz--; printf("删除成功\n"); } else if (input == 2) { char code[CODE_MAX] = { 0 }; printf("请输入要删除书籍的条形码>:"); scanf("%s", code); int pos = FindContact_code(pc, code); if (-1 == pos) { printf("书籍不存在\n"); return; } int i = 0; for (i = pos; i < pc->sz; i++) { pc->date[i] = pc->date[i + 1]; } pc->sz--; printf("删除成功\n"); } else { printf("输入失败\n"); } } void ModifyContact(contact* pc) { printf("根据序号表请输入>:1\n根据书名请输入>:2\n"); int por = -1; int input = 0; scanf("%d", &input); if (input == 2) { printf("请输入要改变书的姓名>:\n"); char name[NAMEBOOK_MAX] = { 0 }; scanf("%s", name); por = FindContact_name(pc, name); } else if (input == 1) { printf("请输入要改变书的姓名>:\n"); char code[CODE_MAX] = { 0 }; scanf("%s", code); por = FindContact_name(pc, code); } else { printf("输入失败\n"); return; } if (-1 == por) { printf("书不存在\n"); return; } printf("请重新输入改后的信息\n"); printf("请输入书姓名:>\n"); scanf("%s", pc->date[por].name_book); printf("请输入作者名\n"); scanf("%s", pc->date[por].name_author); printf("请输入序列号:>\n"); scanf("%s", pc->date[por].barcode); printf("请输入出版社>:\n"); scanf("%s", pc->date[por].press); printf("请输入出版时间>:\n"); scanf("%s", pc->date[por].time); printf("请输入价格>:\n"); scanf("%d", &pc->date[por].price); printf("请输入库存>:\n"); scanf("%d", &pc->date[por].inventory); printf("改变成功\n"); } int FindContact_name_au(contact* pc, char* name_au) { for (int i = 0; i < pc->sz; i++) { if (strcmp(pc->date[i].name_author, name_au) == 0) { return i; } } return -1; } int FindContact_press(contact* pc, char* press) { for (int i = 0; i < pc->sz; i++) { if (strcmp(pc->date[i].name_book, press) == 0) { return i; } } return -1; } void SearchContact(contact* pc) { printf("根据条形码请输入>:1\n根据书名请输入>:2\n根据作者名请输入>: 3\n根据出版社请输入>: 4\n"); int input = 0; scanf("%d", &input); if (input == 2) { printf("请输入书的姓名>:\n"); char name[NAMEBOOK_MAX] = { 0 }; scanf("%s", name); int sum = 0; for (int i = 0; i < pc->sz; i++) { if (strcmp(pc->date[i].name_book, name) == 0) { printf("%-10s\t%-5s\t%-5s\t%-20s\t%-5s\t%-5s\t%-10s\n", "书名", "作者名", "条形码", "出版社", "出版日期", "定价", "馆藏数量"); printf("%-10s\t%-5s\t%-5s\t%-20s\t%-10s\t%-5d\t%-10d", pc->date[i].name_book, pc->date[i].name_author, pc->date[i].barcode, pc->date[i].press, pc->date[i].time, pc->date[i].price, pc->date[i].inventory); sum++; printf("\n"); } } printf("符合情况的有%d种\n", sum); } else if (input == 1) { printf("请输入书的序号>:\n"); char code[CODE_MAX] = { 0 }; scanf("%s", code); int sum = 0; for (int i = 0; i < pc->sz; i++) { if (strcmp(pc->date[i].barcode, code) == 0) { printf("%-10s\t%-5s\t%-5s\t%-20s\t%-5s\t%-5s\t%-10s\n", "书名", "作者名", "条形码", "出版社", "出版日期", "定价", "馆藏数量"); printf("%-10s\t%-5s\t%-5s\t%-20s\t%-10s\t%-5d\t%-10d", pc->date[i].name_book, pc->date[i].name_author, pc->date[i].barcode, pc->date[i].press, pc->date[i].time, pc->date[i].price, pc->date[i].inventory); sum++; printf("\n"); } } printf("符合情况的有%d种\n", sum); } else if (input == 3) { printf("请输入书的作者名>:\n"); char auname[NAMEAUTHOR_MAX] = { 0 }; scanf("%s", auname); int sum = 0; for (int i = 0; i < pc->sz; i++) { if (strcmp(pc->date[i].name_author, auname) == 0) { printf("%-10s\t%-5s\t%-5s\t%-20s\t%-5s\t%-5s\t%-10s\n", "书名", "作者名", "条形码", "出版社", "出版日期", "定价", "馆藏数量"); printf("%-10s\t%-5s\t%-5s\t%-20s\t%-10s\t%-5d\t%-10d", pc->date[i].name_book, pc->date[i].name_author, pc->date[i].barcode, pc->date[i].press, pc->date[i].time, pc->date[i].price, pc->date[i].inventory); sum++; printf("\n"); } } printf("符合情况的有%d种\n", sum); } else if (input == 4) { printf("请输入出版社名称>:\n"); char press[PRESS_MAX] = { 0 }; scanf("%s", press); int sum = 0; for (int i = 0; i < pc->sz; i++) { if (strcmp(pc->date[i].press, press) == 0) { printf("%-10s\t%-5s\t%-5s\t%-20s\t%-5s\t%-5s\t%-10s\n", "书名", "作者名", "条形码", "出版社", "出版日期", "定价", "馆藏数量"); printf("%-10s\t%-5s\t%-5s\t%-20s\t%-10s\t%-5d\t%-10d", pc->date[i].name_book, pc->date[i].name_author, pc->date[i].barcode, pc->date[i].press, pc->date[i].time, pc->date[i].price, pc->date[i].inventory); sum++; printf("\n"); } } printf("符合情况的有%d种\n", sum); } else { printf("未找到该指令\n"); return; } } void DestoryContact(contact* pc) { free(pc->date); pc->date = NULL; pc->sz = 0; pc->capacity = 0; } void SaveContact(contact* pc) { FILE* p = fopen("book.dat", "w"); if (p == NULL) { perror("SaveContact"); return; } int i = 0; for (i = 0; i < pc->sz; i++) { fwrite(pc->date + i, sizeof(Book), 1, p); } fclose(p); p = NULL; } void Clear_Every(contact* pc) { assert(pc); printf("您确定吗?\n输入1以确定您的选择\n"); int input = 0; scanf("%d", &input); if (1 == input) { pc->date = (Book*)calloc(CAPACITY_SZ, sizeof(Book)); if (pc->date == NULL) { perror("Clear_Every"); return; } pc->capacity = CAPACITY_SZ; pc->sz = 0; printf("清除成功"); } else { printf("信息还在\n"); return; } } void Statistics(contact* pc) { printf("统计馆藏数量小于某值请输入>:1\n统计出版日期早于某个值的输入>:2"); int input = 0; scanf("%d", &input); if (input == 1) { int num = 0; printf("请输入值\n"); scanf("%d", &num); for (int i = 0; i < pc->sz; i++) { if (pc->date[i].inventory < num) { printf("%-10s\t%-5s\t%-5s\t%-20s\t%-5s\t%-5s\t%-10s\n", "书名", "作者名", "条形码", "出版社", "出版日期", "定价", "馆藏数量"); printf("%-10s\t%-5s\t%-5s\t%-20s\t%-10s\t%-5d\t%-10d", pc->date[i].name_book, pc->date[i].name_author, pc->date[i].barcode, pc->date[i].press, pc->date[i].time, pc->date[i].price, pc->date[i].inventory); } } } else if (input == 2) { char time[TIME_MAX] = { 0 }; printf("请输入时间\n"); scanf("%s", time); for (int i = 0; i < pc->sz; i++) { if (strcmp(time, pc->date[i].time) > 0) { printf("%-10s\t%-5s\t%-5s\t%-20s\t%-5s\t%-5s\t%-10s\n", "书名", "作者名", "条形码", "出版社", "出版日期", "定价", "馆藏数量"); printf("%-10s\t%-5s\t%-5s\t%-20s\t%-10s\t%-5d\t%-10d", pc->date[i].name_book, pc->date[i].name_author, pc->date[i].barcode, pc->date[i].press, pc->date[i].time, pc->date[i].price, pc->date[i].inventory); } } } else { printf("输入错误"); } }
ok,代码也不多大家应该直接复制到VS上就可以用,如果有运行错误可以找我,我会进行更改的,如果是想让我帮忙写东西的话,要收费的😁。
结尾
这次的博客主要是给写大学生作业的人看的,没有多少技术含量。