可不可以给我一个示范性的代码
时间: 2024-02-24 22:53:18 浏览: 73
一个代码例子
好的,这里为您提供一个示范性的代码,实现了基本的图书管理功能。代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 图书结构体
typedef struct Book {
char title[50]; // 书名
char author[20]; // 作者
char publisher[20]; // 出版社
float price; // 价格
} Book;
// 链表节点结构体
typedef struct Node {
Book *book; // 指向图书结构体的指针
struct Node *next; // 指向下一个节点的指针
} Node;
// 添加图书
void addBook(Node **head) {
Book *book = (Book *)malloc(sizeof(Book));
printf("请输入书名:");
scanf("%s", book->title);
printf("请输入作者:");
scanf("%s", book->author);
printf("请输入出版社:");
scanf("%s", book->publisher);
printf("请输入价格:");
scanf("%f", &book->price);
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->book = book;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
Node *cur = *head;
while (cur->next != NULL) {
cur = cur->next;
}
cur->next = newNode;
}
printf("添加成功!\n");
}
// 删除图书
void deleteBook(Node **head, char *title) {
Node *cur = *head;
Node *prev = NULL;
while (cur != NULL) {
if (strcmp(cur->book->title, title) == 0) {
if (prev == NULL) {
*head = cur->next;
} else {
prev->next = cur->next;
}
free(cur->book);
free(cur);
printf("删除成功!\n");
return;
}
prev = cur;
cur = cur->next;
}
printf("找不到该书!\n");
}
// 修改图书信息
void editBook(Node *head, char *title) {
Node *cur = head;
while (cur != NULL) {
if (strcmp(cur->book->title, title) == 0) {
printf("请输入新的书名(旧书名:%s):", cur->book->title);
scanf("%s", cur->book->title);
printf("请输入新的作者(旧作者:%s):", cur->book->author);
scanf("%s", cur->book->author);
printf("请输入新的出版社(旧出版社:%s):", cur->book->publisher);
scanf("%s", cur->book->publisher);
printf("请输入新的价格(旧价格:%f):", cur->book->price);
scanf("%f", &cur->book->price);
printf("修改成功!\n");
return;
}
cur = cur->next;
}
printf("找不到该书!\n");
}
// 查找图书
void searchBook(Node *head, char *title) {
Node *cur = head;
while (cur != NULL) {
if (strcmp(cur->book->title, title) == 0) {
printf("书名:%s\n", cur->book->title);
printf("作者:%s\n", cur->book->author);
printf("出版社:%s\n", cur->book->publisher);
printf("价格:%f\n", cur->book->price);
return;
}
cur = cur->next;
}
printf("找不到该书!\n");
}
// 显示所有图书
void showAllBooks(Node *head) {
Node *cur = head;
while (cur != NULL) {
printf("书名:%s\n", cur->book->title);
printf("作者:%s\n", cur->book->author);
printf("出版社:%s\n", cur->book->publisher);
printf("价格:%f\n", cur->book->price);
printf("\n");
cur = cur->next;
}
}
// 读取文件
void readFromFile(Node **head) {
FILE *fp = fopen("books.txt", "r");
if (fp == NULL) {
printf("文件打开失败!\n");
return;
}
while (!feof(fp)) {
Book *book = (Book *)malloc(sizeof(Book));
fscanf(fp, "%s %s %s %f", book->title, book->author, book->publisher, &book->price);
Node *newNode = (Node *)malloc(sizeof(Node));
newNode->book = book;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
Node *cur = *head;
while (cur->next != NULL) {
cur = cur->next;
}
cur->next = newNode;
}
}
fclose(fp);
}
// 保存文件
void saveToFile(Node *head) {
FILE *fp = fopen("books.txt", "w");
if (fp == NULL) {
printf("文件打开失败!\n");
return;
}
Node *cur = head;
while (cur != NULL) {
fprintf(fp, "%s %s %s %f\n", cur->book->title, cur->book->author, cur->book->publisher, cur->book->price);
cur = cur->next;
}
fclose(fp);
}
int main() {
Node *head = NULL;
int choice;
char title[50];
readFromFile(&head);
while (1) {
printf("请选择操作:\n");
printf("1. 添加图书\n");
printf("2. 删除图书\n");
printf("3. 修改图书信息\n");
printf("4. 查找图书\n");
printf("5. 显示所有图书\n");
printf("6. 退出\n");
scanf("%d", &choice);
switch (choice) {
case 1:
addBook(&head);
break;
case 2:
printf("请输入要删除的书名:");
scanf("%s", title);
deleteBook(&head, title);
break;
case 3:
printf("请输入要修改的书名:");
scanf("%s", title);
editBook(head, title);
break;
case 4:
printf("请输入要查找的书名:");
scanf("%s", title);
searchBook(head, title);
break;
case 5:
showAllBooks(head);
break;
case 6:
saveToFile(head);
exit(0);
default:
printf("无效的选项!\n");
break;
}
}
return 0;
}
```
注意,在使用该代码之前,需要在程序所在目录下创建一个名为“books.txt”的文件,用于保存图书信息。
阅读全文