C语言图书管理系统代码

合集下载

C语言课程设计图书馆管理系统程序代码

C语言课程设计图书馆管理系统程序代码

#include〈stdio.h〉#include<windows.h〉#include〈string。

h〉#include<conio。

h>#define M 100struct books{char tsmc[12];inttsbh;float dj;char zz[12];char czzt[12];char xm[12];char xb[12];intxh; }s[M];int N;bool flag;intmain (){intsele;flag=0;sele=1;void sr();void cx();void sc();void js();void hs();void tc();void jsh();//system ("tsgl”);printf("\n\n");printf(" ************************\n"); printf(" 1:输入2:查询3:删除\n");printf(”4:借书5:还书0:退出\n");printf(” ************************\n"); while(1){printf(”\n\n请输入功能序号:");scanf (”%d",&sele);if(sele>=0 && sele<=5){switch(sele){case 1:sr();break;case 2:cx();break;case 3:sc();break;case 4:js();break;case 5:hs();break;case 0:jsh();break;}if(flag)break;printf(”\n\n");printf(” ************************\n”); printf(" 1:输入2:查询3:删除\n");printf(”4:借书5:还书0:退出\n”);printf(” ************************\n”);}else{printf(”输入错误请再次输入|\n\n");printf("\n\n");printf(” ************************\n”);printf(" 1:输入2:查询3:删除\n”);printf(”4:借书5:还书0:退出\n");printf(" ************************\n");}printf("\n\n让任意键继续\n”);} //wr_file();}/******主函数***//*********输入*********/void sr(){intn,i;N=0;printf("可输入的书本数:");scanf("%d",&n);N=n+N;printf("请输入相关信息:\n");for(i=1;i<=n;i++){printf(”图书名称:”);scanf(”%s",s[N-n-1+i]。

图书管理系统c代码

图书管理系统c代码

#include <iostream> #include <iomanip>#include <string>#include <fstream>using namespace std;const int Reader=100;const int Maxb=100;const int Bor=5;class Student{private:int tag;int number;char name[10];int borbook[Bor];public:Student() { }char *getname(){return name;}int gettag(){return tag;}int getnumber(){return number;}void setname(char *na){strcpy(name,na);}void delbook(){tag=1;}void addStudent(int n,char *na) {tag=0;number=n;strcpy(name,na);for(int i=0;i<Bor;i++)borbook[i]=0;}void borrowbook(int bookid)//借书操作{for(int i=0;i<Bor;i++){if (borbook[i]==0){borbook[i]=bookid;return;}}}int retbook(int bookid)//还书操作{for(int i=0;i<Bor;i++){if(borbook[i]==bookid){borbook[i]=0;return 1;}}return 0;}void output()//读出读者信息{cout << setw(5) << number <<setw(10) << name<<"借书编号:["; for(int i=0;i<Bor;i++)if(borbook[i]!=0)cout << borbook[i] << "|";cout << "]"<<endl;}};class RData{private:int top; //读者记录指针Student read[Reader];public:RData() //构造函数,将Student.txt读到read[]中Student s;top=-1;fstream file("Student.txt",ios::in);//打开一个输入文件while (1){file.read((char *)&s,sizeof(s));if (!file)break;top++;read[top]=s;}file.close();}void clear(){top=-1;}int addStudent(int n,char *na)//查找是否存在{Student *p=equal(n);if (p==NULL)top++;read[top].addStudent(n,na);return 1;}return 0;}Student *equal(int Studentid)//按编号查找{for (int i=0;i<=top;i++)if (read[i].getnumber()==Studentid && read[i].gettag()==0){return &read[i];}return NULL;}void output(){for (int i=0;i<=top;i++)read[i].output();void Studentdata();//读者库维护~RData() //析构函数,将read[]写到Student.txt文件中{fstream file("Student.txt",ios::out);for (int i=0;i<=top;i++)if (read[i].gettag()==0)file.write((char *)&read[i],sizeof(read[i]));file.close();}};void RData::Studentdata(){char choice;char rname[20];int Studentid;Student *r;while (choice!='0'){cout<<"┏━━━━━━━━━━━━━┓\n";cout<<"┃读者维护┃\n";cout<<"┃━━━━━━━━━━━━━┃\n"; cout<<"┃1.新增┃\n";cout<<"┃━━━━━━━━━━━━━┃\n"; cout<<"┃2.更改┃\n";cout<<"┃━━━━━━━━━━━━━┃\n"; cout<<"┃3.删除┃\n";cout<<"┃━━━━━━━━━━━━━┃\n"; cout<<"┃4.查找┃\n";cout<<"┃━━━━━━━━━━━━━┃\n"; cout<<"┃5.显示┃\n";cout<<"┃━━━━━━━━━━━━━┃\n"; cout<<"┃6.全删┃\n";cout<<"┃━━━━━━━━━━━━━┃\n"; cout<<"┃0.退出┃\n";cout<<"┗━━━━━━━━━━━━━┛\n"; //cout<<cin >> choice;switch (choice){case '1':cout << "输入读者编号:";cin >> Studentid;cout << "输入读者姓名:";cin >> rname;addStudent (Studentid,rname); break;case '2':cout << "输入读者编号:";cin >> Studentid;r=equal(Studentid);if (r==NULL){cout << "该读者不存在"<<endl; break;}cout << "输入新的姓名:";cin >> rname;r->setname(rname);break;case '3':cout << "输入读者编号:";cin >> Studentid;r=equal(Studentid);if (r==NULL){cout <<"该读者不存在" << endl; break;}r->delbook();break;case '4':cout << "读入读者编号:";cin >> Studentid;r=equal(Studentid);if (r==NULL){cout <<"该读者不存在"<< endl; break;}r->output();break;case '5':output();break;case '6':clear();break;default:cout<<"退出:\n";system("cls");break; }}}class Book{private:int tag;int number;char name[20];int onshelf;public:Book(){}char *getname(){return name;}int getnumber(){return number;}int gettag(){return tag;}void setname(char na[]) {strcpy(name,na);}void delbook(){tag=1;}void addbook(int n,char *na) {tag=0;number=n;strcpy(name,na);onshelf=1;}int borrowbook()//借书操作{if (onshelf==1){onshelf=0;return 1;}return 0;}void retbook()//还书操作{onshelf=1;}void output()//输出图书{cout << setw(6) << number << setw(18) << name << setw(10) <<(onshelf==1? "在架":"已借") <<endl;}};class BDatabase{private:int top;Book book[Maxb]; //图书记录public:BDatabase()//构造函数,将book.txt读到book[]中{Book b;top=-1;fstream file("book.txt",ios::in);while (1){file.read((char *)&b,sizeof(b));if (!file) break;top++;book[top]=b;}file.close();}void clear(){top=-1;}int addbook(int n,char *na){Book *p=equal(n);if (NULL==p){top++;book[top].addbook(n,na);return 1;}return 0;}Book *equal(int bookid){for (int i=0;i<=top;i++)if (book[i].getnumber()==bookid &&book[i].gettag()==0) {return &book[i];}return NULL;}void bookdata();void output(){for (int i=0;i<=top;i++)if (book[i].gettag()==0)book[i].output();}~BDatabase()//析构函数,将book[]写到book.txt文件中{fstream file("book.txt",ios::out);for (int i=0;i<=top;i++)if (book[i].gettag()==0)file.write((char *)&book[i],sizeof(book[i]));file.close();}};void BDatabase::bookdata(){char choice;char bname[40];int bookid;Book *b;while (choice!='0'){cout<<"┏━━━━━━━━━━━━━┓\n"; cout<<"┃图书维护┃\n";cout<<"┃━━━━━━━━━━━━━┃\n"; cout<<"┃1.新增┃\n";cout<<"┃━━━━━━━━━━━━━┃\n"; cout<<"┃2.更改┃\n";cout<<"┃━━━━━━━━━━━━━┃\n"; cout<<"┃3.删除┃\n";cout<<"┃━━━━━━━━━━━━━┃\n"; cout<<"┃4.查找┃\n";cout<<"┃━━━━━━━━━━━━━┃\n"; cout<<"┃5.显示┃\n";cout<<"┃━━━━━━━━━━━━━┃\n"; cout<<"┃6.全删┃\n";cout<<"┃━━━━━━━━━━━━━┃\n"; cout<<"┃0.退出┃\n";cout<<"┗━━━━━━━━━━━━━┛\n"; cin >> choice;switch (choice){case '1':cout << "输入图书编号:"<<endl;cin >> bookid;cout << "输入图书书名:"<<endl;cin >> bname;addbook(bookid,bname);break;case '2':cout << "输入图书编号:"<<endl;cin >> bookid;b=equal(bookid);if (b==NULL){cout << "该图书不存在"<<endl;break;}cout << "输入新的书名:"<<endl;cin >> bname;b->setname(bname);break;case '3':cout <<"读入图书编号:"<<endl; cin >> bookid;b=equal(bookid);if (b==NULL){cout <<"该图书不存在" << endl; break;}b->delbook();break;case '4':cout << "读入图书编号:"<<endl; cin >> bookid;b=equal(bookid);if (b==NULL){cout <<"该图书不存在"<< endl;break;}b->output();break;case '5':output();break;case '6':clear();break;default:cout<<"退出\n"; system("cls"); break; }}}void main(){char choice;int bookid,Studentid;RData StudentDB;Student *r;BDatabase BookDB;Book *b;while(choice!='0'){cout<<"┏━━━━━━━━━━━━━┓\n"; cout<<"┃图书管理系统┃\n";cout<<"┃━━━━━━━━━━━━━┃\n"; cout<<"┃1.借书┃\n";cout<<"┃━━━━━━━━━━━━━┃\n"; cout<<"┃2.还书┃\n";cout<<"┃━━━━━━━━━━━━━┃\n"; cout<<"┃3.图书维护┃\n";cout<<"┃━━━━━━━━━━━━━┃\n"; cout<<"┃4.读者维护┃\n";cout<<"┃━━━━━━━━━━━━━┃\n"; cout<<"┃0.退出┃\n";cout<<"┗━━━━━━━━━━━━━┛\n"; cin >> choice;switch (choice){case '1': system("cls");cout <<"借书读者编号:";cin >>Studentid;cout <<"图书编号:";cin >>bookid;r=StudentDB.equal(Studentid);//按编号查找if (NULL==r){cout <<"不存在该读者,不能借书"<< endl; break;}b=BookDB.equal(bookid);if (b==NULL){cout <<"不存在该图书,不能借书"<< endl; break;}if (b->borrowbook()==0){cout << "该图书已借出,不能借书"<< endl; break;}r->borrowbook(b->getnumber());system("cls");case '2':system("cls");cout<<"还书\n读者编号:";cin >>Studentid;cout << "图书编号:";cin >>bookid;r=StudentDB.equal(Studentid);if (r==NULL){cout <<"不存在该读者,不能还书" << endl; break;}b=BookDB.equal(bookid);if (b==NULL){cout <<"不存在该图书,不能还书" <<endl; break;}b->retbook();r->retbook(b->getnumber());case '3':system("cls"); BookDB.bookdata(); break;case '4':system("cls"); StudentDB.Studentdata(); break;default:cout<<"退出\n"; }}}。

图书馆管理系统 c语言编写

图书馆管理系统   c语言编写

