#include <stdio.h> #include <string.h> struct Book{ char bno[10]; char bname[20]; float bprice; char bauther[20]; }books[100] = {{"B001", "程序设计", 45.5f, "里已"}, {"B002", "python程序设计", 20.5f, "Pinpe"}, {"B003", "renpy游戏开发", 32.5f, "李四"}, {"B004", "数据库使用", 47.5f, "王五"}, {"B005", "java入门", 80.5f, "张柳"}}; int bsum = 5; // 图书数量 // 显示主菜单 void dismenu(){ printf("=====欢迎来到图书管理系统=====\n"); printf("1. 查询图书\n"); printf("2. 添加图书\n"); printf("3. 修改图书\n"); printf("4. 删除图书\n"); printf("5. 退出\n"); printf("==============================\n\n"); printf(">>> "); } // 显示查询菜单 void disselmenu(){ printf("===========查询图书===========\n"); printf("1. 查询所有图书\n"); printf("2. 按书名模糊查询\n"); printf("3. 按书价区间查询\n"); printf("4. 返回\n"); printf("==============================\n\n"); printf(">>> "); } // 查询图书 void selbook(){ while(1){ disselmenu(); int selno; scanf("%d", &selno); switch (selno) { case 1: printf("%10s%20s%10s%10s\n", "图书编号", "图书名称", "图书价格", "图书作者"); for(int i=0;i<bsum;i++){ printf("%10s%20s%6.2f%10s\n", books[i].bno, books[i].bname, books[i].bprice, books[i].bauther); } break; case 2: printf("请输入要查询的书名:"); char selname[20]; scanf("%s", selname); printf("%10s%20s%10s%10s\n", "图书编号", "图书名称", "图书价格", "图书作者"); for(int i=0;i<bsum;i++){ if(strstr(books[i].bname, selname)){ printf("%10s%20s%6.2f%10s\n", books[i].bno, books[i].bname, books[i].bprice, books[i].bauther); } } break; case 3:{ float a, b; printf("请输入价格区间:"); scanf("%f%f", &a, &b); printf("%10s%20s%10s%10s\n", "图书编号", "图书名称", "图书价格", "图书作者"); for(int k=0;k<bsum;k++){ if(books[k].bprice >=a && books[k].bprice <= b){ printf("%10s%20s%6.2f%10s\n", books[k].bno, books[k].bname, books[k].bprice, books[k].bauther); } } break; } case 4: printf("是否退出?(y/n)"); char ch; scanf("%c", &ch); scanf("%c", &ch); if(ch == 'y' || ch == 'Y'){ return; } } } } // 添加图书 void addbook(){ while(1){ printf("请输入书号:"); scanf("%s", books[bsum].bno); printf("请输入书名:"); scanf("%s", books[bsum].bname); printf("请输入书价:"); scanf("%f", &books[bsum].bprice); printf("请输入作者:"); scanf("%s", books[bsum].bauther); bsum++; printf("添加成功\n\n"); printf("是否继续添加?(y/n)"); char ch; scanf("%c", &ch); scanf("%c", &ch); if(ch == 'n' || ch == 'N'){ return; } } } // 修改图书 void modifybook(){ while(1){ printf("请输入需要修改的书号:"); char selno[10]; scanf("%s", selno); int i; for(i=0;i<bsum;i++){ if(strcmp(books[i].bno, selno) == 0){ break; } } printf("书号%s的原价为%6.2f,输入新的价格:", selno, books[i].bprice); scanf("%f", &books[i].bprice); printf("修改成功\n\n"); printf("是否继续修改?(y/n)"); char ch; scanf("%c", &ch); scanf("%c", &ch); if(ch == 'n' || ch == 'N'){ return; } } } // 删除图书 void delbook(){ while(1){ printf("请输入需要删除的书号:"); char selno[10]; scanf("%s", selno); int i; for(i=0;i<bsum;i++){ if(strcmp(books[i].bno, selno) == 0){ break; } } int j; for(int j = i; j < bsum - 1; j++) { books[j] = books[j + 1]; // 整体复制结构体 } bsum--; printf("删除成功\n\n"); printf("是否继续删除?(y/n)"); char ch; scanf("%c", &ch); scanf("%c", &ch); if(ch == 'n' || ch == 'N'){ return; } } } int main(){ while(1){ dismenu(); int selon; scanf("%d", &selon); switch (selon) { case 1: selbook(); break; case 2: addbook(); break; case 3: modifybook(); break; case 4: delbook(); break; case 5: printf("是否退出?(y/n)"); char chexit; scanf("%c", &chexit); scanf("%c", &chexit); if(chexit == 'Y' || chexit == 'y'){ return 0; } } } return 0; }
典中典图书管理系统