数据结构图书管理系统c语言代码如何实现
时间: 2024-04-29 09:04:03 浏览: 55
图书管理系统-C语言实现代码
下面是一个简单的数据结构图书管理系统的C语言代码实现,包括书籍的增加、删除、查询和修改等功能。具体实现方法如下:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定义图书结构体
typedef struct book {
int id; // 编号
char name[20]; // 书名
char author[20]; // 作者
char press[20]; // 出版社
float price; // 价格
struct book *next;
} Book;
// 定义全局变量
Book *head = NULL; // 头结点指针
// 函数声明
void add_book();
void delete_book();
void search_book();
void modify_book();
void display_book();
void save_book();
void load_book();
// 主函数
int main() {
int choice;
// 加载图书数据
load_book();
while (1) {
printf("\n");
printf("1. Add book\n");
printf("2. Delete book\n");
printf("3. Search book\n");
printf("4. Modify book\n");
printf("5. Display books\n");
printf("6. Save books\n");
printf("7. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book();
break;
case 2:
delete_book();
break;
case 3:
search_book();
break;
case 4:
modify_book();
break;
case 5:
display_book();
break;
case 6:
save_book();
break;
case 7:
// 退出程序前保存数据
save_book();
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
// 添加图书
void add_book() {
Book *p, *tail;
// 创建新结点
p = (Book *) malloc(sizeof(Book));
if (p == NULL) {
printf("Memory allocation failed!\n");
return;
}
// 输入图书信息
printf("Enter book id: ");
scanf("%d", &p->id);
printf("Enter book name: ");
scanf("%s", p->name);
printf("Enter book author: ");
scanf("%s", p->author);
printf("Enter book press: ");
scanf("%s", p->press);
printf("Enter book price: ");
scanf("%f", &p->price);
// 将新结点插入链表尾部
p->next = NULL;
if (head == NULL) {
head = p;
} else {
tail = head;
while (tail->next != NULL) {
tail = tail->next;
}
tail->next = p;
}
printf("Book added successfully!\n");
}
// 删除图书
void delete_book() {
int id;
Book *p, *prev;
// 输入图书编号
printf("Enter book id: ");
scanf("%d", &id);
// 查找对应结点
p = head;
prev = NULL;
while (p != NULL) {
if (p->id == id) {
break;
}
prev = p;
p = p->next;
}
if (p == NULL) {
printf("Book not found!\n");
return;
}
// 删除结点
if (prev == NULL) {
head = p->next;
} else {
prev->next = p->next;
}
free(p);
printf("Book deleted successfully!\n");
}
// 查找图书
void search_book() {
int id;
Book *p;
// 输入图书编号
printf("Enter book id: ");
scanf("%d", &id);
// 查找对应结点
p = head;
while (p != NULL) {
if (p->id == id) {
printf("Book id: %d\n", p->id);
printf("Book name: %s\n", p->name);
printf("Book author: %s\n", p->author);
printf("Book press: %s\n", p->press);
printf("Book price: %.2f\n", p->price);
break;
}
p = p->next;
}
if (p == NULL) {
printf("Book not found!\n");
}
}
// 修改图书
void modify_book() {
int id;
Book *p;
// 输入图书编号
printf("Enter book id: ");
scanf("%d", &id);
// 查找对应结点
p = head;
while (p != NULL) {
if (p->id == id) {
break;
}
p = p->next;
}
if (p == NULL) {
printf("Book not found!\n");
return;
}
// 修改结点信息
printf("Enter new book name: ");
scanf("%s", p->name);
printf("Enter new book author: ");
scanf("%s", p->author);
printf("Enter new book press: ");
scanf("%s", p->press);
printf("Enter new book price: ");
scanf("%f", &p->price);
printf("Book modified successfully!\n");
}
// 显示图书
void display_book() {
Book *p;
printf("--------------------\n");
printf("ID | Name | Author | Press | Price\n");
printf("--------------------\n");
// 遍历链表输出信息
p = head;
while (p != NULL) {
printf("%d | %s | %s | %s | %.2f\n", p->id, p->name, p->author, p->press, p->price);
p = p->next;
}
}
// 保存图书
void save_book() {
FILE *fp;
Book *p;
// 打开文件
fp = fopen("book.txt", "w");
if (fp == NULL) {
printf("File open failed!\n");
return;
}
// 遍历链表写入文件
p = head;
while (p != NULL) {
fprintf(fp, "%d %s %s %s %.2f\n", p->id, p->name, p->author, p->press, p->price);
p = p->next;
}
// 关闭文件
fclose(fp);
printf("Books saved successfully!\n");
}
// 加载图书
void load_book() {
FILE *fp;
Book *p, *tail;
int id;
char name[20], author[20], press[20];
float price;
// 打开文件
fp = fopen("book.txt", "r");
if (fp == NULL) {
printf("File open failed!\n");
return;
}
// 读取文件内容并插入链表
while (fscanf(fp, "%d %s %s %s %f", &id, name, author, press, &price) != EOF) {
p = (Book *) malloc(sizeof(Book));
if (p == NULL) {
printf("Memory allocation failed!\n");
return;
}
p->id = id;
strcpy(p->name, name);
strcpy(p->author, author);
strcpy(p->press, press);
p->price = price;
p->next = NULL;
if (head == NULL) {
head = p;
} else {
tail = head;
while (tail->next != NULL) {
tail = tail->next;
}
tail->next = p;
}
}
// 关闭文件
fclose(fp);
printf("Books loaded successfully!\n");
}
```
此代码实现了一个简单的图书管理系统,可以通过菜单选择进行图书的增加、删除、查询、修改、显示、保存和加载等操作。其中,图书的信息以链表形式存储,保存在文件book.txt中,程序启动时会将文件中的数据加载到链表中,程序结束时会将链表中的数据保存到文件中,以便下次启动时使用。
阅读全文