C语⾔:简单实现图书管理系统细节要求:可实现功能如下:
登记书籍
浏览书籍
借阅书籍
归还书籍
删除书籍
查书籍
退出系统
可理解为常规增删查改。
参考代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct bookInfo
{
char name[20];
float price;
char author[20];
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;
}
void insertNodeByHead(struct Node* headNode,struct bookInfo data)
{
struct Node* newNode = createNode(data);
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)
return;
else
{
printf("删除成功!!\n");
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;
}
void printList(struct Node* headNode)
{
struct Node* pMove = headNode->next;
printf("书名\t价格\t作者\t数量\n");
while(pMove != NULL)
{
printf("%s\t%.1f\t%s\t%d\n",pMove->data.name,pMove->data.price,pMove->data.author,pMove->data.num);    pMove = pMove->next;
}
}
printf("-----------------------------------------\n");
printf("\tc语⾔图书管理系统\n");
printf("\t0.退出系统\n");
printf("\t1.登记书籍\n");
printf("\t2.浏览书籍\n");
printf("\t3.借阅书籍\n");
printf("\t4.归还书籍\n");
printf("\t5.删除书籍\n");
printf("\t6.查书籍\n");
printf("-----------------------------------------\n");
printf("请输⼊(0~6):");
}
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%.1f\t%s\t%d\n",pMove->data.name,pMove->data.price,pMove->data.author,pMove->data.num);      pMove = pMove->next;
}
fclose(fp);
}
void readInfoFromFile(const char* fileName,struct Node* headNode)
{
struct bookInfo tempData;
FILE *fp = fopen(fileName,"r");
if(fp == NULL)
{
fp = fopen(fileName,"w+");
}
while(fscanf(fp,"%s\t%f\t%s\t%d\n",tempData.name,&tempData.price,tempData.author,&tempData.num)!=EOF)  {
insertNodeByHead(list,tempData);
}
fclose(fp);
}
int usenum = 0;
struct bookInfo tempBook;
struct Node* result = NULL;
scanf("%d",&usenum);
switch(usenum)
{
case 0:
printf("【退出】\n");
printf("退出成功\n");
system("pause");
exit(0);
break;
case 1:
printf("【登记】\n");
printf("输⼊书籍的信息(书名,价格,作者,数量):\n");
scanf("%s%f%s%d",tempBook.name,&tempBook.price,tempBook.author,&tempBook.num);        insertNodeByHead(list,tempBook);
saveInfoToFile("",list);
break;
case 2:
printf("【浏览】\n");
printList(list);
break;
case 3:
printf("【借阅】\n");
printf("请输⼊要借阅的书名:\n");
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("当前书籍⽆库存,借阅失败!!\n");
}
}
break;
case 4:
printf("【归还】\n");
printf("请输⼊要归还的书名:\n");
scanf("%s",tempBook.name);
result = searchByName(list,tempBook.name);
if(result == NULL)
{
printf("该书来历不明!\n");
}
else
{
result->data.num++;
printf("书籍归还成功!!");
}
break;
case 5:
printf("【删除】\n");
printf("请输⼊要删除书名:\n");
scanf("%s",tempBook.name);
deleteNodeByName(list,tempBook.name);
saveInfoToFile("",list);
break;
case 6:
printf("【查】\n");
printf("请输⼊要查询的书名:\n");
scanf("%s",tempBook.name);
result = searchByName(list,tempBook.name);
if(result == NULL)
{
printf("未到相关信息!\n");
}
else
{
printf("书名\t价格\t作者\t数量\n");
printf("%s\t%.1f\t%s\t%d\n",result->data.name,result->data.price,result->data.author,result->data.num);
c语言算法书籍}
break;
default:
printf("【error】\n");
break;
}
}
int main()
{
list = createHead();
readInfoFromFile("",list);

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。