#include "stdio.h"#include "stdlib.h"#include "conio.h"#include "string.h"#include "math.h"#define LEN sizeof(struct library)#define LEN1 sizeof(struct reader)#define NULL 0struct library//图书馆结构体{int shuhao,xcl;char name[20],author[20],chuban[20];struct library *next;};struct reader//读者结构体{int zhenghao;char mingzi[20],riqi[20],zname[20];struct reader *next;};void mainmenu() //显示主菜单{ system ("cls");printf("\n");printf("\t\t\t\t####################\n\n"); printf("\t\t\t\t 1.图书馆信息\n\n");printf("\t\t\t\t 2.借阅系统\n\n");printf("\t\t\t\t 3.退出系统\n\n");printf("\t\t\t 请按键选择,回车确定\n"); printf("\t\t\t\t######################\n"); printf("\n");return ;}void menu1() //显示图书馆信息菜单{ system ("cls");printf("");printf("\t\t\t\t####################\n\n"); printf("\t\t\t\t 1.采编入库\n\n");printf("\t\t\t\t 2.清除库存\n\n");printf("\t\t\t\t 3.图书查询\n\n");printf("\t\t\t\t 4.库存一览\n\n");printf("\t\t\t\t 5.返回上一层\n\n");printf("\t\t\t 请按键选择,回车确定\n");printf("\t\t\t\t#######################\n");printf("\n");return ;}void menu2() //显示查询菜单{ system ("cls");//printf("");printf("\t\t\t\t#######################\n");printf("\t\t\t\t 1.书号查询\n\n");printf("\t\t\t\t 2.书名查询\n\n");printf("\t\t\t\t 3.作者查询\n\n");printf("\t\t\t\t 4.出版社查询\n\n");printf("\t\t\t 请按键选择,回车确定\n");printf("\t\t\t\t#######################\n");//printf("");return ;}void main()//主函数,调用main1{void main1();main1();}void main1()//main1函数{void tsgxx();//声明void jieshuxitong();//声明char choose;mainmenu();scanf("%c",&choose);switch(choose)//功能函数选择{ case '1':tsgxx();break;case '2':jieshuxitong();break;case '3':system ("cls");printf("\n\n\n\n\n\n\n\n\n\t\t\t Byebye...");printf("\n\n\n\n\n\n\n\n\n\t\t\t 欢迎使用本软件!!!");getch();exit(0);system ("cls");break;}}void tsgxx()//图书馆信息函数{ void tsjinku();void shanchu();void chaxunts();void xianshikucun();//函数声明char choose;menu1();//调用菜单函数scanf("%c",&choose);scanf("%c",&choose);for (;;)switch(choose)//功能函数选择{ case '1':tsjinku();break;case '2':shanchu();break;case '3':chaxunts();break;case '4':xianshikucun();break;case '5':main1();break;}}int tjzs()//统计文本个数函数{ FILE *fp;int tshuhao=0,txcl=0,n;char tname[20]={'\0'},tauthor[20]={'\0'},tchuban[20]={'\0'};fp=fopen("library.txt","r");//打开文件for (n=0;!feof(fp);n++)//逐个读文件fscanf(fp,"%d %s %s %s %d",&tshuhao,tname,tauthor,tchuban,&txcl);n--;fclose(fp);//关闭文件return (n);//返回个数}int tjdzzs()//统计文本个数函数{ FILE *fp;int zhenghao=0,n;char mingzi[20]={'\0'},riqi[20]={'\0'},zname[20]={'\0'};fp=fopen("reader.txt","r");//打开文件for (n=0;!feof(fp);n++)//逐个读文件fscanf(fp,"%d %s %s %s ",&zhenghao,&mingzi,&riqi,&zname);fclose(fp);//关闭文件return (n);//返回个数}void tsjinku()//图书进库函数{ FILE *fp;int shuhao=0,xcl=0,n=0;char name[20]={'\0'},author[20]={'\0'},chuban[20]={'\0'};char hitkey;system ("cls");if ((fp=fopen("library.txt","r"))==NULL)//打开图书馆文件,不存在此文件则新建{ fp=fopen("library.txt","w");fclose(fp);}fp=fopen("library.txt","a");printf("\n\n\n\n\n\n\t\t\t请按以下格式输入图书信息:\t\t\t\t\t\t 书号书名作者出版社进库量\n请输入:");//按格式输入图书馆信息for (;hitkey!=27;)//循环输入{ if (n!=0)printf("请输入:");scanf("%d%s%s%s%d",&shuhao,name,author,chuban,&xcl);fprintf(fp,"%d %s %s %s %d\n",shuhao,name,author,chuban,xcl);printf("继续输入请按回车,结束输入请按esc\n");n++;hitkey=getch();for (;hitkey!=13&&hitkey!=27;)hitkey=getch();}fclose(fp);printf("\n\n\n\n\n\n\t\t\t保存成功,按任意键返回上一层!");getch();tsgxx();//返回上一层}void shanchu()//删除图书信息函数{ struct library *head=NULL;struct library *p,*p1,*p2;int tshuhao=0,txcl=0,n=0,j,i;char tname[20]={'\0'},tauthor[20]={'\0'},tchuban[20]={'\0'}, ttname[20]={'\0'};char hitkey;FILE *fp;if ((fp=fopen("library.txt","r"))==NULL)//打开文件{ system ("cls");printf("\n\n\n\n\n\n\n\n\n\t\t\t记录文件不存在!按任意键返回...");getch();tsgxx();}else{ system ("cls");printf("\n\n\n\n\n\n\n\n\t\t请输入你要删除的书名:");//输入删除图书书名scanf("%s",&ttname);printf("\t\t确认删除请回车,取消请按esc\n");hitkey=getch();for (;hitkey!=13&&hitkey!=27;)hitkey=getch();if (hitkey==27)tsgxx();fp=fopen("library.txt","r");for (j=0;!feof(fp);)//读文件夹信息,统计个数{ j++;fscanf(fp,"%d%s%s%s%d",&tshuhao,tname,tauthor,tchuban,&txcl);}fclose(fp);fp=fopen("library.txt","r");for (i=1;i<j;i++){ fscanf(fp,"%d%s%s%s%d",&tshuhao,tname,tauthor,tchuban,&txcl); if (strcmp(ttname,tname))//比较名字,将不同名字的信息复制到链表{ n++;if (n==1)//建立链表{ p1=p2=(struct library*)malloc(LEN);head=p1;}else{ p2->next=p1;p2=p1;p1=(struct library*)malloc(LEN);//新建链表}p1->shuhao=tshuhao;//复制书号strcpy(p1->name,tname);//复制书名strcpy(p1->author,tauthor);//复制作者名子strcpy(p1->chuban,tchuban);//复制出版社p1->xcl=txcl;//复制个数}}if (n==0){ head=NULL;}else{p2->next=p1;p1->next=NULL;fclose(fp);}}fp=fopen("library.txt","w");//清空文件fclose(fp);fp=fopen("library.txt","a");//追加文件p=head;for (;p!=NULL;)//把链表内容覆盖到文件{fprintf(fp,"%d %s %s %s %d \n",p->shuhao,p->name,p->author,p->chuban,p->xcl);p=p->next;}fclose(fp);//关闭文件system ("cls");printf("\n\n\n\n\n\n\n\n\t\t 删除成功\n\t\t按任意键返回上一层\n");getch();//返回上一层tsgxx();}void chaxunts()//查询函数{FILE *fp;char choose;int ttshuhao=0,tshuhao=0,txcl=0,n=0,k=0,i,l;chartname[20]={'\0'},ttauthor[20]={'\0'},tauthor[20]={'\0'},ttchuban[20]={'\0'},tchuban[20]={'\0'}, ttname[20]={'\0'};if ((fp=fopen("library.txt","r"))==NULL)//打开文件{ system ("cls");printf("\n\n\n\n\n\n\n\n\n\t\t\t记录文件不存在!按任意键返回...");getch();tsgxx();}l=tjzs();//获得文件个数menu2();//调用菜单函数scanf("%c",&choose);scanf("%c",&choose);//选择查询方式if (choose=='5')return;else if (choose=='1')//书号查询{ system ("cls");printf("请输入书号:");scanf("%d",&ttshuhao);}elseif (choose=='2')//书名查询{ system ("cls");printf("请输入书名:");scanf("%s",ttname);}elseif (choose=='3')//作者查询{ system ("cls");printf("请输入作者:");scanf("%s",ttauthor);}elseif (choose=='4')//出版社查询{ system ("cls");printf("请输入出版社:");scanf("%s",ttchuban);}system ("cls");for (i=0;i<l;i++){ fscanf(fp,"%d%s%s%s%d",&tshuhao,tname,tauthor,tchuban,&txcl);//读文件信息if(ttshuhao==tshuhao||!strcmp(ttname,tname)||!strcmp(ttauthor,tauthor)||!strcmp(ttchuban,tch uban))//输出查询信息{if (k==0){printf("\t\t\t\t查询结果:\n\n");printf("\t 书号书名作者出版社现存量\n");}printf("\t %-4d %-8s %-8s %-8s %-4d \n",tshuhao,tname,tauthor,tchuban,txcl);k++;}}if (k==0)//文件夹为空则输出无记录{ system ("cls");printf("\n\n\n\n\n\n\n\t\t\t\t无符合记录!\n");getch();tsgxx();}fclose(fp);getch();//返回tsgxx();}void xianshikucun()//现实库存信息{FILE *fp;int shuhao=0,xcl=0,n=0,i=0,j=0;char name[20]={'\0'},author[20]={'\0'},chuban[20]={'\0'};if ((fp=fopen("library.txt","r"))==NULL)//打开文件夹{system ("cls");printf("\n\n\n\n\n\n\n\n\n\t\t\t记录文件不存在!");n= tjzs();if (n==0){ system ("cls");printf("\n\n\n\n\n\n\n\n\n\t\t\t无任何记录!");}fp=fopen("library.txt","r");system ("cls");printf("************************************************************************* *******");printf("\t 书号书名作者出版社库存量\n");printf("************************************************************************* *******");for (i=0;i<n;i++)//输出信息{fscanf(fp,"%d%s%s%s%d",&shuhao,name,author,chuban,&xcl);printf("\t %-4d %-8s %-8s %-8s %-4d \n",shuhao,name,author,chuban,xcl);}fclose(fp);printf("\t\t\t\t按任意键返回\n");getch();//返回tsgxx();}void menu3() //显示借书系统主菜单{ system ("cls");//printf("*********************************************************************** *********");printf("\t\t\t\t#######################\n");printf("\t\t\t\t 1.借书登记\n\n");printf("\t\t\t\t 2.还书登记\n\n");printf("\t\t\t\t 3.借阅情况查看\n\n");printf("\t\t\t\t 4.返回上一层\n\n");printf("\t\t\t 请按键选择,回车确定\n");printf("\t\t\t\t#######################\n");//printf("*********************************************************************** *********");return ;}void jieshuxitong()//借书系统函数{ void jieshu();void huanshu();void duzheyilang();//函数声明char choose;menu3();scanf("%c",&choose);scanf("%c",&choose);//选择功能for (;;)switch(choose)//调用函数{ case '1':jieshu();break;case '2':huanshu();break;case '3':duzheyilang();break;case '4':main1();break;}}void jieshu()//借书函数{FILE *fp,*fp3;struct library *head=NULL;struct library *p,*p1,*p2;int tshuhao=0,txcl=0,i,loop,zhenghao=0,n=0,k=0,t=0,flag=0;char tname[20]={'\0'},tauthor[20]={'\0'},tchuban[20]={'\0'}, ttname[20]={'\0'},mingzi[20]={'\0'},riqi[20]={'\0'},zname[20]={'\0'};char hitkey=0;system ("cls");{if ((fp=fopen("library.txt","r"))==NULL)//打开图书馆文件{system ("cls");printf("\n\n\n\n\n\n\n\n\n\t\t 图书馆无库存!按任意键退出!");getch();exit (0);}else{{printf("\n\n\n\n\n\n\t\t\t请输入借阅书名:\t\t\t\t\t\t \n请输入:");//输入书名scanf("%s",zname);k= tjzs();//统计图书馆文件个数for (i=0;i<k;i++)//读入图书馆信息,存储到链表{fscanf(fp,"%d%s%s%s%d",&tshuhao,tname,tauthor,tchuban,&txcl);n++;if (n==1){ p1=p2=(struct library*)malloc(LEN);head=p1;}else{ p2->next=p1;p2=p1;p1=(struct library*)malloc(LEN);//新建链表}p1->shuhao=tshuhao;//复制书号strcpy(p1->name,tname);//复制书名strcpy(p1->author,tauthor);//复制作者strcpy(p1->chuban,tchuban);//复制出版社p1->xcl=txcl;//复制现存量}if (n==0)head=NULL;else{p2->next=p1;p1->next=NULL;fclose(fp);}}}p=head;for (;p!=NULL;)//读链表{if(!(strcmp(p->name,zname)))//名字相同{flag=1;//标记取1loop=p->xcl;//现存量减1(p->xcl)--;}p=p->next;}if(flag&&(loop>0))//存在借书书名且现存量大于0{ fp=fopen("library.txt","w");fclose(fp);fp=fopen("library.txt","a");p=head;for (;p!=NULL;){fprintf(fp,"%d %s %s %s %d \n",p->shuhao,p->name,p->author,p->chuban,p->xcl);p=p->next;}free(p);//把链表内容覆盖文件fclose(fp);}if(flag&&(loop>0))//存在借书书名且现存量大于0{{if ((fp3=fopen("reader.txt","r"))==NULL)//建读者文件夹{ fp3=fopen("reader.txt","w");fclose(fp3);}fp3=fopen("reader.txt","a");}{{ if (n!=0)printf("\n\n\n\n\n\n\t\t\t请按以下格式输入读者信息:\t\t\t\t\t\t 证号姓名归还日期借书书名\n请输入:");//录入读者信息scanf("%d %s %s %s",&zhenghao,&mingzi[20],&riqi[20],&zname[20]);fprintf(fp3,"%d %s %s %s \n",zhenghao,&mingzi[20],&riqi[20],&zname[20]);fp=fopen("library.txt","w");//删除图书馆文件信息fclose(fp);fp=fopen("library.txt","a");//重新追加信息p=head;for (;p!=NULL;)//把链表内容覆盖图书馆文件{fprintf(fp,"%d %s %s %s %d \n",p->shuhao,p->name,p->author,p->chuban,p->xcl);p=p->next;}fclose(fp);fclose(fp3);printf("成功!按任意键返回\n");getch();//返回jieshuxitong();}}jieshuxitong();}elseprintf("此书已被借完!按任意键返回!");//否则输出此书已被接完getch();//返回jieshuxitong();}}void huanshu()//还书函数{FILE *fp,*fp3;struct reader *head=NULL;struct reader *p,*p1,*p2;struct library *lhead1=NULL;struct library *zp1,*lp1,*lp2;int tshuhao=0,txcl=0,i;char tname[20]={'\0'},tauthor[20]={'\0'},tchuban[20]={'\0'}, ttname[20]={'\0'};int ttzhenghao=0,tzhenghao=0,n=0,k=0,t=0,flag=0;char tmingzi[20]={'\0'},triqi[20]={'\0'},tzname[20]={'\0'},ttzname[20]={'\0'};char hitkey=0;system ("cls");{if ((fp=fopen("reader.txt","r"))==NULL)//不存在读者文件,则输出不能还书{system ("cls");printf("\n\n\n\n\n\n\n\n\n\t\t 不存在借书者!按任意键退出!");getch();exit (0);}else{{printf("\n\n\n\n\n\n\t\t\t请输入读者证号和书名:\t\t\t\t\t\t \n请输入:");scanf("%d %s",&ttzhenghao,ttzname);//输入还书证号和书名k= tjdzzs();//获取读者文件夹信息个数for (i=0;i<k;i++)//读取读者文件夹信息{fscanf(fp,"%d%s%s%s\n ",&tzhenghao,tmingzi,triqi,tzname);if((ttzhenghao==tzhenghao)&&!strcmp(ttzname,tzname))//如果证号书名存在,则标记为1flag=1;}fclose(fp);fp=fopen("reader.txt","r");//打开读者文件if(flag){for (i=0;i<k;i++)//将读者文件复制到链表{fscanf(fp,"%d%s%s%s\n ",&tzhenghao,tmingzi,triqi,tzname);//读取文件信息if(!((ttzhenghao==tzhenghao)&&!strcmp(ttzname,tzname))){ n++;if (n==1){ p1=p2=(struct reader*)malloc(LEN1);//新建链表head=p1;}else{ p2->next=p1;p2=p1;p1=(struct reader*)malloc(LEN1);//新建链表}p1->zhenghao=tzhenghao;//复制证号strcpy(p1->mingzi,tmingzi);//复制读者名字strcpy(p1->riqi,triqi);//复制日期strcpy(p1->zname,tzname);//复制书名}}if (n==0)head=NULL;else{p2->next=p1;p1->next=NULL;fclose(fp);}fp=fopen("reader.txt","w");//清空读者文件fclose(fp);fp=fopen("reader.txt","a");//追加信息p=head;for (;p!=NULL;)//把链表内容覆盖读者文件{fprintf(fp,"%d %s %s %s \n",p->zhenghao,p->mingzi,p->riqi,p->zname);p=p->next;}free(p);fclose(fp);}}}}if(flag)//标记为1,既还书时{{{printf("确认还书请按回车!");for (;hitkey!=13&&hitkey!=27;)hitkey=getch();if (hitkey==13)printf("成功!按任意键返回!");n=0;flag=0;fp3=fopen("library.txt","r");//打开图书馆文件k= tjzs();//获取图书馆文件个数for (i=0;i<k;i++)//将图书馆文件复制到链表{fscanf(fp3,"%d%s%s%s%d",&tshuhao,tname,tauthor,tchuban,&txcl);//读取信息n++;if (n==1){ lp1=lp2=(struct library*)malloc(LEN);//新建链表lhead1=lp1;}else{ lp2->next=lp1;lp2=lp1;lp1=(struct library*)malloc(LEN);//新建链表}lp1->shuhao=tshuhao;//复制书号strcpy(lp1->name,tname);//复制书名strcpy(lp1->author,tauthor);//复制作者strcpy(lp1->chuban,tchuban);//复制出版社lp1->xcl=txcl; //复制现存量}if (n==0){ lhead1=NULL;}else{lp2->next=lp1;lp1->next=NULL;fclose(fp3);}}}zp1=lhead1;for (;zp1!=NULL;){if(!(strcmp(zp1->name,ttzname)))//寻找书名相同++(zp1->xcl);//现存量加1zp1=zp1->next;}fp3=fopen("library.txt","w");//清空图书馆文件fclose(fp);fp3=fopen("library.txt","a");//追加信息zp1=lhead1;for (;zp1!=NULL;)//把链表内容覆盖图书馆文件{fprintf(fp3,"%d %s %s %s %d \n",zp1->shuhao,zp1->name,zp1->author,zp1->chuban,zp1->xcl);//录入信息zp1=zp1->next;}fclose(fp3);getch();//返回jieshuxitong();}elseprintf("不存在此信息!按任意键返回!");getch();//返回jieshuxitong();}void duzheyilang()//显示借书情况函数{FILE *fp;int zhenghao=0,xcl=0,n=0,i=0,j=0;char mingzi[20]={'\0'},riqi[20]={'\0'},zname[20]={'\0'};if ((fp=fopen("reader.txt","r"))==NULL)//打开读者文件夹{system ("cls");printf("\n\n\n\n\n\n\n\n\n\t\t\t记录文件不存在!");}n=tjdzzs();if (n==0){ system ("cls");printf("\n\n\n\n\n\n\n\n\n\t\t\t无任何记录!");}fp=fopen("reader.txt","r");system ("cls");printf("************************************************************************* *******");printf("\t 证号读者姓名还书日期书名\n");printf("************************************************************************* *******");for (i=0;i<n;i++)//输出文件信息{fscanf(fp,"%d%s%s%s\n ",&zhenghao,mingzi,riqi,zname);printf("\t %-4d %-8s %-8s %-8s \n", zhenghao,mingzi,riqi,zname);}fclose(fp);printf("\t\t\t\t按任意键返回\n");getch();//返回jieshuxitong();}。

