通过按不同的序号实现:
1:图书馆系统的建立
2:输入图书基本信息
3:通过顺序查找
4:通过价格查找
5:图书名查找
6:插入
7:删除
8:输出
9:排序
10:借书
11:还书
0:退出
#include
#include
#include
#define N 20
typedef int datatype_t;
struct bookInfo
{
char name[N];//书名
char name1[N];//书号
float price;//价格
int num;//数量
};
struct Node//节点结构体的定义
{
struct bookInfo data;
struct Node *next;
};
struct Node *list = NULL;//全局链表为空,单链表参数传递
//创建表头
struct Node *createHead()//申请头节点包含内存
{
//动态申请
struct Node *headNode = (struct Node *)malloc(sizeof(struct Node));//申请内存
headNode->next = NULL;
return headNode;
};
//创建结点
struct Node *createNode(struct bookInfo data)
{
struct Node *newNode = (struct Node *)malloc(sizeof(struct Node));//申请内存
newNode->data= data;
newNode->next= NULL;
return newNode;
};
//输入头插case2
void insertNodeByhead(struct Node *headNode, struct bookInfo data)
{
struct Node* newNode = createNode(data);
newNode->data=data;
newNode->next = headNode->next;
headNode->next = newNode;
}
//指定插入case 6
void insertAppoint(struct Node *headNode,struct bookInfo data, datatype_t pos)
{
datatype_t i=0;//计数器
struct Node* newNode = createNode(data);
while(i < pos && headNode->next != NULL)
{
headNode=headNode->next;
i++;
}
newNode->next = headNode->next;
headNode->next = newNode;
}
//指定删除
void deleteNodeByName(struct Node *headNode, char *bookName)
{
struct Node *posLeftNode = headNode;//申请头节点
struct Node *posNode = headNode->next;
while(posNode != NULL && strcmp(posNode->data.name,bookName))//查找删除目标,图书名比较
{
posLeftNode = posNode;
posNode = posLeftNode->next;
}
if(posNode == NULL)
{
printf("没有找到书籍,删除失败");
return NULL;
}
else
{
printf("删除成功!");
posLeftNode->next = posNode->next;
free(posNode);
posNode = NULL;
}
}
struct Node *searchByName(struct Node *headNode,char *bookName)//图书名
{
struct Node *posNode = headNode ->next;
while (posNode != NULL && strcmp(posNode->data.name,bookName))
{
posNode = posNode->next;
}
return posNode;
};
struct Node *searchByName1(struct Node *headNode,char *bookName1)//顺序指针它是地址
{
struct Node *posNode = headNode ->next;
while (posNode != NULL && strcmp(posNode->data.name1,bookName1))
{
posNode = posNode->next;
}
return posNode;
};
struct Node *searchByprice(struct Node *headNode,float bookPrice)//价格要用值,
{
struct Node *posNode = headNode ->next;
while (posNode != NULL && posNode->data.price !=bookPrice)
{
posNode = posNode->next;
}
return posNode;
};
void printlist(struct Node *headNode)//打印链表中的数据
{
struct Node *pMove = headNode->next;
printf("书名\t 书号 \t价格\t数量\t\n");
while(pMove != NULL)
{
printf("%s\t %s\t%.2f\t %d\n",pMove->data.name,pMove->data.name1,pMove->data.price,pMove->data.num);
pMove=pMove->next;
}
}
void makeMenu()
{
puts(" ******************图书信息管理系统**************** ");
puts(" ");
puts(" ");
puts(" 1.建立 ");
puts(" 2.输入 ");
puts(" 3.顺序查找 ");
puts(" 4.价格查找 ");
puts(" 5.图书名查找 ");
puts(" 6.插入 ");
puts(" 7.删除 ");
puts(" 8.输出 ");
puts(" 9.排序 ");
puts(" 10.借书 ");
puts(" 11.还书 ");
puts(" 0.退出 ");
puts(" 请按相应的数字选择选项 ");
puts("请输入(0-11):>");
}
//文件操作,保存数据以免每次都要重新输入数据
void saveInfoToFile(const char *fileName,struct Node *headNode)//存储操作
{
FILE *fp = fopen(fileName, "w");//打开文件 写
struct Node *pMove= headNode->next;
while(pMove != NULL)
{
fprintf(fp,"%s\t %s\t%.2f\t %d\n",pMove->data.name,pMove->data.name1,pMove->data.price,pMove->data.num);//打印到文件里面
pMove=pMove->next;
}
fclose(fp);
}
//读操作
void readInfoFromFile(const char *fileName,struct Node *headNode)
{
FILE *fp = fopen(fileName, "r");
if(fp == NULL)
{
fp=fopen(fileName,"w+");//创建
}
struct bookInfo tempData;//临时信息
while(fscanf(fp,"%s\t%s\t%f\t%d\n",tempData.name,tempData.name1,&tempData.price,&tempData.num) != EOF)
{
insertNodeByhead(list,tempData);
}
fclose(fp);
}
void bubbleSortList(struct Node *headNode)//排序从小到大
{
struct Node *p = headNode->next;
struct Node *q = headNode->next;
for( ;p != NULL; p = p->next)
{
for(; q->next != NULL; q = q->next)
{
if(q->data.price > q->next->data.price)
{
struct bookInfo tempData = q ->data;
q->data = q->next->data;
q->next->data = tempData;
}
}
}
printlist(headNode);
}
//交互
void keyDown()
{
int i;
int userkey = 0;
struct bookInfo tempBook; //产生一个临时变量存储书籍信息
struct Node *result = NULL;
scanf("%d",&userkey);
switch(userkey)
{
case 0:
printf("[退出]\n");
printf("退出成功!\n");
system("pause");//防止闪屏
exit(0); //关闭整个程序
break;
case 1:
printf("[建立]\n");
puts(" /****************************图书信息管理系统***********************/ ");
puts(" -----------------------------图书信息表创建-------------------------- ");
puts(" ");
puts(" ");
puts(" 成功建立图书信息表 ");
puts(" ");
puts(" ");
puts(" 请按相应的数字建选项 ");
break;
case 2:
printf("[输入]\n");
printf("输入书籍的信息(书名,书号,价格,数量):>");
scanf("%s%s%f%d",tempBook.name,tempBook.name1,&tempBook.price,&tempBook.num);//字符串不需要取地址&
insertNodeByhead(list,tempBook);
saveInfoToFile("book.txt", list);
break;
case 3:
printf("[顺序查找]\n");
printf("请输入要查询的书号:>");
scanf("%s",tempBook.name1);
result = searchByName1(list,tempBook.name1);
if(result==NULL)
{
printf("未找到相关书籍!\n");
}
else
{
printf("书名\t 书号\t 价格\t 数量\t\n");
printf("%s\t %s\t%.2f\t %d\n",result->data.name,result->data.name1,result->data.price,result->data.num);
}
break;
case 4:
printf("[价格查找]\n");
printf("请输入价格:>");
scanf("%f",&tempBook.price);
result=searchByprice(list,tempBook.price);
if(result==NULL)
{
printf("未找到相关书籍!\n");
}
else
{
printf("书名\t书号\t价格\t数量\t\n");
printf("%s\t %s\t%.2f\t %d\n",result->data.name,result->data.name1,result->data.price,result->data.num);
}
break;
case 5:
printf("[图书名查找]\n");
printf("请输入要查找的书名:>");
scanf("%s",tempBook.name);
result = searchByName(list,tempBook.name);
if(result==NULL)
{
printf("未找到相关书籍!\n");
}
else
{
printf("书名\t 书号\t 价格\t 数量\t\n");
printf("%s\t%s\t %.2f\t %d\n",result->data.name,result->data.name1,result->data.price,result->data.num);
}
break;
case 6:
printf("[插入]\n");
printf("输入书籍的信息(书名,书号,价格,数量)与插入位置:>");
scanf("%s%s%f%d%d",tempBook.name,tempBook.name1,&tempBook.price,&tempBook.num,&i);
insertAppoint(list,tempBook,i);
saveInfoToFile("book.txt", list);
break;
case 7:
printf("[删除]\n");
printf("请输入要删除的书名:>");
scanf("%s",tempBook.name);
deleteNodeByName(list,tempBook.name);
saveInfoToFile("book.txt", list);//同步文件
break;
case 8:
printf("[输出]\n");
printlist(list);
break;
case 9:
printf("[排序]\n");
bubbleSortList(list);
break;
case 10:
printf("[借书]\n");
printf("请输入需要借的书名:>");
scanf("%s",tempBook.name);
result=searchByName(list,tempBook.name);
if(result == NULL)
{
printf("没有相关的图书。\n");
}
else
{
if(result->data.num > 0)
{
result->data.num--;
printf("借书成功。\n");
}
else
{
printf("当前书籍已全部借出,借书失败。");
}
}
break;
case 11:
printf("[还书]\n");
printf("请输入归还的书籍名:>");
scanf("%s",tempBook.name);
result=searchByName(list,tempBook.name);
if(result == NULL)
{
printf("图书库里没有此书,还书失败。\n");
}
else
{
result->data.num++;
printf("还书成功。\n");
}
break;
default:
printf("[输入值超过范围]\n");
system("pause");
exit(0);
break;
}
}
int main()
{
list = createHead();//
readInfoFromFile("book.txt",list);
while(1)
{
makeMenu();
keyDown();
system("pause");
system("cls");
}
system("pause");
return 0;
}