图书信息包含、书号、书名、作者、出版社、单价等信息
能录入图书信息
能够列表显示所有图书信息
能按照书号、书名查找图书信息
能按照书号修改、删除图书信息
能够将所有图书保存到文件中,并能从文件中读入数据进行各项操作
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Library
{
char name[20] = { 0 };
int num = 0;
char author[20] = { 0 };
char press[20] = { 0 };
int price = { 0 };
};
struct Node {
struct Library data;
struct Node* next;
};
struct Node* creatList()
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
//head
headNode->next = NULL;
return headNode;
}
struct Node* creatNode(struct Library data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = data;
newNode->next = NULL;
return newNode;
}
int Linklen(struct Node* headNode) {
int k = 0;
while (headNode->next != NULL)
{
k++;
headNode = headNode->next;
}
return k;
}
void printList(struct Node* headNode)//打印链表
{
struct Node* pMove = headNode->next;
printf("书名\t书号\t作者\t出版社\t单价\n");
while (pMove)
{
printf("%s\t%d\t%s\t%s\t%d\n", pMove->data.name, pMove->data.num, pMove->data.author, pMove->data.press, pMove->data.price);
pMove = pMove->next;
}
printf("\n");
}
void insertNode(struct Node* headNode, struct Library data)//头插入
{
struct Node* newNode = creatNode(data);
newNode->next = headNode->next;
headNode->next = newNode;
}
void deletNode(struct Node* headNode)
{
int num;
printf("请输入要删除书的书号:");
setbuf(stdin, NULL);
scanf("%d", &num);
struct Node* posNode = headNode->next;
struct Node* posNodeFront = headNode;
if (posNode == NULL)
printf("无法删除\n");
else
{
while (posNode->data.num != num)
{
posNodeFront = posNode;
posNode = posNodeFront->next;
if (posNode == NULL)
{
printf("查无此书\n");
return;
}
}
posNodeFront->next = posNode->next;
free(posNode);
printf("删除成功!\n");
}
}
void changeNode(struct Node* headNode)
{
int opt=0;
printf("请输入要修改书的书号:");
setbuf(stdin, NULL);
scanf("%d", &opt);
struct Node* posNode = headNode->next;
struct Node* posNodeFront = headNode;
if (posNode->next == NULL)
printf("无法修改\n");
else
{
while (posNode->data.num != opt)
{
posNodeFront = posNode;
posNode = posNodeFront->next;
if (posNode == NULL)
{
printf("查无此书\n");
return;
}
}
printf("输入修改后名字\n");
setbuf(stdin, NULL);
gets_s(posNode->data.name);
printf("输入修改后作者\n");
gets_s(posNode->data.author);
printf("输入修改后出版社\n");
gets_s(posNode->data.press);
printf("输入修改后价格\n");
scanf("%d",&posNode->data.price);
printf("修改成功!\n");
}
}
void addNode(struct Node* list) {
//struct Node* list = creatList();
struct Library info;
char choice = 0;
do
{
printf("输入添加图书 名字 书号 作者 出版社 单价(用空格隔开)\n");
setbuf(stdin, NULL);
scanf("%s%d%s%s%d", &info.name, &info.num, &info.author, &info.press, &info.price);
insertNode(list, info);
printf("添加成功\n");
printf("是否继续输入:(Y/N)\n");
setbuf(stdin, NULL);
list->next;
scanf("%c", &choice);
if (choice == 'n' || choice == 'N') {
choice = -1;
}
} while (choice != -1);
}
void seekNodeByname(struct Node* headNode) {
struct Node* posNode = headNode;
char name[20];
printf("请输入书名;");
setbuf(stdin, NULL);
scanf("%s", name);
if (posNode == NULL)
printf("图书馆为空");
else
{
do
{
if (posNode->next == NULL)
{
printf("查无此书\n");
return;
}
posNode = posNode->next;
} while (strcmp(posNode->data.name, name) != 0);
printf("书名\t书号\t作者\t出版社\t单价\n");
printf("%s\t%d\t%s\t%s\t%d\n", posNode->data.name, posNode->data.num, posNode->data.author, posNode->data.press, posNode->data.price);
}
}
void seekNodeBynum(struct Node* headNode) {
struct Node* posNode = headNode;
int num;
printf("请输入书号;");
setbuf(stdin, NULL);
scanf("%d", &num);
if (posNode == NULL)
printf("图书馆为空");
else
{
do
{
if (posNode->next == NULL)
{
printf("查无此书!\n");
return;
}
posNode = posNode->next;
} while (posNode->data.num != num);
printf("书名\t书号\t作者\t出版社\t单价\n");
printf("%s\t%d\t%s\t%s\t%d\n", posNode->data.name, posNode->data.num, posNode->data.author, posNode->data.press, posNode->data.price);
}
}
void saveNode(struct Node* headNode)//保存到文件
{
FILE* fp = NULL;
fopen_s(&fp, "Lib.txt", "w");
struct Node* p = headNode->next;
int num = Linklen(headNode);
if (fp == NULL) exit(1);
fprintf(fp, "%d\n", num);
while (p != nullptr)
{
fprintf(fp, "%s %d %s %s %d\n", p->data.name, p->data.num, p->data.author, p->data.press, p->data.price);
p = p->next;
}
fclose(fp);
fp = NULL;
}
void loadNode(struct Node* headNode)
{
FILE* fp = NULL;
fopen_s(&fp, "Lib.txt", "r");
int sum = 0;
struct Library txt;
fscanf_s(fp, "%d", &sum);
for (int i = 0; i < sum; i++)
{
fscanf_s(fp, "%s", txt.name, 20);
fscanf_s(fp, "%d", &txt.num);
fscanf_s(fp, "%s", txt.author, 20);
fscanf_s(fp, "%s", txt.press, 20);
fscanf_s(fp, "%d", &txt.price);
insertNode(headNode, txt);
}
fclose(fp);
fp = NULL;
}
void menu()
{
printf("*************************\n");
printf("* 图书管理系统 *\n");
printf("*************************\n");
printf("* 1.添加图书 *\n");
printf("* 2.按书名查询图书 *\n");
printf("* 3.按书号查询图书 *\n");
printf("* 4.按书号修改图书 *\n");
printf("* 5.按书号删除图书 *\n");
printf("* 6.显示所有图书 *\n");
printf("* 7.退出 *\n");
printf("*************************\n");
}
int main()
{
menu();
struct Node* List = creatList();
int choice,option;
loadNode(List);
do
{
printf("请输入选项:");
setbuf(stdin, NULL);
scanf("%d", &choice);
option = choice;
switch (choice)
{
case 1:addNode(List); saveNode(List); break;
case 2:seekNodeByname(List); break;
case 3:seekNodeBynum(List); break;
case 4:changeNode(List);saveNode(List); break;
case 5:deletNode(List);saveNode(List); break;
case 6:printList(List); break;
case 7:break;
}
} while (option != 7);
system("pause");
return 0;
}
PS:第一次写,求各位大佬指点一下。