图书馆管理系统 c语言编写

图书馆管理系统   c语言编写

#include "stdio.h"#include "stdlib.h"#include "conio.h"#include "string.h"#include "math.h"#define LEN sizeof(struct library)#define LEN1 sizeof(struct reader)#define NULL 0struct library//图书馆结构体{int shuhao,xcl;char name[20],author[20],chuban[20];struct library *next;};struct reader//读者结构体{int zhenghao;char mingzi[20],riqi[20],zname[20];struct reader *next;};void mainmenu() //显示主菜单{ system ("cls");printf("\n");printf("\t\t\t\t####################\n\n"); printf("\t\t\t\t 1.图书馆信息\n\n");printf("\t\t\t\t 2.借阅系统\n\n");printf("\t\t\t\t 3.退出系统\n\n");printf("\t\t\t 请按键选择,回车确定\n"); printf("\t\t\t\t######################\n"); printf("\n");return ;}void menu1() //显示图书馆信息菜单{ system ("cls");printf("");printf("\t\t\t\t####################\n\n"); printf("\t\t\t\t 1.采编入库\n\n");printf("\t\t\t\t 2.清除库存\n\n");printf("\t\t\t\t 3.图书查询\n\n");printf("\t\t\t\t 4.库存一览\n\n");printf("\t\t\t\t 5.返回上一层\n\n");printf("\t\t\t 请按键选择,回车确定\n");printf("\t\t\t\t#######################\n");printf("\n");return ;}void menu2() //显示查询菜单{ system ("cls");//printf("");printf("\t\t\t\t#######################\n");printf("\t\t\t\t 1.书号查询\n\n");printf("\t\t\t\t 2.书名查询\n\n");printf("\t\t\t\t 3.作者查询\n\n");printf("\t\t\t\t 4.出版社查询\n\n");printf("\t\t\t 请按键选择,回车确定\n");printf("\t\t\t\t#######################\n");//printf("");return ;}void main()//主函数,调用main1{void main1();main1();}void main1()//main1函数{void tsgxx();//声明void jieshuxitong();//声明char choose;mainmenu();scanf("%c",&choose);switch(choose)//功能函数选择{ case '1':tsgxx();break;case '2':jieshuxitong();break;case '3':system ("cls");printf("\n\n\n\n\n\n\n\n\n\t\t\t Byebye...");printf("\n\n\n\n\n\n\n\n\n\t\t\t 欢迎使用本软件!!!");getch();exit(0);system ("cls");break;}}void tsgxx()//图书馆信息函数{ void tsjinku();void shanchu();void chaxunts();void xianshikucun();//函数声明char choose;menu1();//调用菜单函数scanf("%c",&choose);scanf("%c",&choose);for (;;)switch(choose)//功能函数选择{ case '1':tsjinku();break;case '2':shanchu();break;case '3':chaxunts();break;case '4':xianshikucun();break;case '5':main1();break;}}int tjzs()//统计文本个数函数{ FILE *fp;int tshuhao=0,txcl=0,n;char tname[20]={'\0'},tauthor[20]={'\0'},tchuban[20]={'\0'};fp=fopen("library.txt","r");//打开文件for (n=0;!feof(fp);n++)//逐个读文件fscanf(fp,"%d %s %s %s %d",&tshuhao,tname,tauthor,tchuban,&txcl);n--;fclose(fp);//关闭文件return (n);//返回个数}int tjdzzs()//统计文本个数函数{ FILE *fp;int zhenghao=0,n;char mingzi[20]={'\0'},riqi[20]={'\0'},zname[20]={'\0'};fp=fopen("reader.txt","r");//打开文件for (n=0;!feof(fp);n++)//逐个读文件fscanf(fp,"%d %s %s %s ",&zhenghao,&mingzi,&riqi,&zname);fclose(fp);//关闭文件return (n);//返回个数}void tsjinku()//图书进库函数{ FILE *fp;int shuhao=0,xcl=0,n=0;char name[20]={'\0'},author[20]={'\0'},chuban[20]={'\0'};char hitkey;system ("cls");if ((fp=fopen("library.txt","r"))==NULL)//打开图书馆文件,不存在此文件则新建{ fp=fopen("library.txt","w");fclose(fp);}fp=fopen("library.txt","a");printf("\n\n\n\n\n\n\t\t\t请按以下格式输入图书信息:\t\t\t\t\t\t 书号书名作者出版社进库量\n请输入:");//按格式输入图书馆信息for (;hitkey!=27;)//循环输入{ if (n!=0)printf("请输入:");scanf("%d%s%s%s%d",&shuhao,name,author,chuban,&xcl);fprintf(fp,"%d %s %s %s %d\n",shuhao,name,author,chuban,xcl);printf("继续输入请按回车,结束输入请按esc\n");n++;hitkey=getch();for (;hitkey!=13&&hitkey!=27;)hitkey=getch();}fclose(fp);printf("\n\n\n\n\n\n\t\t\t保存成功,按任意键返回上一层!");getch();tsgxx();//返回上一层}void shanchu()//删除图书信息函数{ struct library *head=NULL;struct library *p,*p1,*p2;int tshuhao=0,txcl=0,n=0,j,i;char tname[20]={'\0'},tauthor[20]={'\0'},tchuban[20]={'\0'}, ttname[20]={'\0'};char hitkey;FILE *fp;if ((fp=fopen("library.txt","r"))==NULL)//打开文件{ system ("cls");printf("\n\n\n\n\n\n\n\n\n\t\t\t记录文件不存在!按任意键返回...");getch();tsgxx();}else{ system ("cls");printf("\n\n\n\n\n\n\n\n\t\t请输入你要删除的书名:");//输入删除图书书名scanf("%s",&ttname);printf("\t\t确认删除请回车,取消请按esc\n");hitkey=getch();for (;hitkey!=13&&hitkey!=27;)hitkey=getch();if (hitkey==27)tsgxx();fp=fopen("library.txt","r");for (j=0;!feof(fp);)//读文件夹信息,统计个数{ j++;fscanf(fp,"%d%s%s%s%d",&tshuhao,tname,tauthor,tchuban,&txcl);}fclose(fp);fp=fopen("library.txt","r");for (i=1;i<j;i++){ fscanf(fp,"%d%s%s%s%d",&tshuhao,tname,tauthor,tchuban,&txcl); if (strcmp(ttname,tname))//比较名字,将不同名字的信息复制到链表{ n++;if (n==1)//建立链表{ p1=p2=(struct library*)malloc(LEN);head=p1;}else{ p2->next=p1;p2=p1;p1=(struct library*)malloc(LEN);//新建链表}p1->shuhao=tshuhao;//复制书号strcpy(p1->name,tname);//复制书名strcpy(p1->author,tauthor);//复制作者名子strcpy(p1->chuban,tchuban);//复制出版社p1->xcl=txcl;//复制个数}}if (n==0){ head=NULL;}else{p2->next=p1;p1->next=NULL;fclose(fp);}}fp=fopen("library.txt","w");//清空文件fclose(fp);fp=fopen("library.txt","a");//追加文件p=head;for (;p!=NULL;)//把链表内容覆盖到文件{fprintf(fp,"%d %s %s %s %d \n",p->shuhao,p->name,p->author,p->chuban,p->xcl);p=p->next;}fclose(fp);//关闭文件system ("cls");printf("\n\n\n\n\n\n\n\n\t\t 删除成功\n\t\t按任意键返回上一层\n");getch();//返回上一层tsgxx();}void chaxunts()//查询函数{FILE *fp;char choose;int ttshuhao=0,tshuhao=0,txcl=0,n=0,k=0,i,l;chartname[20]={'\0'},ttauthor[20]={'\0'},tauthor[20]={'\0'},ttchuban[20]={'\0'},tchuban[20]={'\0'}, ttname[20]={'\0'};if ((fp=fopen("library.txt","r"))==NULL)//打开文件{ system ("cls");printf("\n\n\n\n\n\n\n\n\n\t\t\t记录文件不存在!按任意键返回...");getch();tsgxx();}l=tjzs();//获得文件个数menu2();//调用菜单函数scanf("%c",&choose);scanf("%c",&choose);//选择查询方式if (choose=='5')return;else if (choose=='1')//书号查询{ system ("cls");printf("请输入书号:");scanf("%d",&ttshuhao);}elseif (choose=='2')//书名查询{ system ("cls");printf("请输入书名:");scanf("%s",ttname);}elseif (choose=='3')//作者查询{ system ("cls");printf("请输入作者:");scanf("%s",ttauthor);}elseif (choose=='4')//出版社查询{ system ("cls");printf("请输入出版社:");scanf("%s",ttchuban);}system ("cls");for (i=0;i<l;i++){ fscanf(fp,"%d%s%s%s%d",&tshuhao,tname,tauthor,tchuban,&txcl);//读文件信息if(ttshuhao==tshuhao||!strcmp(ttname,tname)||!strcmp(ttauthor,tauthor)||!strcmp(ttchuban,tch uban))//输出查询信息{if (k==0){printf("\t\t\t\t查询结果:\n\n");printf("\t 书号书名作者出版社现存量\n");}printf("\t %-4d %-8s %-8s %-8s %-4d \n",tshuhao,tname,tauthor,tchuban,txcl);k++;}}if (k==0)//文件夹为空则输出无记录{ system ("cls");printf("\n\n\n\n\n\n\n\t\t\t\t无符合记录!\n");getch();tsgxx();}fclose(fp);getch();//返回tsgxx();}void xianshikucun()//现实库存信息{FILE *fp;int shuhao=0,xcl=0,n=0,i=0,j=0;char name[20]={'\0'},author[20]={'\0'},chuban[20]={'\0'};if ((fp=fopen("library.txt","r"))==NULL)//打开文件夹{system ("cls");printf("\n\n\n\n\n\n\n\n\n\t\t\t记录文件不存在!");n= tjzs();if (n==0){ system ("cls");printf("\n\n\n\n\n\n\n\n\n\t\t\t无任何记录!");}fp=fopen("library.txt","r");system ("cls");printf("************************************************************************* *******");printf("\t 书号书名作者出版社库存量\n");printf("************************************************************************* *******");for (i=0;i<n;i++)//输出信息{fscanf(fp,"%d%s%s%s%d",&shuhao,name,author,chuban,&xcl);printf("\t %-4d %-8s %-8s %-8s %-4d \n",shuhao,name,author,chuban,xcl);}fclose(fp);printf("\t\t\t\t按任意键返回\n");getch();//返回tsgxx();}void menu3() //显示借书系统主菜单{ system ("cls");//printf("*********************************************************************** *********");printf("\t\t\t\t#######################\n");printf("\t\t\t\t 1.借书登记\n\n");printf("\t\t\t\t 2.还书登记\n\n");printf("\t\t\t\t 3.借阅情况查看\n\n");printf("\t\t\t\t 4.返回上一层\n\n");printf("\t\t\t 请按键选择,回车确定\n");printf("\t\t\t\t#######################\n");//printf("*********************************************************************** *********");return ;}void jieshuxitong()//借书系统函数{ void jieshu();void huanshu();void duzheyilang();//函数声明char choose;menu3();scanf("%c",&choose);scanf("%c",&choose);//选择功能for (;;)switch(choose)//调用函数{ case '1':jieshu();break;case '2':huanshu();break;case '3':duzheyilang();break;case '4':main1();break;}}void jieshu()//借书函数{FILE *fp,*fp3;struct library *head=NULL;struct library *p,*p1,*p2;int tshuhao=0,txcl=0,i,loop,zhenghao=0,n=0,k=0,t=0,flag=0;char tname[20]={'\0'},tauthor[20]={'\0'},tchuban[20]={'\0'}, ttname[20]={'\0'},mingzi[20]={'\0'},riqi[20]={'\0'},zname[20]={'\0'};char hitkey=0;system ("cls");{if ((fp=fopen("library.txt","r"))==NULL)//打开图书馆文件{system ("cls");printf("\n\n\n\n\n\n\n\n\n\t\t 图书馆无库存!按任意键退出!");getch();exit (0);}else{{printf("\n\n\n\n\n\n\t\t\t请输入借阅书名:\t\t\t\t\t\t \n请输入:");//输入书名scanf("%s",zname);k= tjzs();//统计图书馆文件个数for (i=0;i<k;i++)//读入图书馆信息,存储到链表{fscanf(fp,"%d%s%s%s%d",&tshuhao,tname,tauthor,tchuban,&txcl);n++;if (n==1){ p1=p2=(struct library*)malloc(LEN);head=p1;}else{ p2->next=p1;p2=p1;p1=(struct library*)malloc(LEN);//新建链表}p1->shuhao=tshuhao;//复制书号strcpy(p1->name,tname);//复制书名strcpy(p1->author,tauthor);//复制作者strcpy(p1->chuban,tchuban);//复制出版社p1->xcl=txcl;//复制现存量}if (n==0)head=NULL;else{p2->next=p1;p1->next=NULL;fclose(fp);}}}p=head;for (;p!=NULL;)//读链表{if(!(strcmp(p->name,zname)))//名字相同{flag=1;//标记取1loop=p->xcl;//现存量减1(p->xcl)--;}p=p->next;}if(flag&&(loop>0))//存在借书书名且现存量大于0{ fp=fopen("library.txt","w");fclose(fp);fp=fopen("library.txt","a");p=head;for (;p!=NULL;){fprintf(fp,"%d %s %s %s %d \n",p->shuhao,p->name,p->author,p->chuban,p->xcl);p=p->next;}free(p);//把链表内容覆盖文件fclose(fp);}if(flag&&(loop>0))//存在借书书名且现存量大于0{{if ((fp3=fopen("reader.txt","r"))==NULL)//建读者文件夹{ fp3=fopen("reader.txt","w");fclose(fp3);}fp3=fopen("reader.txt","a");}{{ if (n!=0)printf("\n\n\n\n\n\n\t\t\t请按以下格式输入读者信息:\t\t\t\t\t\t 证号姓名归还日期借书书名\n请输入:");//录入读者信息scanf("%d %s %s %s",&zhenghao,&mingzi[20],&riqi[20],&zname[20]);fprintf(fp3,"%d %s %s %s \n",zhenghao,&mingzi[20],&riqi[20],&zname[20]);fp=fopen("library.txt","w");//删除图书馆文件信息fclose(fp);fp=fopen("library.txt","a");//重新追加信息p=head;for (;p!=NULL;)//把链表内容覆盖图书馆文件{fprintf(fp,"%d %s %s %s %d \n",p->shuhao,p->name,p->author,p->chuban,p->xcl);p=p->next;}fclose(fp);fclose(fp3);printf("成功!按任意键返回\n");getch();//返回jieshuxitong();}}jieshuxitong();}elseprintf("此书已被借完!按任意键返回!");//否则输出此书已被接完getch();//返回jieshuxitong();}}void huanshu()//还书函数{FILE *fp,*fp3;struct reader *head=NULL;struct reader *p,*p1,*p2;struct library *lhead1=NULL;struct library *zp1,*lp1,*lp2;int tshuhao=0,txcl=0,i;char tname[20]={'\0'},tauthor[20]={'\0'},tchuban[20]={'\0'}, ttname[20]={'\0'};int ttzhenghao=0,tzhenghao=0,n=0,k=0,t=0,flag=0;char tmingzi[20]={'\0'},triqi[20]={'\0'},tzname[20]={'\0'},ttzname[20]={'\0'};char hitkey=0;system ("cls");{if ((fp=fopen("reader.txt","r"))==NULL)//不存在读者文件,则输出不能还书{system ("cls");printf("\n\n\n\n\n\n\n\n\n\t\t 不存在借书者!按任意键退出!");getch();exit (0);}else{{printf("\n\n\n\n\n\n\t\t\t请输入读者证号和书名:\t\t\t\t\t\t \n请输入:");scanf("%d %s",&ttzhenghao,ttzname);//输入还书证号和书名k= tjdzzs();//获取读者文件夹信息个数for (i=0;i<k;i++)//读取读者文件夹信息{fscanf(fp,"%d%s%s%s\n ",&tzhenghao,tmingzi,triqi,tzname);if((ttzhenghao==tzhenghao)&&!strcmp(ttzname,tzname))//如果证号书名存在,则标记为1flag=1;}fclose(fp);fp=fopen("reader.txt","r");//打开读者文件if(flag){for (i=0;i<k;i++)//将读者文件复制到链表{fscanf(fp,"%d%s%s%s\n ",&tzhenghao,tmingzi,triqi,tzname);//读取文件信息if(!((ttzhenghao==tzhenghao)&&!strcmp(ttzname,tzname))){ n++;if (n==1){ p1=p2=(struct reader*)malloc(LEN1);//新建链表head=p1;}else{ p2->next=p1;p2=p1;p1=(struct reader*)malloc(LEN1);//新建链表}p1->zhenghao=tzhenghao;//复制证号strcpy(p1->mingzi,tmingzi);//复制读者名字strcpy(p1->riqi,triqi);//复制日期strcpy(p1->zname,tzname);//复制书名}}if (n==0)head=NULL;else{p2->next=p1;p1->next=NULL;fclose(fp);}fp=fopen("reader.txt","w");//清空读者文件fclose(fp);fp=fopen("reader.txt","a");//追加信息p=head;for (;p!=NULL;)//把链表内容覆盖读者文件{fprintf(fp,"%d %s %s %s \n",p->zhenghao,p->mingzi,p->riqi,p->zname);p=p->next;}free(p);fclose(fp);}}}}if(flag)//标记为1,既还书时{{{printf("确认还书请按回车!");for (;hitkey!=13&&hitkey!=27;)hitkey=getch();if (hitkey==13)printf("成功!按任意键返回!");n=0;flag=0;fp3=fopen("library.txt","r");//打开图书馆文件k= tjzs();//获取图书馆文件个数for (i=0;i<k;i++)//将图书馆文件复制到链表{fscanf(fp3,"%d%s%s%s%d",&tshuhao,tname,tauthor,tchuban,&txcl);//读取信息n++;if (n==1){ lp1=lp2=(struct library*)malloc(LEN);//新建链表lhead1=lp1;}else{ lp2->next=lp1;lp2=lp1;lp1=(struct library*)malloc(LEN);//新建链表}lp1->shuhao=tshuhao;//复制书号strcpy(lp1->name,tname);//复制书名strcpy(lp1->author,tauthor);//复制作者strcpy(lp1->chuban,tchuban);//复制出版社lp1->xcl=txcl; //复制现存量}if (n==0){ lhead1=NULL;}else{lp2->next=lp1;lp1->next=NULL;fclose(fp3);}}}zp1=lhead1;for (;zp1!=NULL;){if(!(strcmp(zp1->name,ttzname)))//寻找书名相同++(zp1->xcl);//现存量加1zp1=zp1->next;}fp3=fopen("library.txt","w");//清空图书馆文件fclose(fp);fp3=fopen("library.txt","a");//追加信息zp1=lhead1;for (;zp1!=NULL;)//把链表内容覆盖图书馆文件{fprintf(fp3,"%d %s %s %s %d \n",zp1->shuhao,zp1->name,zp1->author,zp1->chuban,zp1->xcl);//录入信息zp1=zp1->next;}fclose(fp3);getch();//返回jieshuxitong();}elseprintf("不存在此信息!按任意键返回!");getch();//返回jieshuxitong();}void duzheyilang()//显示借书情况函数{FILE *fp;int zhenghao=0,xcl=0,n=0,i=0,j=0;char mingzi[20]={'\0'},riqi[20]={'\0'},zname[20]={'\0'};if ((fp=fopen("reader.txt","r"))==NULL)//打开读者文件夹{system ("cls");printf("\n\n\n\n\n\n\n\n\n\t\t\t记录文件不存在!");}n=tjdzzs();if (n==0){ system ("cls");printf("\n\n\n\n\n\n\n\n\n\t\t\t无任何记录!");}fp=fopen("reader.txt","r");system ("cls");printf("************************************************************************* *******");printf("\t 证号读者姓名还书日期书名\n");printf("************************************************************************* *******");for (i=0;i<n;i++)//输出文件信息{fscanf(fp,"%d%s%s%s\n ",&zhenghao,mingzi,riqi,zname);printf("\t %-4d %-8s %-8s %-8s \n", zhenghao,mingzi,riqi,zname);}fclose(fp);printf("\t\t\t\t按任意键返回\n");getch();//返回jieshuxitong();}。

