#include<stdio.h> #include<stdlib.h> #include<string.h>
//图书结构体 struct book1 { char isbn[20];//编号 char name[20];//书名 char userName[20];//借书用户 char userBook[20];//所借书籍 char userTime[20];//借书时间 char author[20];//作者 int amount;//库存量 }; struct book { struct book1 book1;//图书信息结构体 struct book *next; };
void bookManagement();//主界面 void addBook();//增加书籍 void show(); struct book *read();//读取文件存储到链表内 void write(struct book *head);//写入书籍 void deleteBook();//删除 书籍 void showBook();//显示书籍 void changeBook();//修改书籍 void findBook();//查找书籍 void addInventory();//增加库存 void lendBook();//借书 void lendWrite();//借书写入文件 void lendDisplay();//借书信息 void returnBook();//还书
int main() { bookManagement();//主界面 system(“pause”); return 0;
} void bookManagement() { FILE fp; int n=1; while(n!=0) { printf("-------------------图书管理系统-----------------\n"); printf(" \n"); printf(" *\n"); printf("*1.增加图书\t2.删除图书\t3.修改图书信息 *\n"); printf("*4.显示图书\t5.查找图书\t6.增加库存 *\n"); printf(“7.借书 \t8.还书 \t9.借阅情况 \n"); printf(" \n"); printf("------------------------------------------------\n”); printf("\n"); printf(“请选择功能,退出请按0:”); scanf("%d",&n); fp=fopen(“libraries.txt”,“r”); if((n>=3&&n<=9)&&(fp==NULL||fgetc(fp)EOF)) { printf(“没有书籍\n”); system(“pause”); } else { switch(n) { case 1:addBook();break; case 2:deleteBook();break; case 3:changeBook();break; case 4:showBook();break; case 5:findBook();break; case 6:addInventory();break; case 7:lendBook();break; case 8:returnBook();break; case 9:lendDisplay();break; } } fclose(fp); } printf("\n"); if(n0) printf(“您已成功退出图书管理系统!\n”); }
void addBook() //添加书籍 { struct book *p=NULL,*p1=NULL,*head=NULL,p2; char n=1; system(“cls”); printf(“编号 书名 作者 数量\n”); p=head=read(); p2=(struct book)malloc(sizeof(struct book)); scanf("%s%s%s%d",p2->book1.isbn,p2->book1.name,p2->book1.author,&p2->book1.amount); while§ { if(strcmp(p->book1.name,p2->book1.name)0) { p->book1.amount++; printf(“添加成功\n”); printf(“原书籍数量增加\n”); break; } else p=p->next; } if(headNULL) head=p2; else { p1=head; while(p1->next!=NULL) p1=p1->next; p1->next=p2; p2->next=NULL; } write(head); n=getchar(); printf(“书籍添加成功\n”); show(); system(“pause”); }
void show() //显示所有书籍 { struct book1 book1; FILE *fp; //system(“cls”); printf(“图书馆藏书\n”); printf("\n"); fp=fopen(“libraries.txt”,“r”); if(!fp) { printf(“文件打开失败\n”); exit(0); } else { printf(“编号 书名 作者 数量\n”); while(fscanf(fp,"%s%s%s%d",book1.isbn,book1.name,book1.author,&book1.amount)!=EOF) { printf("%-20s%-15s%-15s%-14d\n",book1.isbn,book1.name,book1.author,book1.amount); } } fclose(fp); printf("\n"); }
struct book* read() //取数据存放在链表 { FILE *fp; struct book1 book1; struct book *p,*tail=NULL,head=NULL; fp=fopen(“libraries.txt”,“r”); if(!fp) { printf(“文件打开失败\n”); exit(0); } while(fscanf(fp,"%s%s%s%d",book1.isbn,book1.name,book1.author,&book1.amount)!=EOF) { p=(struct book)malloc(sizeof(struct book)); p->book1=book1; if(head==NULL) head=p; if(tail!=NULL) tail->next=p; tail=p; tail->next=NULL; } fclose(fp); return head; }
void write(struct book *head) //写入文件 { FILE *fp; struct book *p=NULL; fp=fopen(“libraries.txt”,“w”); if(!fp) { printf(“文件打开失败\n”); exit(0); } p=head; while§ {
fprintf(fp,"%-20s%-15s%-15s%-14d",p->book1.isbn,p->book1.name,p->book1.author,p->book1.amount);
p=p->next;
if(p == NULL){
break;
}
}
fclose(fp);
}
void deleteBook() //删除书籍 { struct book *p1=NULL,*p2=NULL,*head=NULL; char name[20]={’\0’}; char ch; p1=p2=head=read(); system(“cls”); show(); printf("\n"); printf(“输入要删除的书籍名称:”); scanf("%s",name); while(strcmp(p1->book1.name,name)!=0&&p1!=NULL) p1=p1->next; if(headp1) head=head->next; else { while(p2) { if(p2->nextp1) { p2->next=p1->next; free(p1); break; } p2=p2->next; } } printf(“删除成功!\n”); write(head); show(); printf("\n"); system(“pause”); }
void showBook(){ system(“cls”); show(); system(“pause”); }
void changeBook() //修改书籍 { struct book *p,head,q=NULL; system(“cls”); int n=1,amount; char isbn[20]={’\0’},name[20]={’\0’},author[20]={’\0’},print[20]={’\0’},name1[20]={’\0’},zone[15]={’\0’}; float price; p=head=read(); show(); printf(“输入要修改的图书书名\n”); scanf("%s",name); while§ { if(strcmp(p->book1.name,name)==0) { q=p; break; } else p=p->next; } while(n!=0) { printf(“请选择您要修改的信息,退出修改请按0\n”); printf("---------------------------------------------------------------\n"); printf(“1.编号 2.修改书名 3.修改作者信息 4.修改数量 \n"); printf("---------------------------------------------------------------\n”); scanf("%d",&n); switch(n) { case 1: printf(“编号”); scanf("%s",isbn); strcpy(q->book1.isbn,isbn);break; case 2: printf(“输入新书名:”); scanf("%s",name1); strcpy(q->book1.name,name1);break; case 3: printf(“输入新作者信息:”); scanf("%s",author); strcpy(q->book1.author,author);break; case 4: printf(“输入新数量:”); scanf("%d",&amount); q->book1.amount=amount;break; } } write(head); printf(“修改成功!\n”); show(); system(“pause”); }
void findBook() { struct book *head=NULL,*p; char name[20]={’\0’}; int flag=0; char n=1; system(“cls”); head=read(); p=head; printf(“输入书籍名查找书籍):”); scanf("%s",name); while§ { if(strcmp(p->book1.name,name)==0||strcmp(p->book1.isbn,name)0) { printf(“编号 书名 作者 数量 \n”); printf("%-20s%-15s%-15s%-14d\n",p->book1.isbn,p->book1.name,p->book1.author,p->book1.amount); printf("\n"); flag=1; break; } else p=p->next; } if(flag0) printf(“没有此书籍,请确认书名是否正确!\n\n”); system(“pause”); }
void addInventory() { struct book *p,*head,*q=NULL; system(“cls”); int n=1,amount; char isbn[20]={’\0’},name[20]={’\0’},author[20]={’\0’},print[20]={’\0’},name1[20]={’\0’},zone[15]={’\0’}; float price; p=head=read(); show(); printf(“输入要增加库存的书籍”); scanf("%s",name); while§ { if(strcmp(p->book1.name,name)==0) { q=p; break; } else p=p->next; }
printf("输入增加库存数:");
scanf("%d",&amount);
q->book1.amount= p->book1.amount + amount;
write(head);
printf("修改成功!\n");
show();
system("pause");
}
void lendBook()//借书 { struct book *p=NULL,*p1=NULL,*head=NULL,p2; char n=1; system(“cls”); printf(“借阅书名 借阅人 借阅时间\n”); p=head=read();//从文件读取数据 p2=(struct book)malloc(sizeof(struct book)); scanf("%s%s%s",p2->book1.userBook,p2->book1.userName,p2->book1.userTime);
if(head==NULL)
head=p2;
else
{
p1=head;
while(p1->next!=NULL)
p1=p1->next;
p1->next=p2;
p2->next=NULL;
}
lendWrite(head,1);
n=getchar();
printf("书籍借阅成功\n");
// lendDisplay();
system("pause");
}
void lendWrite(struct book *head,int n) //借阅写入文件 {
FILE *fp;
struct book *p=NULL;
fp=fopen("lendb.txt","a+");
if(!fp)
{
printf("文件打开失败\n");
exit(0);
}
p=head;
// printf("%s%s%s",p->book1.userBook,p->book1.userName,p->book1.userTime);
if (n != 0){
for (int i = 0; i < 4; i++)
{
p = p->next;
}
}
while(p)
{
// printf("%s--_%s---%s\n",p->book1.userBook,p->book1.userName,p->book1.userTime);
// printf("------------------\n");
fprintf(fp,"%-20s%-15s%-15s",p->book1.userBook,p->book1.userName,p->book1.userTime);
p=p->next;
if(p == NULL){
break;
}
}
fclose(fp);
}
void lendDisplay() //书籍借阅情况 { struct book1 book1; FILE *fp; //system(“cls”); printf(“借阅信息\n”); printf("\n"); fp=fopen(“lendb.txt”,“r”); if(!fp) { printf(“文件打开失败\n”); exit(0); } else { printf(“借阅书名 借阅人 借阅时间\n”); while(fscanf(fp,"%s%s%s",book1.userBook,book1.userName,book1.userTime)!=EOF) { printf("%-20s%-15s%-15s%\n",book1.userBook,book1.userName,book1.userTime); } } fclose(fp); printf("\n"); }
void returnBook()//还书 { struct book *p1=NULL,*p2=NULL,*head=NULL; char name[20]={’\0’}; char ch; p1=p2=head=read(); // system(“cls”);
printf("\n");
printf("还书人 还书书名:\n");
scanf("%s",name);
// while(strcmp(p1->book1.userBook,name)!=0&&p1!=NULL)//第一本书和要删除的书名不同则一直寻找
// p1=p1->next;
if(head==p1)
head=head->next;
else
{
while(p2)
{
if(p2->next==p1)
{
p2->next=p1->next;
free(p1);
break;
}
p2=p2->next;
}
}
printf("还书成功\n");
// lendWrite(head,0);
// lendDisplay();
printf("\n");
system("pause");
}
|