图书信息包括:登录号、书名、作者名、分类号、出版单位、出版时间、价格等。试设计一图书信息管理系统,使之能提供以下功能:
(1)系统以菜单方式工作。
(2)图书信息录入功能--输入。
(3)图书信息浏览功能--输出。
(4)查询和排序功能:(至少一种查询方式)--算法。
按书名查询
按作者名查询
(5)图书信息的删除与修改。
(1)设计一个主函数和多个子函数,每个子函数完成一个相对独立的子功能。
(2)程序运行时,首先进行口令检查,再显示菜单。并能根据菜单调用相应的函数功能。
口令程序段的功能要求:
《1》提示用户输入一个口令。
《2》用户输入口令后,若口令对,则提示用户通过,可执行后续程序;否则不通过。
《3》可对用户的口令输入进行次数限制(如:重复输入3次都不对),则自动退出系统。
(3)显示数据时,一页显示不下,可分页显示。
- 代码一:
#include <iostream.h> /*引用库函数*/ #include <stdio.h> #include <string.h> #include <conio.h> #include <stdlib.h> #include <windows.h> typedef struct book_info { //定义图书信息的结构体变量并声明新的类型名 char AN[10]; /*登录号*/ char name[20]; /*书名*/ char author[20]; /*作者名*/ char clc[10]; /*分类名*/ char company[20]; /*出版单位*/ char date[20]; /*出版日期*/ char price[10]; /*价格*/ struct book_info *next; }Booklist, *Pointer; int num = 0; //全局变量的定义 Pointer Head = NULL; //头指针为空 FILE *fp; //指向文件的指针 /*声明函数*/ int menu_select(); /*主菜单函数*/ void Insert(Pointer * Head); /*录入函数*/ void Scan(Pointer Head); /*显示函数*/ void Search_name(Pointer Head); /*按书名查找函数*/ void Search_author(Pointer Head); /*按作者名查找函数*/ void Listbyname(Pointer *Head); /*按书名排序函数*/ void Delete(Pointer * Head); //删除函数 void Update(Pointer Head); //修改函数 void Save(); //用文本文件形式保存函数 void Read(); //读入文本文件函数 void Exit(); //退出函数 int main() { //主函数 system("cls"); /*运行前清屏*/ for(;;) { switch(menu_select()) { case 1:Insert(& Head); break; case 2:Scan(Head); break; case 3:Search_name(Head); break; case 4:Search_author(Head); break; case 5:Listbyname(&Head); break; case 6:Delete(&Head); break; case 7:Update(Head); break; case 8:Save(); break; case 9:Read(); break; case 0:Exit(); default: putchar('a'); } } return 0; } menu_select() { //主菜单函数 int a; printf("nttt欢迎使用图书信息管理系统nnnnntt ***** 请按任意键进入系统菜单! ***** n"); getch(); system("cls"); printf("tt********************MENU*********************n"); //主菜单 printf("tt 1. 录入图书信息n"); printf("tt 2. 浏览图书信息n"); printf("tt 3. 按书名查询图书信息n"); printf("tt 4. 按作者名查询图书信息n"); printf("tt 5. 图书信息排序n"); printf("tt 6. 删除图书信息n"); printf("tt 7. 修改图书信息n"); printf("tt 8. 图书数据保存n"); printf("tt 9. 图书信息文件打开n"); printf("tt 0. 退出n"); printf("tt***********************************************n"); do { printf("nt请选择您所需要的服务:"); scanf("%d",&a); } while(a < 0 || a > 9); return a; } void Insert(Pointer * Head) { //录入图书信息函数 char AN[10] ; char c; Pointer p, q, r; printf("ntt**************** 请输入图书信息 ****************n"); /*交互输入*/ printf("ntt请输入登录号:"); scanf("%s", AN); p = q = *Head; //检测登录号是否重复 while(p != NULL) { if(strcmp(p->AN, AN) == 0) { printf ("已经有相同的登录号:"); return; } else { q = p; p = p->next; } } r = (Pointer)malloc(sizeof(Booklist)); r->next = NULL; if(r == NULL) { printf("分配空间失败!"); return; } if(q == NULL) *Head = r; else{ q->next = r; } strcpy(r->AN, AN); printf("ntt输入书名:"); //录入图书信息 scanf("%s", r->name); getchar(); printf("ntt输入作者名:"); scanf("%s", r->author); getchar(); printf("ntt输入分类号:"); scanf("%s", r->clc) ; getchar(); printf("ntt输入出版单位:"); scanf("%s", r->company) ; getchar(); printf("ntt输入出版日期:"); gets(r->date) ; printf("ntt输入价格:"); scanf("%s", r->price); do{ printf("ntt录入成功!!!!"); num++; printf("选择是否继续录入(Y/N)?:"); /*连续录入图书信息*/ getchar(); scanf("%c", &c); if(c == 'y' || c == 'Y') Insert(Head); else { if(c == 'n' || c == 'N') return; else printf("ntt输入错误,请重新输入!!!"); } } while(c != 'y' && c != 'n' && c != 'Y' && c != 'N'); } void Scan(Pointer Head) { //显示图书信息函数 Pointer p; p = Head; if(p == NULL) printf("记录为空"); //检测是否有图书信息 else { printf("nt共有%d条记录",num); while(p != NULL) { printf("nntt登录号:%-10s", p->AN); //显示图书信息 printf("ntt书名: %-20s", p->name); printf("ntt作者名: %-20s", p->author); printf("ntt分类号: %-10s", p->clc); printf("ntt出版单位:%-20s", p->company); printf("ntt出版时间:%-20s", p->date); printf("ntt价格: ¥%-10s", p->price); p = p->next; } printf("ntt请按任意键回到主菜单"); return; } } void Search_name(Pointer Head) { //按书名查找函数 int flag = 0; //标记变量的初值 char name[10]; Pointer p; printf("n请输入需要查询的书名:"); scanf("%s", name); printf("ntt************* 以下是您查找的信息 ***************"); p = Head; while(p != NULL) { if(strcmp(p->name, name) == 0) { //查找符合的图书 printf("nt登录号: %-10s", p->AN); printf("nt书名: %-20s", p->name); printf("nt作者名: %-20s", p->author); printf("nt分类号: %-10s", p->clc); printf("nt出版单位:%-20s", p->company); printf("nt出版时间:%-20s", p->date); printf("nt价格: ¥%-10s", p->price); flag = 1; //找到标记变量设为1 p = p->next; //指针走到下一个节点 } else p = p->next; } if(flag == 0) printf("ntt没有相同书名纪录"); printf("ntt请按任意键返回主菜单"); getchar(); } void Search_author(Pointer Head) { //按作者名查找函数 int flag = 0; char author[10]; Pointer p; printf("n请输入需要查询的作者名:"); scanf("%s", author); printf("ntt************* 以下是您查找的信息 ***************"); p = Head; while(p != NULL) { //查找符合的图书 if(strcmp(p->author, author) == 0) { /*找到图书显示信息*/ printf("nt登录号: %-10s", p->AN); printf("nt书名: %-20s", p->name); printf("nt作者名: %-20s", p->author); printf("nt分类号: %-10s", p->clc); printf("nt出版单位:%-20s", p->company); printf("nt出版时间:%-20s", p->date); printf("nt价格: ¥%-10s", p->price); flag = 1; p = p->next; } else p = p->next; } if(flag == 0) printf("ntt没有相同作者名纪录"); printf("ntt请按任意键返回主菜单"); getch(); } void Listbyname(Pointer *Head) { //按书名排序函数 Pointer p, q; int i, j; char t[10]; char c; if(Head == NULL) { printf("ntt没有任何资料!n"); return; } if(num == 0) { //检查是否存在数据可供排序 printf("ntt图书信息记录为空!!请按任意键返回主菜单。"); getchar(); return; } p = q = *Head; for(i = 0; i < num; i++) { //利用冒泡排序 for(j = i + 1; j < num; j++) { q = p; p = p->next; //使指针指向下一个结点 if(strcmp(q->name,p->name)>0) { //检查二者排序先后:p指针对应数据应排于q指针对应数据后,p,q进行数据交换 strcpy(t, p->AN); strcpy(p->AN, q->AN); strcpy(q->AN, t); strcpy(t, p->author); strcpy(p->author, q->author); strcpy(q->author, t); strcpy(t, p->clc); strcpy(p->clc, q->clc); strcpy(q->clc, t); strcpy(t, p->company); strcpy(p->company, q->company); strcpy(q->company, t); strcpy(t, p->date); strcpy(p->date, q->date); strcpy(q->date, t); strcpy(t, p->name); strcpy(p->name, q->name); strcpy(q->name, t); strcpy(t, p->price); strcpy(p->price, q->price); strcpy(q->price, t); } } q = *Head; p = *Head; } do { printf("nt排序完成,是否显示(Y/N)?:"); /*询问是否显示排序结果*/ getchar(); scanf("%c", &c); if(c == 'y' || c == 'Y') Scan(*Head); //显示排序结果 else { if(c == 'n' || c == 'N') return; //返回主菜单 else printf("ntt输入错误,请重新输入!!!"); //错误则继续询问 } } while(c != 'y' && c != 'n' && c != 'Y' && c != 'N'); } void Delete(Pointer *Head) { /*删除函数*/ int flag = 1; char AN[10]; char c, z; Pointer p, q; printf("ntt******************* 图书删除 *******************n"); printf("t请输入要删除图书的信息的登录号:"); scanf("%s", AN); p = q = *Head; /*查找符合条件的图书*/ while(p != NULL && flag) { if(strcmp(p->AN, AN) == 0) { /*找到该图书*/ printf("tn登录号:%-10s", p->AN); //显示即将要删除的图书的信息 printf("tn书名:%-20s", p->name); printf("tn作者名:%-20s", p->author); printf("tn分类号:%-10s", p->clc); printf("tn出版单位:%-20s", p->company); printf("tn出版时间:%-20s", p->date); printf("tn价格:¥%-10sn", p->price); printf("确定删除?确定请输Y,其它则不删除"); //询问是否删除 getchar(); scanf("%c", &z); if(z == 'Y' || z == 'y') { if(p == *Head) { *Head = p->next; free(p); /*删除图书信息*/ } else { q->next = p->next; free(p); } flag = 0; } else { printf("图书信息未删除,返回主菜单。"); return; } } else { q = p; p = p->next; /*指针走到下一个节点*/ } printf("tt删除成功!!!n"); } if(flag) printf("t没有找到可以删除的数据!!!"); do { printf("选择是否继续删除(Y/N)?:"); /*连续删除图书信息*/ getchar(); scanf("%c", &c); if(c == 'y' || c == 'Y') Delete(Head); /*继续删除*/ else { if(c == 'n' || c == 'N') return; /*不删除返回主菜单*/ else printf("ntt输入错误,请重新输入!!!"); } } while(c != 'y' && c != 'n' && c != 'Y' && c != 'N'); } void Update(Pointer Head) { /*图书信息修改函数*/ int flag = 1; char AN[10]; char c; Pointer p; printf("ntt***************** 图书信息修改 *****************n"); printf("t请输入要修改的图书的登录号:"); scanf("%s", AN); /*查找符合条件的图书*/ p = Head; while(p != NULL && flag) { if(strcmp(p->AN, AN) == 0) { printf("ntt请输入登录号:"); /*修改图书信息*/ scanf("%s", p->AN); printf("ntt输入书名:"); scanf("%s", p->name); getchar(); printf("ntt输入作者名:"); scanf("%s", p->author); getchar(); printf("ntt输入分类号:"); scanf("%s",p->clc) ; getchar(); printf("ntt输入出版单位:"); scanf("%s", p->company) ; getchar(); printf("ntt输入出版日期:"); gets(p->date); printf("ntt输入价格:"); scanf("%s", p->price); flag = 0; printf("修改成功!!n"); } else p = p->next; /*指针走到下一个节点*/ } if(flag) printf("ntt没有该图书记录!!!"); do { printf("选择是否继续修改(Y/N)?:"); /*连续修改图书信息*/ getchar(); scanf("%c", &c); if(c == 'y' || c == 'Y') Update(Head); /*继续修改*/ else { if(c == 'n' || c == 'N') return; //不修改,返回菜单 else printf("ntt输入错误,请重新输入!!!"); } } while(c != 'y' && c != 'n' && c != 'Y' && c != 'N'); //输入错误则继续询问 } void Save() { /*以文本文件形式保存的函数*/ Pointer p; p = Head; char file[20]; /*用来存放文件保存路径以及文件名*/ printf("请输入文件路径及文件名:"); scanf("%s", file); if((fp = fopen(file, "w+")) == NULL) { /*判断能否打开文件*/ printf("不能打开文件!n"); return; } while(p != NULL) { fprintf(fp, "%st%st%st%st%st%st%sn", p->AN, p->name, p->author, p->clc, p->company, p->date, p->price); //将数据写入文件 p = p->next; /*下移一个结点*/ } fclose(fp); //写入完成,关闭文件 printf("文件已经保存!n"); return; } void Read() { /*读入文本文件的函数*/ Pointer p, q; int m = 0; char file[20]; printf("请输入文件路径及文件名:"); scanf("%s", file); /*输入文件路径及名称*/ if((fp = fopen(file, "r+")) == NULL) { //检查文件是否存在 printf("不能打开文件!n"); return; } m = m + 1; if(m == 1) { p = (Pointer)malloc(sizeof(Booklist)); /*开辟一个新单元*/ Head = p; //将p的地址赋给头指针Head fscanf(fp, "%st%st%st%st%st%st%stn", &p->AN, &p->name, &p->author, &p->clc, &p->company, &p->date, &p->price); /*文件读入*/ do { num = num + 1; //记录书籍信息量 if(num == 1) //区别开链表开头与中间的处理方法 Head->next = p; else q->next = p; q = p; p = (Pointer)malloc(sizeof(Booklist)); /*开辟一个新单元*/ fscanf(fp,"%st%st%st%st%st%st%stn", &p->AN, &p->name, &p->author, &p->clc, &p->company, &p->date, &p->price); //读入文件数据 } while(!feof(fp)); //检查文件是否结束,若是则停止读入,否则继续读入 q->next = p; p->next = NULL; //链表结尾处理 num = num + 1; //正确的图书信息量 } printf("写入数据成功,可返回浏览其信息。"); fclose(fp); /*结束读入,关闭文件*/ return; } void Exit() { /*退出程序的函数*/ char c; do { printf("ntt退出中......是否保存到文件(Y/N)?"); /*询问是否保存图书信息,防止丢失*/ getchar(); scanf("%c", &c); if(c == 'y' || c == 'Y') { Save(); exit(0); } else { if(c == 'n' || c == 'N') { exit(0); } else printf("ntt输入错误,请重新输入!!!"); } } while(c != 'y' && c != 'n' && c != 'Y' && c != 'N'); //错误则继续询问 }
- 代码二:
// (1)主函数 void main() { /*主函数*/ struct library *head; int d, i, b; for(i = 0; i < 3; i++) { printf("nnnnnnnnnnn 请用户输入口令:"); scanf("%d", &d); if(d != 111) { printf("口令输入错误n"); if(i == 2) { printf(“你已经输入3次错误口令,系统自动关闭n”); exit(0); } else break; } system("cls"); do { switch(b = menu()){ case 1: head = creat(); save(head); break; case 2: out(head); break; case 3: check(head); save(head); break; case 4: scores(head); save(head); out(head); break; case 5: del(head); save(head); out(head); break; case 6: corret(head); save(head); out(head); break; case 0: printf(“谢谢使用n”); exit(0); default: printf(“你所选择功能不存在请重新选择n”); } }while(b != 0); } } // (2)菜单和初始化 int menu() { /*菜单*/ char *menu[] = {"nnnnnn ========图书信息管理系统==========nn", " 1. 录入功能 ","2. 浏览功能 nn", " 3. 查询功能 ","4. 排序功能 nn", " 5. 删除功能 ","6. 修改功能 nn", " 0. 退出系统nn", " ==================================n"}; int c, i; for(i = 0; i < 9; i++) printf("%s",menu[i]); do { printf("n 请输入选项(0~6)并按回车键:"); scanf("%d",&c); } while(c < 0 || c > 6); return(c); } 《(3)(录入函数) 》 int n; struct library *creat(void) { /*建立链表*/ struct library *head; struct library *p1, *p2; n = 0; system("cls"); printf(" 这是录入功能请输入图书数据n"); p1 = p2 = (struct library *)malloc(LEN); head = NULL; while(1) { printf("登录号:"); scanf("%ld", &p1->num); if(p1->num != 0) { printf("书名:"); scanf("%s", p1->bookname); printf("作者名:"); scanf("%s", p1->author); printf("类型号:"); scanf("%s", p1->type); printf("出版单位"); scanf("%s", p1->publishing_house); printf("出版时间:"); scanf("%s", p1->publishing_time); printf("价格:"); scanf("%f", &p1->sale); printf("n"); n = n + 1; if(n == 1) head = p1; else p2->next = p1; p2 = p1; p1 = (struct library *)malloc(LEN); } else break; } p2->next = NULL; return(head); } 《(4)(保存函数) 》 void save(struct library *head) { /*保存文件*/ FILE *fp; if((fp = fopen("library.dat", "wb")) == NULL) { printf("不能打开文件n"); exit(0); } do { fwrite(head, sizeof(struct library), 1, fp); head = head->next; } while(head != NULL); fclose(fp); printf("n文件已保存nnn"); } 《(5)输出函数 》 struct library *out(struct library *head) { /*读入文件*/ FILE *fp; struct library *p1, *p2; if((fp = fopen("library.dat","rb")) == NULL) { printf("不能打开文件n"); exit(0); } fp = fopen("library.dat", "rb"); p1 = (struct library *)malloc(LEN); head = p2 = p1; printf("n 图书的数据为n"); while(!feof(fp)) { fread(p1, sizeof(struct library), 1, fp); printf(" 登录号:%ld", p1->num); printf("书名:%s", p1->bookname); printf("作者名:%s", p1->author); printf("类型号:%s", p1->type); printf("出版单位:%s", p1->publishing_house); printf("出版时间:%s", p1->publishing_time); printf("价格:%6.2f", p1->sale); printf("n"); if(p1->next == 0) break; p1 = (struct library *)malloc(LEN); p2->next = p1; p2 = p1; } p2->next = 0; fclose(fp); return(head); } 《(6)查询函数 》 struct library *check(struct library *head) { /*查询函数*/ struct library *p; char style[10], a[10]; printf(" 这是图书查询功能n"); printf(" 请输入查询的方法:"); scanf("%s", style); printf(" 请输入要查询%s:",style); scanf("%s", a); printf("你所查询的数据:n"); if(head == NULL) { printf("n该列表为空表n"); goto end; } p = head; if(p != 0) { if(strcmp(style, "bookname") == 0) { if(strcmp(a, p->bookname) == 0) { printf(" 登录号:%ld", p->num); printf("书名:%s", p->bookname); printf("作者名:%s", p->author); printf("类型号:%s", p->type); printf("出版单位:%s", p->publishing_house); printf("出版时间:%s", p->publishing_time); printf("价格:%6.2f", p->sale); printf("n"); } else { do { p = p->next; } while(strcmp(a, p->bookname) != 0 && p->next != NULL); printf(" 登录号:%ld", p->num); printf("书名:%s", p->bookname); printf("作者名:%s", p->author); printf("类型号:%s", p->type); printf("出版单位:%s", p->publishing_house); printf("出版时间:%s", p->publishing_time); printf("价格:%6.2f", p->sale); printf("n"); } } if(strcmp(style, "author") == 0) { if(strcmp(a, p->author) == 0) { printf(" 登录号:%ld", p->num); printf("书名:%s", p->bookname); printf("作者名:%s", p->author); printf("类型号:%s", p->type); printf("出版单位:%s", p->publishing_house); printf("出版时间:%s", p->publishing_time); printf("价格:%6.2f", p->sale); printf("n"); } else { do { p = p->next; } while(strcmp(a, p->author) != 0 && p->next != NULL); printf(" 登录号:%ld",p->num); printf("书名:%s",p->bookname); printf("作者名:%s",p->author); printf("类型号:%s",p->type); printf("出版单位:%s",p->publishing_house); printf("出版时间:%s",p->publishing_time); printf("价格:%6.2f",p->sale); printf("n"); } } } end: return(head); } 《(7)排序函数 》 struct library *scores(struct library *head) { /*排序函数*/ struct library *p1, *p2; float i; long t; char a[20], b[20], c[20], d[20], e[20]; p1 = head; if(head == NULL) { printf("该列表为空表n"); } else { while(p1 != NULL){ p2 = p1->next; while(p2 != NULL) { if(strcmp(p1->bookname, p2->bookname) >= 0) { t = p2->num; p2->num = p1->num; p1->num = t; i = p2->sale; p2->sale = p1->sale; p1->sale = i; strcpy(a, p2->bookname); strcpy(b, p2->author); strcpy(c, p2->type); strcpy(d, p2->publishing_house); strcpy(e, p2->publishing_time); strcpy(p2->bookname, p1->bookname); strcpy(p2->author, p1->author); strcpy(p2->type, p1->type); strcpy(p2->publishing_house, p1->publishing_house); strcpy(p2->publishing_time, p1->publishing_time); strcpy(p1->bookname, a); strcpy(p1->author, b); strcpy(p1->type, c); strcpy(p1->publishing_house, d); strcpy(p1->publishing_time, e); } p2 = p2->next; } p1 = p1->next; } } printf("文件已排序"); return(head); } 《(8)修改函数 》 struct library *corret(struct library *head) { /*修改函数*/ struct library *p; char m[10], z[10]; int b, x; float y; printf("请输入要修改的书名:"); scanf("%s", m); if(head==NULL) { printf("n该列表为空表n"); goto end; } p = head; if(p != 0) { while((strcmp(p->bookname, m) != 0) && p->next != NULL) { p = p->next; } if(strcmp(p->bookname, m) == 0) { printf("登录号:%ld", p->num); printf("书名:%s", p->bookname); printf("作者名:%s", p->author); printf("类型号:%s", p->type); printf("出版单位:%s", p->publishing_house); printf("出版时间:%s", p->publishing_time); printf("价格:%6.2f", p->sale); printf("n"); printf("请输入要修改的类型:n1.登录号n2.书名n3.作者名n4.分类号n5.出版单位n6.出版时间n7.价格n"); scanf("%d", &b); printf("请输入修改信息:"); if(b == 1) { scanf("%ld", &x); p->num = x; } else if (b == 7) { scanf("%f", &y); p->sale = y; } else { scanf("%s",z); switch(b) { case 2: strcpy(p->bookname, z); break; case 3: strcpy(p->author, z); break; case 4: strcpy(p->type, z); break; case 5: strcpy(p->publishing_house, z); break; case 6: strcpy(p->publishing_time, z); break; default:printf("发生错误n"); } } } } printf("文件已修改"); end: return(head); } 《(9)删除函数 》 struct library *del(struct library *head) { /*删除函数*/ struct library *p1, *p2; long num; if(head == NULL) { printf("n图书记录为空!n"); goto end; } printf("请输入要删除的图书数据的登录号:"); scanf("%ld",&num); while(num != 0) { p1 = head; while(num != p1->num && p1->next != NULL) { p2 = p1; p1 = p1->next; } if(num == p1->num) { if(p1 == head) head = p1->next; else p2->next = p1->next; printf("删除:%ldn", num); } else printf("%ld 找不到这个图书记录!n", num); scanf("%ld", &num); } printf("已删除信息"); end: return(head); }