图书管理系统 C语言程序

图书管理系统  C语言程序

#include<stdio.h>/*输入输出函数*/#include<string.h>/*字符串操作函数*/#include<stdlib.h>/*数值转换函数*///#include<conio.h>/*控制台输入输出函数*/#define MAX 100/*宏定义,参数为形参*/struct book/*图书信息结构定义*/{intnum;/*图书编号*/char name[100];/*书名*/char author[100];/*作者名*/char fn[100];/*分类号*/char place[100];/*出版单位*/char day[100];/*出版时间*/float money;/*价格*/}book[MAX],temp;main()/*主函数*/{void Input();/*输入*/void Output();/*输出*/void Find();/*查找*/void Delete();/*删除*/void Change();/*修改*/void Paixu();/*排序*/int n;printf("\n");printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");printf("※※\n");printf("※欢迎来河南工业大学图书信息管理系统※\n");printf("※※\n");printf("※※\n");printf("※★1.图书管理员系统★※\n");printf("※※\n");printf("※★2.读者系统★※\n");printf("※※\n");printf("※▲退出(输入其他数字)※\n");printf("※※\n");printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");printf("请输入选择项(1-2):");scanf("%d",&n);printf("\n\n\n\n");if(n==1){int m;printf("请输入图书管理系统的密码:");scanf("%d",&m);if(m==1002){for(;;)/*省略"初始化"、"条件表达式"和"增量"*/{int s;printf("\n");printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");printf("※※\n");printf("※欢迎来河南工业大学图书信息管理系统※\n");printf("※※\n");printf("※※\n");printf("※主菜单※\n");printf("※※\n");printf("※★1.图书信息录入★※\n");printf("※※\n");printf("※★2.图书信息浏览★※\n");printf("※※\n");printf("※★3.图书信息查询★※\n");printf("※※\n");printf("※★4.图书信息删除★※\n");printf("※※\n");printf("※★5.图书信息修改★※\n");printf("※※\n");printf("※★6.图书排序★※\n");printf("※※\n");printf("※★7.退出系统★※\n");printf("※※\n");printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n\n");printf("请输入选择项(1-7):");scanf("%d",&s);printf("\n\n\n\n");if(s>0&&s<8){switch(s){case 1:Input();break;case 2:Output();break;case 3:Find();break;case 4:Delete();break;case 5:Change();break;case 6:Paixu();break;case 7:printf("※※※※※※※※※※※※※※※※※※※※※※※※\n");printf("※※\n");printf("※谢谢使用! ※\n");printf("※再见! ※\n");printf("※※\n");printf("※※※※※※※※※※※※※※※※※※※※※※※※\n");exit(0);}}}}elseprintf("密码错误,请您重新进入系统\n");return 0;}else if(n==2){int t;printf("请输入读者密码:");scanf("%d",&t);if(t==.320){for(;;)/*省略"初始化"、"条件表达式"和"增量"*/{int h;printf("\n");printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");printf("※※\n");printf("※欢迎来河南工业大学图书服务系统※\n");printf("※※\n");printf("※※\n");printf("※主菜单※\n");printf("※※\n");printf("※★1.图书信息浏览★※\n");printf("※※\n");printf("※★2.图书信息查询★※\n");printf("※※\n");printf("※★3.图书排序★※\n");printf("※※\n");printf("※★4.退出系统★※\n");printf("※※\n");printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n\n");printf("请输入选择项(1-4):");scanf("%d",&h);printf("\n\n\n\n");if(h>0&&h<5){switch(h){case 1:Output();break;case 2:Find();break;case 3:Paixu();break;case 4:printf("※※※※※※※※※※※※※※※※※※※※※※※※\n");printf("※※\n");printf("※谢谢使用! ※\n");printf("※再见! ※\n");printf("※※\n");printf("※※※※※※※※※※※※※※※※※※※※※※※※\n");exit(0);}}}}elseprintf("密码错误,请您重新进入系统\n");return 0;}else return 0;}void Find()/*查找*/{int i;intchoose,t;charans[100];do{printf("※※※※※※※※※※※※※※※※※※※※※※※※\n");printf("※※\n");printf("※ 1.按书名查找※\n");printf("※ 2.按作者名查找※\n");printf("※▲返回主菜单(输入其他数字)※\n");printf("※※\n");printf("※※※※※※※※※※※※※※※※※※※※※※※※\n");printf("输入选项代号:");scanf("%d",&choose);if(choose==1){printf("输入所查书名:\n");scanf("%s",ans);t=-1;if(choose==1){for(i=0;i<MAX;i++)if(strcmp(ans,book[i].name)==0){t=i;printf("%d %s %s %s %s %s %2f\n",book[t].num,book[t].name,book[t].author,bo ok[t].fn,book[t].place,book[t].day,book[t].money);}}if(t==-1)printf("不存在该信息\n");}else if(choose==2){printf("输入所查作者名:\n");scanf("%s",ans);t=-1;if(choose==2){for(i=0;i<MAX;i++)if(strcmp(ans,book[i].author)==0){t=i;printf("%d %s %s %s %s %s %2f\n",book[t].num,book[t].name,book[t].author,bo ok[t].fn,book[t].place,book[t].day,book[t].money);}}if(t==-1) printf("不存在该信息\n");}else return;}while(1);}void Output()/*输出*/{FILE *fp;int i;fp=fopen("book","rb");printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");printf(" 图书列表\n");printf("-----------------------------------------------------------\n");printf("图书编号书名作者名分类号出版单位出版时间价格\n");printf("-----------------------------------------------------------\n");for(i=0;fread(&book[i],sizeof(struct book),1,fp)==1;i++){printf("%6d %8s %8s %8s %8s %8s %10.2f\n",book[i].num,book[i].name,book[i]. author,book[i].fn,book[i].place,book[i].day,book[i].money);}fclose(fp);}void Input()/*输入*/{FILE *fp;int n;fp=fopen("book","ab");/*建立一个新二进制文件*/for(n=0;n<MAX;n++){printf("n=%d 输入序号n(当输入n=-1时,返回),n=",n++);scanf("%d",&n);if(n==-1){fclose(fp);return;/*默认不返回值*/}else{printf("请输入图书编号书名作者名分类号出版单位出版时间价格\n");scanf("%d%s%s%s%s%s%f",&book[n].num,book[n].name,book[n].author,book[n ].fn,book[n].place,book[n].day,&book[n].money);fwrite(&book[n],sizeof(struct book),1,fp);/*写入文件*/}}fclose(fp);}void Delete()/*删除*/{FILE *fp;inti,flag,n,s,j;fp=fopen("book","rb+");rewind(fp);printf(" 图书列表\n");printf("-----------------------------------------------------------\n");printf("图书编号书名作者名分类号出版单位出版时间价格\n");printf("-----------------------------------------------------------\n");for(i=0;fread(&book[i],sizeof(struct book),1,fp)==1;i++){printf("%6d %8s %8s %8s %8s %8s %10.2f\n",book[i].num,book[i].name,book[i].auth or,book[i].fn,book[i].place,book[i].day,book[i].money);printf("\n");}n=i;printf("输入待删除图书编号:\n");scanf("%d",&s);for(i=0,flag=1;flag&&i<n;i++){if(s==book[i].num){for(j=i;j<n-1;j++){book[j].num=book[j+1].num;strcpy(book[j].name,book[j+1].name);strcpy(book[j].author,book[j+1].author);strcpy(book[j].fn,book[j+1].fn);strcpy(book[j].place,book[j+1].place);strcpy(book[j].day,book[j+1].day);book[j].money=book[j+1].money;}flag=0;}}if(!flag)n=n-1;elseprintf("没有此号\n");fp=fopen("book","wb");for(i=0;i<n;i++)fwrite(&book[i],sizeof(struct book),1,fp);fclose(fp);fp=fopen("book","r");printf(" 图书列表\n");printf("-----------------------------------------------------------\n");printf("图书编号书名作者名分类号出版单位出版时间价格\n");printf("-----------------------------------------------------------\n");for(i=0;i<n;i++){fread(&book[i],sizeof(struct book),1,fp);printf("%6d %8s %8s %8s %8s %8s %10.2f\n",book[i].num,book[i].name,book[i].auth or,book[i].fn,book[i].place,book[i].day,book[i].money);printf("\n");}fclose(fp);}void Change()/*修改*/{FILE *fp;inti,num,n;int flag=0;printf("请输入要修改的图书编号:");scanf("%d",&num);for(i=0;i<=MAX;i++)if(book[i].num==num){printf(" 图书列表\n");printf("-----------------------------------------------------------\n");printf("图书编号书名作者名分类号出版单位出版时间价格\n");printf("-----------------------------------------------------------\n");printf("%6d %8s %8s %8s %8s %8s %10.2f\n",book[i].num,book[i].name,book[i].auth or,book[i].fn,book[i].place,book[i].day,book[i].money);printf("-----------------------------------------------------------\n\n");n=i;flag=1;break;}if(flag==0){printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");printf("※输入错误! ※\n");printf("※请返回! ※\n");printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");return;}printf("\n\n\n");fp=fopen("book","rb+");fseek(fp,n*sizeof(struct book),0);printf("图书编号书名作者名分类号出版单位出版时间价格\n");scanf("%d%s%s%s%s%s%f",&book[n].num,book[n].name,book[n].author,book[n].fn, book[n].place,book[n].day,&book[n].money);fwrite(&book[i],sizeof(struct book),1,fp);fclose(fp);fp=fopen("book","rb");printf(" 图书列表\n");printf("-----------------------------------------------------------\n");printf("图书编号书名作者名分类号出版单位出版时间价格\n");printf("-----------------------------------------------------------\n");for(i=0;fread(&book[i],sizeof(struct book),1,fp)==1;i++){printf("%6d %8s %8s %8s %8s %8s %10.2f\n",book[i].num,book[i].name,book[i].auth or,book[i].fn,book[i].place,book[i].day,book[i].money);}printf("-----------------------------------------------------------\n\n");fclose(fp);}void Paixu()/*排序*/{FILE *fp;inti,max,s;fp=fopen("book","rb");for(s=0;fread(&book[s],sizeof(struct book),1,fp)==1;s++);for(i=0;i<s-i;i++){if(book[i].money>book[i+1].money){max=i;temp=book[i];book[i]=book[i+1];book[i+1]=temp;}}printf("※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※\n");printf(" 图书列表\n");printf("-----------------------------------------------------------\n");printf("图书编号书名作者名分类号出版单位出版时间价格\n");for(i=0;i<s;i++){printf("%6d %8s %8s %8s %8s %8s %10.2f\n",book[i].num,book[i].name,book[i]. author,book[i].fn,book[i].place,book[i].day,book[i].money);}printf("-----------------------------------------------------------\n");getchar();}。

