C语言图书管理系统用结构体,链表,文件
时间: 2023-08-10 13:38:50 浏览: 186
下面是一个简单的 C 语言图书管理系统的实现,使用了结构体、链表和文件的相关知识:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义图书结构体
typedef struct book {
char name[100]; // 书名
char author[50]; // 作者
float price; // 价格
int num; // 数量
struct book *next; // 指向下一本书的指针
} Book;
// 在链表末尾插入新节点
Book *insert(Book *head, Book *new_book) {
if (head == NULL) {
return new_book;
} else {
Book *p = head;
while (p->next != NULL) {
p = p->next;
}
p->next = new_book;
return head;
}
}
// 在链表中查找指定书名的节点
Book *search(Book *head, char *name) {
Book *p = head;
while (p != NULL) {
if (strcmp(p->name, name) == 0) {
return p;
}
p = p->next;
}
return NULL;
}
// 从链表中删除指定节点
Book *delete(Book *head, Book *del_book) {
if (head == NULL) {
return NULL;
} else if (head == del_book) {
head = head->next;
free(del_book);
return head;
} else {
Book *p = head;
while (p != NULL && p->next != del_book) {
p = p->next;
}
if (p != NULL) {
p->next = del_book->next;
free(del_book);
}
return head;
}
}
// 显示链表中所有节点
void display(Book *head) {
Book *p = head;
while (p != NULL) {
printf("书名:%s 作者:%s 价格:%.2f 数量:%d\n", p->name, p->author, p->price, p->num);
p = p->next;
}
}
// 保存链表中所有节点到文件
void save(Book *head, char *filename) {
FILE *fp = fopen(filename, "w");
if (fp == NULL) {
printf("无法打开文件 %s。\n", filename);
} else {
Book *p = head;
while (p != NULL) {
fprintf(fp, "%s %s %.2f %d\n", p->name, p->author, p->price, p->num);
p = p->next;
}
fclose(fp);
printf("保存成功。\n");
}
}
// 从文件中加载所有节点到链表
Book *load(Book *head, char *filename) {
FILE *fp = fopen(filename, "r");
if (fp == NULL) {
printf("无法打开文件 %s。\n", filename);
return head;
} else {
char name[100], author[50], line[200];
float price;
int num;
while (fgets(line, sizeof(line), fp) != NULL) {
sscanf(line, "%s %s %f %d", name, author, &price, &num);
Book *new_book = (Book *)malloc(sizeof(Book));
strcpy(new_book->name, name);
strcpy(new_book->author, author);
new_book->price = price;
new_book->num = num;
new_book->next = NULL;
head = insert(head, new_book);
}
fclose(fp);
return head;
}
}
int main() {
Book *head = NULL;
char filename[] = "books.txt";
head = load(head, filename); // 加载所有节点到链表
while (1) {
printf("1. 添加图书\n");
printf("2. 删除图书\n");
printf("3. 查找图书\n");
printf("4. 显示所有图书\n");
printf("5. 保存所有图书到文件\n");
printf("6. 退出\n");
printf("请选择操作(1-6):");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1: {
char name[100], author[50];
float price;
int num;
printf("请输入书名:");
scanf("%s", name);
if (search(head, name) != NULL) {
printf("该书已存在。\n");
break;
}
printf("请输入作者:");
scanf("%s", author);
printf("请输入价格:");
scanf("%f", &price);
printf("请输入数量:");
scanf("%d", &num);
Book *new_book = (Book *)malloc(sizeof(Book));
strcpy(new_book->name, name);
strcpy(new_book->author, author);
new_book->price = price;
new_book->num = num;
new_book->next = NULL;
head = insert(head, new_book);
printf("添加成功。\n");
break;
}
case 2: {
char name[100];
printf("请输入要删除的书名:");
scanf("%s", name);
Book *del_book = search(head, name);
if (del_book == NULL) {
printf("没有找到该书。\n");
break;
}
head = delete(head, del_book);
printf("删除成功。\n");
break;
}
case 3: {
char name[100];
printf("请输入要查找的书名:");
scanf("%s", name);
Book *p = search(head, name);
if (p == NULL) {
printf("没有找到该书。\n");
} else {
printf("书名:%s 作者:%s 价格:%.2f 数量:%d\n", p->name, p->author, p->price, p->num);
}
break;
}
case 4: {
display(head);
break;
}
case 5: {
save(head, filename);
break;
}
case 6: {
printf("退出程序。\n");
return 0;
}
default: {
printf("无效的选择。\n");
break;
}
}
printf("\n");
}
}
```
该程序实现了以下功能:
1. 添加图书
2. 删除图书
3. 查找图书
4. 显示所有图书
5. 保存所有图书到文件
6. 退出
其中,每本图书都使用一个结构体表示,所有图书按照链表的方式组织起来。在添加、删除、查找、显示和保存图书时,都需要对链表进行相关操作。使用文件来保存所有图书,程序启动时会从文件中读取所有节点并加载到链表中,程序退出时会将所有节点保存到文件中。
阅读全文