C语言,使用顺序查找表实现图书信息管理系统,相关数据在内存中创建和存储。要求实现插入或是创建、查找、排序功能,查找要求应用到顺序查找和折半查找的方法,例如,可以按书名实施顺序查找、按ISBN号折半查找。查找信息从键盘中获取
时间: 2025-01-24 20:09:30 浏览: 37
好的,下面是一个使用C语言实现的图书信息管理系统示例。该系统使用顺序查找表在内存中创建和存储图书信息,并实现了插入、查找和排序功能。查找功能包括顺序查找和折半查找。
```c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_BOOKS 100
#define TITLE_LENGTH 100
#define ISBN_LENGTH 20
typedef struct {
char title[TITLE_LENGTH];
char isbn[ISBN_LENGTH];
float price;
} Book;
Book library[MAX_BOOKS];
int book_count = 0;
// 插入图书信息
void insertBook() {
if (book_count >= MAX_BOOKS) {
printf("Library is full!\n");
return;
}
printf("Enter book title: ");
scanf("%s", library[book_count].title);
printf("Enter book ISBN: ");
scanf("%s", library[book_count].isbn);
printf("Enter book price: ");
scanf("%f", &library[book_count].price);
book_count++;
printf("Book inserted successfully!\n");
}
// 顺序查找
int sequentialSearch(char *title) {
for (int i = 0; i < book_count; i++) {
if (strcmp(library[i].title, title) == 0) {
return i;
}
}
return -1;
}
// 折半查找
int binarySearch(char *isbn) {
int left = 0, right = book_count - 1;
while (left <= right) {
int mid = (left + right) / 2;
int cmp = strcmp(library[mid].isbn, isbn);
if (cmp == 0) {
return mid;
} else if (cmp < 0) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
// 排序图书信息
void sortBooks() {
for (int i = 0; i < book_count - 1; i++) {
for (int j = 0; j < book_count - 1 - i; j++) {
if (strcmp(library[j].title, library[j + 1].title) > 0) {
Book temp = library[j];
library[j] = library[j + 1];
library[j + 1] = temp;
}
}
}
printf("Books sorted successfully!\n");
}
// 显示所有图书信息
void displayBooks() {
printf("Library contents:\n");
for (int i = 0; i < book_count; i++) {
printf("Book %d: Title: %s, ISBN: %s, Price: %.2f\n", i + 1, library[i].title, library[i].isbn, library[i].price);
}
}
int main() {
int choice;
char title[TITLE_LENGTH];
char isbn[ISBN_LENGTH];
while (1) {
printf("\n1. Insert book\n2. Search book (by title)\n3. Search book (by ISBN)\n4. Sort books\n5. Display books\n6. Exit\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
insertBook();
break;
case 2:
printf("Enter book title to search: ");
scanf("%s", title);
int index = sequentialSearch(title);
if (index != -1) {
printf("Book found: Title: %s, ISBN: %s, Price: %.2f\n", library[index].title, library[index].isbn, library[index].price);
} else {
printf("Book not found!\n");
}
break;
case 3:
printf("Enter book ISBN to search: ");
scanf("%s", isbn);
index = binarySearch(isbn);
if (index != -1) {
printf("Book found: Title: %s, ISBN: %s, Price: %.2f\n", library[index].title, library[index].isbn, library[index].price);
} else {
printf("Book not found!\n");
}
break;
case 4:
sortBooks();
break;
case 5:
displayBooks();
break;
case 6:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
```
阅读全文
相关推荐



