C语言图书管理系统源代码

C语言图书管理系统源代码

/*需要在源代码相同目录下创建四个txt文件:,,,*/#include<>#include<>#include<>struct tushu{ /*图书结构体*/char num[11]; /*编号*/char name[31]; /*书名*/char writer[21]; /*作者*/char press[21]; /*出版社*/char kind[21]; /*类别*/double time; /*时间*/double price; /*价格*/struct tushu *next;};struct stu /*学生结构体*/{int snum; /*学号*/char mima[11]; /*密码*/struct stu *next;};struct jieshu{ /*借书信息结构体*/int xuehao; /*学生学号*/char num[11]; /*编号*/char name[31]; /*书名*/char writer[21]; /*作者*/char press[21]; /*出版社*/char kind[21]; /*类别*/double time; /*时间*/double price; /*价格*/struct jieshu *next;};int denglu=-1; /*已登录学生学号*/FILE *fp; /*图书文件*/FILE *fp1; /*管理员信息文件*/FILE *fp2; /*学生信息文件*/FILE *fp3; /*借书信息文件*/int main(); /*主函数声明*/struct tushu * create(); /*从文件创建图书链表(从文件中读出图书信息,建立单链表)*/struct stu * xcreate(); /*从文件创建学生信息(从文件读出学生信息,建立学生链表)*/struct jieshu * jcreate(); /2.否):");scanf("%d",&x);if(x==1)fprint(head);else{system("cls");printf("\n\n\n\t\t\t 文件没有被保存!\n\n\n\n");system("pause");system("cls");menu();}}lf %.2lf\n", p1->num,p1->name,p1->writer,p1->press,p1->kind,p1->time,p1->price );/*将图书信息写入文件*/fclose(fp);system("cls");printf("\n 图书信息已成功保存到文件中!\n");system("pause");system("cls");getchar();menu();}lf %.2lf\n", p1->num,p1->name,p1->writer,p1->press,p1->kind,p1->time,p1->price );/*将图书信息写入文件*/fclose(fp);system("cls");printf("\n 图书信息已成功保存到文件%s 中!\n",a);system("pause");system("cls");menu();}lf %.2lf\n", p1->num,p1->name,p1->writer,p1->press,p1->kind,p1->time,p1->price );/*将图书信息写入文件*/fclose(fp);xmenu();}lf %.2lf\n", p1->num,p1->name,p1->writer,p1->press,p1->kind,p1->time,p1->price );/*将图书信息写入文件*/}fclose(fp);system("cls");getchar();xmenu();}lf %.2lf\n",pj->xuehao,pj->num,pj->name,pj->writer,pj->press,pj->kind,pj->time,pj->price );fclose(fp3);}lf\n", p1->num,p1->name,p1->writer,p1->press,p1->kind,p1->time,p1->price);}system("pause");system("cls");menu();}lf\n", p1->num,p1->name,p1->writer,p1->press,p1->kind,p1->time,p1->price);}system("pause");system("cls");xmenu();}lf\n", p->num,p->name,p->writer,p->press,p->kind,p->time,p->price);i++;}p=p->next;}if(i==0){system("cls");printf("\n 对不起!没有找到名为《%s》的图书!\n",b);system("pause");}printf("\n\n\n\t\t\t是否继续查找图书信息(1.是/其他.返回):");scanf("%d",&n);if(n==1)gfind();else{system("cls");menu();}}lf\n", p->num,p->name,p->writer,p->press,p->kind,p->time,p->price);i++;}p=p->next;}if(i==0){system("cls");printf("\n 对不起!没有找到' %s '所着的相关图书!\n",b);system("pause");}printf("\n\n\n\t\t\t是否继续查找图书信息(1.是/其他.返回):");scanf("%d",&n);if(n==1)gfind();else{system("cls");menu();}}lf\n", p->num,p->name,p->writer,p->press,p->kind,p->time,p->price);i++;}p=p->next;}if(i==0){system("cls");printf("\n 对不起!没有找到类别为' %s '的图书!\n",b);system("pause");}printf("\n\n\n\t\t\t是否继续查找图书信息(1.是/其他.返回):");scanf("%d",&n);if(n==1)gfind();else{system("cls");menu();}}lf\n", p->num,p->name,p->writer,p->press,p->kind,p->time,p->price);i++;}p=p->next;}if(i==0){system("cls");printf("\n 对不起!没有找到名为《%s》的图书!\n",b);system("pause");}printf("\n\n\n\t\t\t是否继续查找图书信息(1.是/其他.返回):");scanf("%d",&n);if(n==1)xfind();else{system("cls");xmenu();}}lf\n", p->num,p->name,p->writer,p->press,p->kind,p->time,p->price);i++;}p=p->next;}if(i==0)system("cls");printf("\n 对不起!没有找到' %s '所着的相关图书!\n",b);system("pause");printf("\n\n\n\t\t\t是否继续查找图书信息(1.是/其他.返回):");scanf("%d",&n);if(n==1)xfind();else{system("cls");xmenu();}}lf\n", p->num,p->name,p->writer,p->press,p->kind,p->time,p->price);i++;}p=p->next;}if(i==0)system("cls");printf("\n 对不起!没有找到类别为' %s '的图书!\n",b);system("pause");printf("\n\n\n\t\t\t是否继续查找图书信息(1.是/其他.返回):");scanf("%d",&n);if(n==1)xfind();else{system("cls");xmenu();}}lf\n", p5->num,p5->name,p5->writer,p5->press,p5->kind,p5->time,p5->price);system("pause");system("cls");printf("\n\n\n\t\t\t是否保存图书信息(1.是/2.否(返回)):");scanf("%d",&x);if(x==1)fprint_(p1);elsesystem("cls");printf("\n\n\n\t\t\t 没有被保存到文件!\n\n\n\n");system("pause");system("cls");menu();return;}lf\n", p5->num,p5->name,p5->writer,p5->press,p5->kind,p5->time,p5->price);system("pause");system("cls");printf("\n\n\n\t\t\t是否保存图书信息(1.是/2.否(返回)):");scanf("%d",&x);if(x==1)fprint_(p1);elsesystem("cls");printf("\n\n\n\t\t\t 没有被保存到文件!\n\n\n\n");system("pause");system("cls");menu();return;}lf\n", p5->num,p5->name,p5->writer,p5->press,p5->kind,p5->time,p5->price);system("pause");system("cls");printf("\n\n\n\t\t\t是否保存图书信息(1.是/2.否(返回)):");scanf("%d",&x);if(x==1)fprint_(p1);elsesystem("cls");printf("\n\n\n\t\t\t 没有被保存到文件!\n\n\n\n");system("pause");system("cls");menu();return;}/2.否(返回)):");scanf("%d",&x);if(x==1)fprint_(head);elsesystem("cls");printf("\n\n\n\t\t\t 并没有被保存到文件!\n\n\n\n");system("pause");system("cls");menu();return;}/2.否(返回)):");scanf("%d",&x);if(x==1)fprint_(head);elsesystem("cls");printf("\n\n\n\t\t\t 没有被保存到文件!\n\n\n\n");system("pause");system("cls");menu();return;}lf %.2lf\n",pj->xuehao,pj->num,pj->name,pj->writer,pj->press,pj->kind,pj->time,pj->price);fclose(fp3);}lf %.2lf\n", h->num,h->name,h->writer,h->press,h->kind,h->time,h->price);fclose(fp);}....\n");getchar();there:fflush(stdin);system("cls");printf("\n\n\n1 :管理员登陆系统\n\n2 :学生登陆系统\n\n0 :退出\n\n\n\n");printf("请选择:");scanf("%d",&i);if(i==0){system("cls");printf("\n\n\n\n************************************************************** *******************\n");printf(" 感谢使用图书管理系统\n");printf("********************************************************************* ************\n\n\n");system("pause");exit(0);}else if(i==1){system("cls");secret();}else if(i==2){system("cls");head1=xcreate();if(head1==NULL){printf("暂没有学生信息,学生不能登录!\n");system("pause");system("cls");main();}xsecret(head1);}else{printf("\n\n输入错误,请重新输入!\n\n\n");system("pause");system("cls");j++;if(j==3){system("cls");printf("\n\n\n*************************************************************** **********\n\n");printf("\n\n\t\t你输入的次数过多,自动退出!谢谢使用!\n\n");printf("\n\n*************************************************************************\n\n");system("pause");exit(0);}goto there;}}。

C语言图书管理系统代码

C语言图书管理系统代码

