#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LEN sizeof(struct book)
struct book{
char name[20];
int status;
char author[20];
struct book* next;
};
struct book *head=NULL,*p1,*p2;
char c[2][10]={"可借","已借出"};
void print(){
printf("=======欢迎来到至诚书库管理系统=======\n");
printf(" 请输入您的需求:\n");
printf(" 1.添加书籍\n");
printf(" 2.查看书籍\n");
printf(" 3.删除书籍\n");
printf(" 4.借出书籍\n");
printf(" 5.归还书籍\n");
printf(" 0.退出程序\n");
printf("======================================\n");
}
void add(){
if (head==NULL) head=p1;
else
p2->next=p1;
p2=p1;
if (head!=NULL) p2->next=NULL;
printf("请输入书籍名字:");
scanf("%s",p1->name);
printf("请输入作者名字:");
scanf("%s",p1->author);
p1->status=0;
p1=(struct book *)malloc(LEN);
printf("成功添加《%s》到书库\n",p2->name);
}
void check() {
int n,f=0;
char a[20],x[3]="《",y[3]="》";
struct book *p=head;
printf("请输入查找方式:\n");
printf("1.按书名查找\n");
printf("2.按作者查找\n");
printf("3.查看所有书籍\n");
scanf("%d",&n);
if (n==1) {
printf("请输入书名:");
scanf("%s",a);
while (strcmp(p->name,a)!=0&&p->next!=NULL) p=p->next;
if (strcmp(p->name,a)==0) {
printf("%-20s%-20s%-20s\n","书名","作者","状态");
printf("%-20s%-20s%-20s\n",strcat(strcat(x,p->name),y),p->author,c[p->status]);
f=1;
}
if (f==0) printf("查无此书!");
}
if (n==2) {
printf("请输入作者名:");
scanf("%s",a);
printf("%-20s%-20s%-20s\n","书名","作者","状态");
while (p!=NULL) {
if (strcmp(p->author,a)==0) {
strcpy(x,"《");
printf("%-20s%-20s%-20s\n",strcat(strcat(x,p->name),y),p->author,c[p->status]);
f=1;
}
p=p->next;
}
if (f==0) printf("查无此书!");
}
if (n==3) {
printf("%-20s%-20s%-20s\n","书名","作者","状态");
while (p!=NULL) {
strcpy(x,"《");
printf("%-20s%-20s%-20s\n",strcat(strcat(x,p->name),y),p->author,c[p->status]);
p=p->next;
}
}
}
void del() {
int f=0;
struct book *p=head;
char a[20];
printf("请输入您要删除的书籍:");
scanf("%s",a);
while(p!=NULL) {
if (strcmp(head->name,a)==0) {
head=head->next;
f=1;
break;
}
else if(strcmp(p->next->name,a)==0) {
p->next=p->next->next;
f=1;
break;
}
if (p!=NULL) p=p->next;
else break;
}
if (f==1) printf("删除成功!");
if (f==0) printf("删除失败,没有该书!");
}
void borrow() {
struct book *p=head;
char a[20];
printf("请输入您要借的书籍:");
scanf("%s",a);
while (strcmp(p->name,a)!=0&&p->next!=NULL) p=p->next;
if (strcmp(p->name,a)==0&&p->status==0) {
printf("《%s》借书成功!",p->name);
p->status=1;
}
else printf("借书失败!");
}
void revert() {
int f=0;
struct book *p=head;
char a[20];
printf("请输入您要归还的书籍:");
scanf("%s",a);
while (strcmp(p->name,a)!=0&&p->next!=NULL) p=p->next;
if (strcmp(p->name,a)==0) {
if (p->status==1) {
printf("还书成功!");
p->status=0;
f=1;
}
}
if (f==0) printf("还书失败!无需归还此书。");
}
void tips() {
printf("输入回车返回\n");
getchar();
getchar();
return;
}
void addbook(char a[20],int n,char b[20]) {
if (head==NULL) head=p1;
else {
p2->next=p1;
p2=p1;
}
if (head!=NULL) p2->next=NULL;
strcpy(p1->name,a);
strcpy(p1->author,b);
p1->status=n;
p1=(struct book *)malloc(LEN);
}
int main(void)
{
p1=p2=(struct book *)malloc(LEN);
addbook("活着",1,"余华");
addbook("龙族",0,"江南");
addbook("许三观卖血记",0,"余华");
addbook("1984",0,"乔治-奥威尔");
while (1) {
print();
int op;
printf("请选择:");
scanf("%d",&op);
switch (op) {
case 1: printf("-->添加书籍\n");add(); break;
case 0:printf("程序退出\n");return 0;
case 2: printf("-->查看书籍\n");check(); break;
case 3: printf("-->删除书籍\n");del(); break;
case 4: printf("-->借出书籍\n");borrow(); break;
case 5: printf("-->归还书籍\n");revert(); break;
default: printf("请正确输入!");
}
tips();
system("cls");
}
return 0;
}