#include<stdio.h>#include<stdlib.h>#include<string.h>struct book{int num;char bname[50];char wname[20];char press[50];char sort[50];int time;float price;struct book *next;};struct book *creatbook(); //创建链表struct book *addbook(struct book *head); //添加图书int yanzheng(struct book *head,int m); //验证新添加的图书编码是否已存在void deletebook(struct book *head); //删除图书页脚内容1void fprint(struct book *head); //将链表写入文件struct book *load(); //从文件中读取信息并建成链表void print_book(struct book *head); //将链表信息输出void chaxun(struct book *head); //查询图书信息void num_chaxun(struct book *head); //按图书编号查询图书void wname_chaxun(struct book *head); //按作者名查询图书void sort_chaxun(struct book *head); //按类别查询图书void time_chaxun(struct book *head); //按出版时间查询图书void bname_chaxun(struct book *head); //按图书名查询图书void xiugai(struct book *head); //修改图书信息void paixu(struct book *head); //对图书进行排序void num_paixu(struct book *head); //按图书编号排序void time_paixu(struct book *head); //按图书出版时间排序void price_paixu(struct book *head); //按图书价格排序void bname_paixu(struct book *head); //按图书名排序void wname_paixu(struct book *head); //按作者名排序int main()页脚内容2{int choice,n,x,y=1,c,c1=1234;char a,d,b[10],b1[10]="yjk";struct book *head=NULL;while(y){system("cls");printf("\n\n\n\n\n\n\n");printf(" ********** 欢迎光临**********\n\n");printf(" ********************** 图书信息管理系统************************\n\n\n");printf("\n\n");printf(" ============1-用户登录===========\n");printf(" ============0-退出系统===========\n");printf(" 请输入您的选择:");scanf("%d",&n);printf("\n");getchar();页脚内容3switch(n){case 0:y=0;break;case 1:printf(" 请输入您的用户名:");gets(b);printf("\n");printf(" 请输入您的密码:");scanf("%d",&c);printf("\n");if(strcmp(b,b1)!=0||c!=c1){printf(" 验证失败,请重新输入!\n");scanf("%c",&d);getchar();system("cls");}页脚内容4else{printf(" 验证通过!请按Enter键进入!\n");scanf("%c",&d);getchar();x=1;while(x){system("cls");printf(" ------------------\n");printf(" *图书信息管理系统*\n");printf(" ------------------\n\n");printf(" **********************************************\n\n");printf(" **********************************************\n\n");printf(" || 1-添加图书2-删除图书||\n\n");printf(" || 3-图书列表4-图书排序||\n\n");printf(" || 5-查询图书6-修改图书||\n\n");printf(" || 7-录入数据0-退出系统||\n\n");页脚内容5printf(" **********************************************\n\n");printf(" **********************************************\n\n");printf("请输入所选择的序号:");scanf("%d",&choice);getchar();system("cls");switch(choice){case 0:x=0;break;case 1:head=load();if(head==NULL){printf("文件为空,请先录入数据!\n");getchar();break;}页脚内容6else{head=addbook(head);printf("添加成功!\n");printf("是否将新信息保存到文件?(y/n)\n"); scanf("%c",&a);getchar();switch(a){case 'n':break;case 'y':fprint(head);printf("保存成功!\n");getchar();break;}break;}页脚内容7case 2:head=load();if(head==NULL){printf("文件为空,请先录入数据!\n");getchar();break;}else{deletebook(head);getchar();break;}break;case 3:head=load();if(head==NULL){页脚内容8printf("文件为空,请先录入数据!\n");getchar();break;}else{print_book(head);getchar();break;}case 4:head=load();if(head==NULL){printf("文件为空,请先录入数据!\n");getchar();break;}else{页脚内容9paixu(head);getchar();}break;case 5:head=load();if(head==NULL){printf("文件为空,请先录入数据!\n");getchar();break;}else{chaxun(head);getchar();}break;case 6:页脚内容10head=load();if(head==NULL){printf("文件为空,请先录入数据!\n");getchar();break;}else{xiugai(head);getchar();break;}break;case 7:printf("注意:输入图书编码为0时结束!\n");head=creatbook();printf("是否将输入的信息保存到文件以覆盖文件中已存在的信息?(y/n)\n");getchar();页脚内容11scanf("%c",&a);getchar();switch(a){case 'n':break;case 'y':fprint(head);printf("保存成功!\n");getchar();break;}break;default:printf("您的输入有误,请重新输入!\n");getchar();break;}页脚内容12}}break;default:printf(" 您的输入有误! 请重新输入!\n");getchar();break;}}}//录入数据并形成链表struct book *creatbook(){struct book *head,*tail,*p;int num,time,n;char bname[50],wname[20],press[50],sort[50];页脚内容13float price;int size=sizeof(struct book);head=tail=NULL;printf("请输入图书编号:");scanf("%d",&num);printf("请输入图书名:");scanf("%s",bname);getchar();printf("请输入作者名:");scanf("%s",wname);getchar();printf("请输入出版社:");scanf("%s",press);getchar();printf("请输入类别:");scanf("%s",sort);getchar();页脚内容14printf("请输入出版时间:");scanf("%d",&time);getchar();printf("请输入价格:");scanf("%f",&price);getchar();while(1){p=(struct book *)malloc(size);p->num=num;strcpy(p->bname,bname);strcpy(p->wname,wname);strcpy(p->press,press);strcpy(p->sort,sort);p->time=time;p->price=price;p->next=NULL;if(head==NULL)页脚内容15head=p;elsetail->next=p;tail=p;do{printf("请输入图书编号:");scanf("%d",&num);n=yanzheng(head,num);if(n==0)break;elseprintf("您输入的编号已存在,请重新输入!\n"); }while(1);if(num==0)break;else{页脚内容16printf("请输入图书名:");scanf("%s",bname);getchar();printf("请输入作者名:");scanf("%s",wname);getchar();printf("请输入出版社:");scanf("%s",press);getchar();printf("请输入类别:");scanf("%s",sort);getchar();printf("请输入出版时间:");scanf("%d",&time);getchar();printf("请输入价格:");scanf("%f",&price);页脚内容17getchar();}}return head;}//插入结点,并且插入后仍按一定顺序struct book *addbook(struct book *head){struct book *ptr,*p1,*p2,*p;char bname[50],wname[20],press[50],sort[50];int size=sizeof(struct book);int num,time,n=1;float price;do{printf("请输入图书编号:");scanf("%d",&num);页脚内容18n=yanzheng(head,num);if(n==0)break;elseprintf("您输入的编号已存在,请重新输入!\n"); }while(1);printf("请输入图书名:");scanf("%s",bname);getchar();printf("请输入作者名:");scanf("%s",wname);getchar();printf("请输入出版社:");scanf("%s",press);getchar();printf("请输入类别:");scanf("%s",sort);页脚内容19getchar();printf("请输入出版时间:");scanf("%d",&time);getchar();printf("请输入价格:");scanf("%f",&price);getchar();p=(struct book *)malloc(size);p->num=num;strcpy(p->bname,bname);strcpy(p->wname,wname);strcpy(p->press,press);strcpy(p->sort,sort);p->time=time;p->price=price;p2=head;ptr=p;页脚内容20while((ptr->num>p2->num)&&(p2->next!=NULL)){ p1=p2;p2=p2->next;}if(ptr->num<=p2->num){if(head==p2)head=ptr;else{p1->next=ptr;p->next=p2;}}else{p2->next=ptr;p->next=NULL;}return head;页脚内容21}//验证添加的图书编号是否已存在int yanzheng(struct book *head,int m){struct book *p;p=head;while(p!=NULL){if(p->num==m)break;p=p->next;}if(p==NULL)return 0;elsereturn 1;}页脚内容22//将新链表写入文件中void fprint(struct book *head){FILE *fp;char ch='1';struct book *p1;if((fp=fopen("f1.txt","w"))==NULL){printf("File open error!\n");exit(0);}fputc(ch,fp);for(p1=head;p1;p1=p1->next){fprintf(fp,"%d %s %s %s %s %d %f\n",p1->num,p1->bname,p1->wname,p1->press,p1->sort,p1->time,p1->p rice);}fclose(fp);}页脚内容23//从文件中读取图书信息struct book *load(){FILE *fp;char ch;struct book *head,*tail,*p1;head=tail=NULL;if((fp=fopen("f1.txt","r"))==NULL){printf("File open error!\n");exit(0);}ch=fgetc(fp);if(ch=='1'){while(!feof(fp)){p1=(struct book *)malloc(sizeof(struct book));fscanf(fp,"%d%s%s%s%s%d%f\n",&p1->num,p1->bname,p1->wname,p1->press,p1->sort,&p1->time,&p1->price页脚内容24);if(head==NULL)head=p1;elsetail->next=p1;tail=p1;}tail->next=NULL;fclose(fp);return head;}elsereturn NULL;}//将整个链表的信息输出void print_book(struct book *head)页脚内容25{struct book *ptr;if(head==NULL){printf("\n没有信息!\n");return;}printf(" 图书信息列表如下\n");printf("==========================================================\n");printf(" 编号图书名作者名出版社类别出版时间价格\n");for(ptr=head;ptr;ptr=ptr->next)printf(" %d %s %s %s %s %d %.2f\n",ptr->num,ptr->bna me,ptr->wname,ptr->press,ptr->sort,ptr->time,ptr->price);printf("==========================================================\n");}//删除图书信息页脚内容26void deletebook(struct book *head){int a;char b,ch='1';struct book *p1,*p2;FILE *fp;printf("请输入要删除的图书编号:");scanf("%d",&a);p1=head;if(p1->num==a&&p1->next==NULL){ //对于文件中只有一组数据printf("是否清空文件!(y/n)\n");getchar();scanf("%c",&b);getchar();switch(b){case 'n':break;页脚内容27case 'y':if((fp=fopen("f1.txt","w"))==NULL){printf("File open error!\n");exit(0);}fclose(fp);printf("文件已清空!\n");}}else{while(p1->num!=a&&p1->next!=NULL){p2=p1;p1=p1->next;}if(p1->next==NULL){if(p1->num==a){页脚内容28p2->next=NULL;printf("是否确定从文件中彻底删除该图书?(y/n)\n");getchar();scanf("%c",&b);switch(b){case 'n':break;case 'y':fprint(head);printf("删除成功!\n");getchar();break;}}else{printf("没有找到要删除的数据!\n");页脚内容29getchar();}}else if(p1==head){head=p1->next;printf("是否确定从文件中彻底删除该图书?(y/n)\n");getchar();scanf("%c",&b);switch(b){case 'n':break;case 'y':fprint(head);printf("删除成功!\n");getchar();break;页脚内容30}}else{p2->next=p1->next;printf("是否确定从文件中彻底删除该图书?(y/n)\n");getchar();scanf("%c",&b);switch(b){case 'n':break;case 'y':fprint(head);printf("删除成功!\n");getchar();break;}页脚内容31}}}//图书查询void chaxun(struct book *head){int a;printf("==========================================================\n");printf(" ** 1-按图书编号查询2-按图书名查询**\n");printf(" ** 3-按图书类别查询4-按作者名查询**\n");printf(" ** 5-按出版时间查询0-退出查询**\n");printf("==========================================================\n");printf("请输入所选择的编号:");scanf("%d",&a);getchar();页脚内容32switch(a){case 0:break;case 1:num_chaxun(head);break;case 2:bname_chaxun(head);break;case 3:sort_chaxun(head);break;case 4:wname_chaxun(head);break;case 5:time_chaxun(head);页脚内容33break;default:printf("您的输入有误!\n");break;}}//按编号查询图书信息void num_chaxun(struct book *head){int a;struct book *p;printf("请选择您要查询的图书编号:");scanf("%d",&a);getchar();p=head;while(p!=NULL){if(p->num==a)页脚内容34break;p=p->next;}if(p==NULL){printf("没有找到该编号的图书!\n");}else{printf(" 你所查询的图书信息如下\n");printf("================================================================== =======\n");printf(" ** 编号图书名作者名出版社类别出版时间价格**\n");printf(" ** %d %s %s %s %s %d %.2f **\n",p->num,p->bname,p->wname,p->press,p->sort,p->time,p->price);printf("================================================================== =======\n");}页脚内容35}//按图书名查询图书信息void bname_chaxun(struct book *head){char a[50];int flag=0;struct book *p;printf("请选择您要查询的图书名:");gets(a);p=head;while(p!=NULL){if(strcmp(p->bname,a)==0){flag=1;break;}p=p->next;}页脚内容36if(flag==0){printf("没有找到该图书名的图书!\n");}else{printf(" 你所查询的图书信息如下\n");printf("================================================================== =======\n");printf(" ** 编号图书名作者名出版社类别出版时间价格**\n");while(p!=NULL){if(strcmp(p->bname,a)==0){printf(" ** %d %s %s %s %s %d %.2f **\n",p->num,p->bname,p->wname,p->press,p->sort,p->time,p->price);}p=p->next;}printf("==================================================================页脚内容37=======\n");}}//按作者名查询图书信息void wname_chaxun(struct book *head){char a[50];int flag=0;struct book *p;printf("请选择您要查询的图书作者名:");gets(a);p=head;while(p!=NULL){if(strcmp(p->wname,a)==0){flag=1;break;页脚内容38}p=p->next;}if(flag==0){printf("没有找到该图书名的图书!\n");}else{printf(" 你所查询的图书信息如下\n");printf("================================================================== =======\n");printf(" ** 编号图书名作者名出版社类别出版时间价格**\n");while(p!=NULL){if(strcmp(p->wname,a)==0){printf(" ** %d %s %s %s %s %d %.2f **\n",p->num,p->bname,p->wname,p->press,p->sort,p->time,p->price);flag=1;页脚内容39}p=p->next;}printf("================================================================== =======\n");}}//按图书类别查询图书信息void sort_chaxun(struct book *head){char a[50];int flag=0;struct book *p;printf("请选择您要查询的图书类别:");gets(a);p=head;页脚内容40while(p!=NULL){if(strcmp(p->sort,a)==0){flag=1;break;}p=p->next;}if(flag==0){printf("没有找到该图书名的图书!\n");}else{printf(" 你所查询的图书信息如下\n");printf("================================================================== =======\n");printf(" ** 编号图书名作者名出版社类别出版时间价格**\n");while(p!=NULL){页脚内容41if(strcmp(p->sort,a)==0){printf(" ** %d %s %s %s %s %d %.2f **\n",p->num,p->bname,p->wname,p->press,p->sort,p->time,p->price);flag=1;}p=p->next;}printf("================================================================== =======\n");}}//按图书出版时间查询图书信息void time_chaxun(struct book *head){int a,flag=0;struct book *p;printf("请选择您要查询的图书出版时间:");页脚内容42scanf("%d",&a);getchar();p=head;while(p!=NULL){if(p->time==a){flag=1;break;}p=p->next;}if(flag==0){printf("没有找到该图书名的图书!\n");}else{printf(" 你所查询的图书信息如下\n");printf("==================================================================页脚内容43=======\n");printf(" ** 编号图书名作者名出版社类别出版时间价格**\n");while(p!=NULL){if(p->time==a){printf(" ** %d %s %s %s %s %d %.2f **\n",p->num,p->bname,p->wname,p->press,p->sort,p->time,p->price);flag=1;}p=p->next;}printf("================================================================== =======\n");}}//修改图书信息void xiugai(struct book *head){页脚内容44int a,b;char c;struct book *p;printf("请输入要修改的图书编号:");scanf("%d",&a);p=head;while(p!=NULL){if(p->num==a)break;p=p->next;}if(p==NULL){printf("没有找到该编号的图书!\n");getchar();}else{printf("页脚内容45============================================================\n");printf(" ** 1-编号2-图书名3-作者名**\n");printf(" ** 4-出版社5-类别6-出版时间**\n");printf(" ** 7-价格8-修改全部0-放弃修改**\n");printf("============================================================\n");printf("请选择你要修改的信息编号:");scanf("%d",&b);getchar();switch(b){case 1:printf("请输入新编号:");scanf("%d",&p->num);printf("修改成功!\n");getchar();break;case 2:页脚内容46printf("请输入新图书名:");gets(p->bname);printf("修改成功!\n");break;case 3:printf("请输入新作者名:");gets(p->wname);printf("修改成功!\n");break;case 4:printf("请输入新出版社:");gets(p->press);printf("修改成功!\n");break;case 5:printf("请输入新类别:");gets(p->sort);页脚内容47printf("修改成功!\n");break;case 6:printf("请输入新出版时间:");scanf("%d",&p->time);printf("修改成功!\n");getchar();break;case 7:printf("请输入新价格:");scanf("%f",&p->price);printf("修改成功!\n");getchar();break;case 8:printf("请输入新图书编号:");scanf("%d",&p->num);页脚内容48printf("请输入新图书名:");scanf("%s",p->bname);getchar();printf("请输入新作者名:");scanf("%s",p->wname);getchar();printf("请输入新出版社:");scanf("%s",p->press);getchar();printf("请输入新类别:");scanf("%s",p->sort);getchar();printf("请输入新出版时间:");scanf("%d",&p->time);getchar();printf("请输入新价格:");scanf("%f",&p->price);页脚内容49getchar();printf("修改成功!\n");getchar();break;case 0:break;default :printf("您的输入有误!\n");break;}printf("是否将修改后的信息保存到文件中?(y/n)\n"); scanf("%c",&c);getchar();switch(c){case 'n':break;case 'y':页脚内容50。

C语言 图书信息管理系统 最终源代码

C语言  图书信息管理系统 最终源代码

C语言图书信息管理系统最终源代码#include<stdio.h>#include"string.h"struct book /* book2 是用来装用户要录入图书的信息*/{long num,time;char name[12],writer[12],fenlei[8],danwei[10];int pay;}stu[100];struct book1{long num,time;char name[12],writer[12],fenlei[8],danwei[10];int pay;}stu1[100];struct book2 /* book 是用来装cc.dat 文件中的图书信息*/{long num,time;char name[12],writer[12],fenlei[8],danwei[10];int pay;}stu2[100];denglu(){char a[15]="drzhang",b[15]="29257966",c[15],d[15];L3:system("cls");printf(" 欢迎登陆图书管理系统\n\n");printf("请输入姓名:");gets(c);printf("请输入密码:");gets(d);if(strcmp(a,c)==0&&strcmp(b,d)==0)return 0;else{printf("输入错误请重新输入\n\n");printf("1.重新输入 2.退出系统\n\n");printf("请输入.....");switch(getch()){case 49: goto L3;break;default: return 56;break;}}}/*此功能实现的是开始菜单的显示和具体功能的选择*/start(){printf("******************************欢迎使用图书管理系统******************************");printf("* 功能目录:*");printf("* |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| *");printf("* || || *");`printf("* || 1、图书查询2、图书浏览3、录入图书4、删除与修改5、备份与还原|| *");printf("* || || *");printf("* || || *");printf("* || || *");printf("* |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| *");printf("* 使用说明:如要选择“图书查询”,直接输入:1 退出:0 *");printf("************************************************************************* *******");printf("你想要做什么:");return getch(); /*输入选择对应功能的值,返回输入的值,即:用于判断选择的功能*/ }/*实现备份功能,以防图书信息出错带来的麻烦,不必看懂*/beifen(){FILE *fp1,*fp2;int i,a;jieshuo();system("cls");printf("\n\n请选择\\ 备份(B)| 浏览备份(L)/ \\ 还原(H)/ \\ 退出(Q)/ \n");switch(getch()){case 'B':case 'b':if((fp1=fopen("cc","r"))==NULL){printf("不能打开次文件\n");exit(0);}if((fp2=fopen("cc01","w"))==NULL){printf("不能打开次文件\n");exit(0);}fscanf(fp1,"%d",&a);fprintf(fp2,"%d\n",a);printf("\n\n确认备份吗?(Y/N)");switch(getch()){case 'Y':case 'y':for(i=0;i<a;i++){fscanf(fp1,"%ld %s %s %s %s %ld %d",&stu[i].num,stu[i].name,stu[i].writer,stu[i].fenlei,stu[i] .danwei,&stu[i].time,&stu[i].pay);fprintf(fp2,"%ld\t%s\t%s\t%s\t%s\t%ld\t%d\n",stu[i].num,stu[i].name,stu[i].writer,stu[i].fenlei,st u[i].danwei,stu[i].time,stu[i].pay);}printf("\n\n备份成功!\n\n是否打开浏览备份文件?(Y/N)");switch(getch()){case 'Y':case 'y':fclose(fp1);fclose(fp2);liulan02();break;case 'N':case 'n':break;}break;case 'N':case 'n':break;}fclose(fp1);fclose(fp2);break;case 'H':case 'h':if((fp1=fopen("cc01","r"))==NULL){printf("不能打开此文件\n");exit(0);}if((fp2=fopen("cc","w"))==NULL){printf("不能打开此文件\n");exit(0);}fscanf(fp1,"%d",&a);fprintf(fp2,"%d\n",a);printf("\n\n确认还原吗?(Y/N)");switch(getch()){case 'Y':case 'y':for(i=0;i<a;i++){fscanf(fp1,"%ld %s %s %s %s %ld %d",&stu[i].num,stu[i].name,stu[i].writer,stu[i].fenlei,stu[i] .danwei,&stu[i].time,&stu[i].pay);fprintf(fp2,"%ld\t%s\t%s\t%s\t%s\t%ld\t%d\n",stu[i].num,stu[i].name,stu[i].writer,stu[i].fenlei,st u[i].danwei,stu[i].time,stu[i].pay);}printf("\n\n还原成功!\n\n是否打开浏览还原文件?(Y/N)");switch(getch()){case 'Y':case 'y':fclose(fp1);fclose(fp2);liulan();break;case 'N':case 'n':break;}break;case 'N':case 'n':break;}fclose(fp1);fclose(fp2);break;case 'l':case 'L':liulan02();break;case 'Q':case 'q':break;}}/* 在屏幕上打印查询菜单*/chaxun(){jieshuo();system("cls");printf("******************************欢迎使用图书查询系统******************************");printf("* 查询方式:*");printf("**");printf("* 1、书名2、作者名3、出版时间*");printf("**");printf("* 4、价格5、分类号6、出版单位*");printf("**");printf("* 使用说明:如要选择“书名”,直接输入:1 退出:0 *");printf("************************************************************************* *******");printf("\n请输入你要怎么查询:");chaxun01(); /*打印完毕后,进入chaxun()函数,执行用户需要的查询功能*/}/*此程序段实行查询的各种功能*/chaxun01(){FILE *fp;char ch[8];int a=0,i,j,k,n=0,c,d;long b;if((fp=fopen("cc","r"))==NULL) /*说明:cc.dat 文件装着图书的信息,*/ {printf("不能打开此文件\n");exit(0);} /*并且文件开记录的是图书的书量,后*/ fscanf(fp,"%d",&a); /*从文件中读出图书数*/ /*面依次才是具体信息。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include<stdio.h>#include<stdlib.h>#include<string.h>struct book{int num;char bname[50];char wname[20];char press[50];char sort[50];int time;float price;struct book *next;};struct book *creatbook(); //创建链表struct book *addbook(struct book *head); //添加图书int yanzheng(struct book *head,int m); //验证新添加的图书编码是否已存在void deletebook(struct book *head); //删除图书void fprint(struct book *head); //将链表写入文件struct book *load(); //从文件中读取信息并建成链表void print_book(struct book *head); //将链表信息输出void chaxun(struct book *head); //查询图书信息void num_chaxun(struct book *head); //按图书编号查询图书void wname_chaxun(struct book *head); //按作者名查询图书void sort_chaxun(struct book *head); //按类别查询图书void time_chaxun(struct book *head); //按出版时间查询图书void bname_chaxun(struct book *head); //按图书名查询图书void xiugai(struct book *head); //修改图书信息void paixu(struct book *head); //对图书进行排序void num_paixu(struct book *head); //按图书编号排序void time_paixu(struct book *head); //按图书出版时间排序void price_paixu(struct book *head); //按图书价格排序void bname_paixu(struct book *head); //按图书名排序void wname_paixu(struct book *head); //按作者名排序int main(){int choice,n,x,y=1,c,c1=1234;char a,d,b[10],b1[10]="yjk";struct book *head=NULL;while(y){system("cls");printf("\n\n\n\n\n\n\n");printf(" ********** 欢迎光临**********\n\n");printf(" ********************** 图书信息管理系统************************\n\n\n");printf("\n\n");printf(" ============1-用户登录===========\n");printf(" ============0-退出系统===========\n");printf(" 请输入您的选择:");scanf("%d",&n);printf("\n");getchar();switch(n){case 0:y=0;break;case 1:printf(" 请输入您的用户名:");gets(b);printf("\n");printf(" 请输入您的密码:");scanf("%d",&c);printf("\n");if(strcmp(b,b1)!=0||c!=c1){printf(" 验证失败,请重新输入!\n");scanf("%c",&d);getchar();system("cls");}else{printf(" 验证通过!请按Enter键进入!\n");scanf("%c",&d);getchar();x=1;while(x){system("cls");printf(" ------------------\n");printf(" *图书信息管理系统*\n");printf(" ------------------\n\n");printf(" **********************************************\n\n");printf(" **********************************************\n\n");printf(" || 1-添加图书2-删除图书||\n\n");printf(" || 3-图书列表4-图书排序||\n\n");printf(" || 5-查询图书6-修改图书||\n\n");printf(" || 7-录入数据0-退出系统||\n\n");printf(" **********************************************\n\n");printf(" **********************************************\n\n");printf("请输入所选择的序号:");scanf("%d",&choice);getchar();system("cls");switch(choice){case 0:x=0;break;case 1:head=load();if(head==NULL){printf("文件为空,请先录入数据!\n");getchar();break;}else{head=addbook(head);printf("添加成功!\n");printf("是否将新信息保存到文件?(y/n)\n");scanf("%c",&a);getchar();switch(a){case 'n':break;case 'y':fprint(head);printf("保存成功!\n");getchar();break;}break;}case 2:head=load();if(head==NULL){printf("文件为空,请先录入数据!\n");getchar();break;}else{deletebook(head);getchar();break;}break;case 3:head=load();if(head==NULL){printf("文件为空,请先录入数据!\n");getchar();break;}else{print_book(head);getchar();break;}case 4:head=load();if(head==NULL){printf("文件为空,请先录入数据!\n");getchar();break;}else{paixu(head);getchar();}break;case 5:head=load();if(head==NULL){printf("文件为空,请先录入数据!\n");getchar();break;}else{chaxun(head);getchar();}break;case 6:head=load();if(head==NULL){printf("文件为空,请先录入数据!\n");getchar();break;}else{xiugai(head);getchar();break;}break;case 7:printf("注意:输入图书编码为0时结束!\n");head=creatbook();printf("是否将输入的信息保存到文件以覆盖文件中已存在的信息?(y/n)\n");getchar();scanf("%c",&a);getchar();switch(a){case 'n':break;case 'y':fprint(head);printf("保存成功!\n");getchar();break;}break;default:printf("您的输入有误,请重新输入!\n");getchar();break;}}}break;default:printf(" 您的输入有误! 请重新输入!\n");getchar();break;}}}//录入数据并形成链表struct book *creatbook(){struct book *head,*tail,*p;int num,time,n;char bname[50],wname[20],press[50],sort[50];float price;int size=sizeof(struct book);head=tail=NULL;printf("请输入图书编号:");scanf("%d",&num);printf("请输入图书名:");scanf("%s",bname);getchar();printf("请输入作者名:");scanf("%s",wname);getchar();printf("请输入出版社:");scanf("%s",press);getchar();printf("请输入类别:");scanf("%s",sort);getchar();printf("请输入出版时间:");scanf("%d",&time);getchar();printf("请输入价格:");scanf("%f",&price);getchar();while(1){p=(struct book *)malloc(size);p->num=num;strcpy(p->bname,bname);strcpy(p->wname,wname);strcpy(p->press,press);strcpy(p->sort,sort);p->time=time;p->price=price;p->next=NULL;if(head==NULL)head=p;elsetail->next=p;tail=p;do{printf("请输入图书编号:");scanf("%d",&num);n=yanzheng(head,num);if(n==0)break;elseprintf("您输入的编号已存在,请重新输入!\n");}while(1);if(num==0)break;else{printf("请输入图书名:");scanf("%s",bname);getchar();printf("请输入作者名:");scanf("%s",wname);getchar();printf("请输入出版社:");scanf("%s",press);getchar();printf("请输入类别:");scanf("%s",sort);getchar();printf("请输入出版时间:");scanf("%d",&time);getchar();printf("请输入价格:");scanf("%f",&price);getchar();}}return head;}//插入结点,并且插入后仍按一定顺序struct book *addbook(struct book *head){struct book *ptr,*p1,*p2,*p;char bname[50],wname[20],press[50],sort[50];int size=sizeof(struct book);int num,time,n=1;float price;do{printf("请输入图书编号:");scanf("%d",&num);n=yanzheng(head,num);if(n==0)break;elseprintf("您输入的编号已存在,请重新输入!\n"); }while(1);printf("请输入图书名:");scanf("%s",bname);getchar();printf("请输入作者名:");scanf("%s",wname);getchar();printf("请输入出版社:");scanf("%s",press);getchar();printf("请输入类别:");scanf("%s",sort);getchar();printf("请输入出版时间:");scanf("%d",&time);getchar();printf("请输入价格:");scanf("%f",&price);getchar();p=(struct book *)malloc(size);p->num=num;strcpy(p->bname,bname);strcpy(p->wname,wname);strcpy(p->press,press);strcpy(p->sort,sort);p->time=time;p->price=price;p2=head;ptr=p;while((ptr->num>p2->num)&&(p2->next!=NULL)){ p1=p2;p2=p2->next;}if(ptr->num<=p2->num){if(head==p2)head=ptr;else{p1->next=ptr;p->next=p2;}}else{p2->next=ptr;p->next=NULL;}return head;}//验证添加的图书编号是否已存在int yanzheng(struct book *head,int m) {struct book *p;p=head;while(p!=NULL){if(p->num==m)break;p=p->next;}if(p==NULL)return 0;elsereturn 1;}//将新链表写入文件中void fprint(struct book *head){FILE *fp;char ch='1';struct book *p1;if((fp=fopen("f1.txt","w"))==NULL){ printf("File open error!\n");exit(0);}fputc(ch,fp);for(p1=head;p1;p1=p1->next){fprintf(fp,"%d %s %s %s %s %d %f\n",p1->num,p1->bname,p1->wname,p1->press,p1-> sort,p1->time,p1->price);}fclose(fp);}//从文件中读取图书信息struct book *load(){FILE *fp;char ch;struct book *head,*tail,*p1;head=tail=NULL;if((fp=fopen("f1.txt","r"))==NULL){printf("File open error!\n");exit(0);}ch=fgetc(fp);if(ch=='1'){while(!feof(fp)){p1=(struct book *)malloc(sizeof(struct book));fscanf(fp,"%d%s%s%s%s%d%f\n",&p1->num,p1->bname,p1->wname,p1->press,p1->sort,&p 1->time,&p1->price);if(head==NULL)head=p1;elsetail->next=p1;tail=p1;}tail->next=NULL;fclose(fp);return head;}elsereturn NULL;}//将整个链表的信息输出void print_book(struct book *head){struct book *ptr;if(head==NULL){printf("\n没有信息!\n");return;}printf(" 图书信息列表如下\n");printf("==========================================================\n");printf(" 编号图书名作者名类别出版时间价格\n");for(ptr=head;ptr;ptr=ptr->next)printf(" %d %s %s %s %s %d %.2f\n",ptr->num,ptr->bname,ptr->wname,ptr->press,ptr->sort,ptr->time,ptr->price);printf("==========================================================\n");}//删除图书信息void deletebook(struct book *head){int a;char b,ch='1';struct book *p1,*p2;FILE *fp;printf("请输入要删除的图书编号:");scanf("%d",&a);p1=head;if(p1->num==a&&p1->next==NULL){ //对于文件中只有一组数据printf("是否清空文件!(y/n)\n");getchar();scanf("%c",&b);getchar();switch(b){case 'n':break;case 'y':if((fp=fopen("f1.txt","w"))==NULL){printf("File open error!\n");exit(0);}fclose(fp);printf("文件已清空!\n");}}else{while(p1->num!=a&&p1->next!=NULL){p2=p1;p1=p1->next;}if(p1->next==NULL){if(p1->num==a){p2->next=NULL;printf("是否确定从文件中彻底删除该图书?(y/n)\n");getchar();scanf("%c",&b);switch(b){case 'n':break;case 'y':fprint(head);printf("删除成功!\n");getchar();break;}}else{printf("没有找到要删除的数据!\n");getchar();}}else if(p1==head){head=p1->next;printf("是否确定从文件中彻底删除该图书?(y/n)\n");getchar();scanf("%c",&b);switch(b){case 'n':break;case 'y':fprint(head);printf("删除成功!\n");getchar();break;}}else{p2->next=p1->next;printf("是否确定从文件中彻底删除该图书?(y/n)\n");getchar();scanf("%c",&b);switch(b){case 'n':break;case 'y':fprint(head);printf("删除成功!\n");getchar();break;}}}}//图书查询void chaxun(struct book *head){int a;printf("==========================================================\n");printf(" ** 1-按图书编号查询2-按图书名查询**\n");printf(" ** 3-按图书类别查询4-按作者名查询**\n");printf(" ** 5-按出版时间查询0-退出查询**\n");printf("==========================================================\n");printf("请输入所选择的编号:");scanf("%d",&a);getchar();switch(a){case 0:break;case 1:num_chaxun(head);break;case 2:bname_chaxun(head);break;case 3:sort_chaxun(head);break;case 4:wname_chaxun(head);break;case 5:time_chaxun(head);break;default:printf("您的输入有误!\n");break;}}//按编号查询图书信息void num_chaxun(struct book *head){int a;struct book *p;printf("请选择您要查询的图书编号:");scanf("%d",&a);getchar();p=head;while(p!=NULL){if(p->num==a)break;p=p->next;}if(p==NULL){printf("没有找到该编号的图书!\n");}else{printf(" 你所查询的图书信息如下\n");printf("=========================================================================\n" );printf(" ** 编号图书名作者名类别出版时间价格**\n");printf("** %d %s %s %s %s %d %.2f **\n",p->num,p->bname,p->wname,p->press,p->sort,p->time,p->price);printf("=========================================================================\n" );}}//按图书名查询图书信息void bname_chaxun(struct book *head){char a[50];int flag=0;struct book *p;printf("请选择您要查询的图书名:");gets(a);p=head;while(p!=NULL){if(strcmp(p->bname,a)==0){flag=1;break;}p=p->next;}if(flag==0){printf("没有找到该图书名的图书!\n");}else{printf(" 你所查询的图书信息如下\n");printf("=========================================================================\n" );printf(" ** 编号图书名作者名类别出版时间价格**\n");while(p!=NULL){if(strcmp(p->bname,a)==0){printf("** %d %s %s %s %s %d %.2f **\n",p->num,p->bname,p->wname,p->press,p->sort,p->time,p->price);}p=p->next;}printf("=========================================================================\n" );}}//按作者名查询图书信息void wname_chaxun(struct book *head){char a[50];int flag=0;struct book *p;printf("请选择您要查询的图书作者名:");gets(a);p=head;while(p!=NULL){if(strcmp(p->wname,a)==0){flag=1;break;}p=p->next;}if(flag==0){printf("没有找到该图书名的图书!\n");}else{printf(" 你所查询的图书信息如下\n");printf("=========================================================================\n" );printf(" ** 编号图书名作者名类别出版时间价格**\n");while(p!=NULL){if(strcmp(p->wname,a)==0){printf("** %d %s %s %s %s %d %.2f **\n",p->num,p->bname,p->wname,p->press,p->sort,p->time,p->price);flag=1;}p=p->next;}printf("=========================================================================\n" );}}//按图书类别查询图书信息void sort_chaxun(struct book *head){char a[50];int flag=0;struct book *p;printf("请选择您要查询的图书类别:");gets(a);p=head;while(p!=NULL){if(strcmp(p->sort,a)==0){flag=1;break;}p=p->next;}if(flag==0){printf("没有找到该图书名的图书!\n");}else{printf(" 你所查询的图书信息如下\n");printf("=========================================================================\n" );printf(" ** 编号图书名作者名类别出版时间价格**\n");while(p!=NULL){if(strcmp(p->sort,a)==0){printf("** %d %s %s %s %s %d %.2f **\n",p->num,p->bname,p->wname,p->press,p->sort,p->time,p->price);flag=1;}p=p->next;}printf("=========================================================================\n" );}}//按图书出版时间查询图书信息void time_chaxun(struct book *head){int a,flag=0;struct book *p;printf("请选择您要查询的图书出版时间:");scanf("%d",&a);getchar();p=head;while(p!=NULL){if(p->time==a){flag=1;break;}p=p->next;}if(flag==0){printf("没有找到该图书名的图书!\n");}else{printf(" 你所查询的图书信息如下\n");printf("=========================================================================\n" );printf(" ** 编号图书名作者名类别出版时间价格**\n");while(p!=NULL){if(p->time==a){printf("** %d %s %s %s %s %d %.2f **\n",p->num,p->bname,p->wname,p->press,p->sort,p->time,p->price);flag=1;}p=p->next;}printf("=========================================================================\n" );}}//修改图书信息void xiugai(struct book *head){int a,b;char c;struct book *p;printf("请输入要修改的图书编号:");scanf("%d",&a);p=head;while(p!=NULL){if(p->num==a)break;p=p->next;}if(p==NULL){printf("没有找到该编号的图书!\n");getchar();}else{printf("============================================================\n");printf(" ** 1-编号2-图书名3-作者名**\n");printf(" ** 4- 5-类别6-出版时间**\n");printf(" ** 7-价格8-修改全部0-放弃修改**\n");printf("============================================================\n");printf("请选择你要修改的信息编号:");scanf("%d",&b);getchar();switch(b){case 1:printf("请输入新编号:");scanf("%d",&p->num);printf("修改成功!\n");getchar();break;case 2:printf("请输入新图书名:");gets(p->bname);printf("修改成功!\n");break;case 3:printf("请输入新作者名:");gets(p->wname);printf("修改成功!\n");break;case 4:printf("请输入新出版社:");gets(p->press);printf("修改成功!\n");break;case 5:printf("请输入新类别:");gets(p->sort);printf("修改成功!\n");break;case 6:printf("请输入新出版时间:");scanf("%d",&p->time);printf("修改成功!\n");getchar();break;case 7:printf("请输入新价格:");scanf("%f",&p->price);printf("修改成功!\n");getchar();break;case 8:printf("请输入新图书编号:");scanf("%d",&p->num);printf("请输入新图书名:");scanf("%s",p->bname);getchar();printf("请输入新作者名:");scanf("%s",p->wname);getchar();printf("请输入新出版社:");scanf("%s",p->press);getchar();printf("请输入新类别:");scanf("%s",p->sort);getchar();printf("请输入新出版时间:");scanf("%d",&p->time);getchar();printf("请输入新价格:");scanf("%f",&p->price);getchar();printf("修改成功!\n");getchar();break;case 0:break;default :printf("您的输入有误!\n");break;}printf("是否将修改后的信息保存到文件中?(y/n)\n");scanf("%c",&c);getchar();switch(c){case 'n':break;case 'y':fprint(head);printf("保存成功!\n");getchar();break;}}}//图书排序void paixu(struct book *head){int a;printf("================================================================\n");printf(" ** 1-按图书编号排序2-按出版时间排序**\n");printf(" ** 3-按图书价格排序4-按图书名排序**\n");printf(" ** 5-按作者名排序0-取消排序操作**\n");printf("================================================================\n");printf("请输入您选择的编号:");scanf("%d",&a);getchar();switch(a){case 0:break;case 1:num_paixu(head);break;case 2:time_paixu(head);break;case 3:price_paixu(head);break;case 4:bname_paixu(head);break;case 5:wname_paixu(head);break;default:printf("您的输入有误!\n");break;}}//按图书编号排序void num_paixu(struct book *head){struct book *a[1000],*p,*p1,*temp;int i,k,index,n=0;char b;p1=head;for(p=head;p;p=p->next)n++;for(i=0;i<n;i++){a[i]=p1;p1=p1->next;}for(k=0;k<n-1;k++){index=k;for(i=k+1;i<n;i++){if(a[i]->num<a[index]->num)index=i;}temp=a[index];a[index]=a[k];a[k]=temp;}printf("排序成功!\n");printf("是否显示排序结果?(y/n)\n");scanf("%s",&b);getchar();switch(b){case 'n':break;case 'y':printf("================================================================\n");printf(" ** 编号图书名作者名类别出版时间价格**\n");for(i=0;i<n;i++){printf("** %d %s %s %s %s %d %.2f **\n",a[i]->num,a[i]->bname,a[i]->wname,a[i]->press,a[i]->sort,a[i]->time,a[i]->price);}printf("================================================================\n");break;default:printf("您的输入有误!\n");break;}}//按出版时间排序void time_paixu(struct book *head){struct book *a[1000],*p,*p1,*temp;int i,k,index,n=0;char b;p1=head;for(p=head;p;p=p->next)n++;for(i=0;i<n;i++){a[i]=p1;p1=p1->next;}for(k=0;k<n-1;k++){index=k;for(i=k+1;i<n;i++){if(a[i]->time<a[index]->time)index=i;}temp=a[index];a[index]=a[k];a[k]=temp;}printf("排序成功!\n");printf("是否显示排序结果?(y/n)\n");scanf("%s",&b);getchar();switch(b){case 'n':break;case 'y':printf("===============================================================\n");printf(" ** 编号图书名作者名类别出版时间价格**\n");for(i=0;i<n;i++){printf("** %d %s %s %s %s %d %.2f **\n",a[i]->num,a[i]->bname,a[i]->wname,a[i]->press,a[i]->sort,a[i]->time,a[i]->price);}printf("===============================================================\n");break;default:printf("您的输入有误!\n");break;}}//按图书价格排序void price_paixu(struct book *head){struct book *a[1000],*p,*p1,*temp;int i,k,index,n=0;char b;p1=head;for(p=head;p;p=p->next)n++;for(i=0;i<n;i++){a[i]=p1;p1=p1->next;}for(k=0;k<n-1;k++){index=k;for(i=k+1;i<n;i++){if(a[i]->price<a[index]->price)index=i;}temp=a[index];a[index]=a[k];a[k]=temp;}printf("排序成功!\n");printf("是否显示排序结果?(y/n)\n");scanf("%s",&b);getchar();switch(b){case 'n':break;case 'y':printf("===============================================================\n");printf(" ** 编号图书名作者名类别出版时间价格**\n");for(i=0;i<n;i++){printf("** %d %s %s %s %s %d %.2f **\n",a[i]->num,a[i]->bname,a[i]->wname,a[i]->press,a[i]->sort,a[i]->time,a[i]->price);}printf("===============================================================\n");break;default:printf("您的输入有误!\n");break;}}//按图书名排序void bname_paixu(struct book *head){struct book *a[1000],*p,*p1,*temp;int i,k,index,n=0;char b;p1=head;for(p=head;p;p=p->next)n++;for(i=0;i<n;i++){a[i]=p1;p1=p1->next;}for(k=0;k<n-1;k++){index=k;for(i=k+1;i<n;i++){if(strcmp(a[index]->bname,a[i]->bname)>0)index=i;}temp=a[index];a[index]=a[k];a[k]=temp;}printf("排序成功!\n");printf("是否显示排序结果?(y/n)\n");scanf("%s",&b);getchar();switch(b){case 'n':break;case 'y':printf("===============================================================\n");printf(" ** 编号图书名作者名类别出版时间价格**\n");for(i=0;i<n;i++){printf("** %d %s %s %s %s %d %.2f **\n",a[i]->num,a[i]->bname,a[i]->wname,a[i]->press,a[i]->sort,a[i]->time,a[i]->price);}printf("===============================================================\n");break;default:printf("您的输入有误!\n");break;}}//按作者名排序void wname_paixu(struct book *head){struct book *a[1000],*p,*p1,*temp;int i,k,index,n=0;char b;p1=head;for(p=head;p;p=p->next)n++;for(i=0;i<n;i++){a[i]=p1;p1=p1->next;}for(k=0;k<n-1;k++){index=k;for(i=k+1;i<n;i++){if(strcmp(a[index]->wname,a[i]->wname)>0)index=i;}temp=a[index];a[index]=a[k];a[k]=temp;}printf("排序成功!\n");printf("是否显示排序结果?(y/n)\n");scanf("%s",&b);getchar();switch(b){case 'n':break;case 'y':printf("===============================================================\n");printf(" ** 编号图书名作者名类别出版时间价格**\n");for(i=0;i<n;i++){printf("** %d %s %s %s %s %d %.2f **\n",a[i]->num,a[i]->bname,a[i]->wname,a[i]->press,a[i]->sort,a[i]->time,a[i]->price);}printf("===============================================================\n");break;default:printf("您的输入有误!\n");break;}}。

相关文档
最新文档