基于顺序存储结构的图书信息表的创建和输出
时间: 2023-12-19 07:05:24 浏览: 46
创建:
1. 定义一个结构体,包含图书的信息,如书名、作者、出版社、出版日期等。
```
struct Book {
char name[50];
char author[50];
char press[50];
char date[20];
float price;
};
```
2. 定义一个数组来存储图书信息。
```
#define MAX_SIZE 100
Book bookList[MAX_SIZE];
int length = 0;
```
3. 编写函数来添加图书信息。
```
void addBook() {
if (length >= MAX_SIZE) {
printf("The book list is full.\n");
return;
}
Book book;
printf("Please enter the book name: ");
scanf("%s", book.name);
printf("Please enter the author: ");
scanf("%s", book.author);
printf("Please enter the press: ");
scanf("%s", book.press);
printf("Please enter the date (yyyy-mm-dd): ");
scanf("%s", book.date);
printf("Please enter the price: ");
scanf("%f", &book.price);
bookList[length++] = book;
printf("The book has been added.\n");
}
```
4. 编写函数来输出图书信息。
```
void displayBookList() {
if (length == 0) {
printf("The book list is empty.\n");
return;
}
printf("Book List:\n");
printf("--------------------------------------------------------------\n");
printf("| %-50s | %-20s | %-10s | %-10s |\n", "Name", "Author", "Press", "Date");
printf("--------------------------------------------------------------\n");
for (int i = 0; i < length; i++) {
printf("| %-50s | %-20s | %-10s | %-10s | $%-6.2f |\n", bookList[i].name, bookList[i].author, bookList[i].press, bookList[i].date, bookList[i].price);
}
printf("--------------------------------------------------------------\n");
}
```
输出:
在主函数中调用上述函数即可实现图书信息的创建和输出。
```
int main() {
int choice;
while (1) {
printf("Please select an option:\n");
printf("1. Add book\n");
printf("2. Display book list\n");
printf("3. Exit\n");
printf("Your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addBook();
break;
case 2:
displayBookList();
break;
case 3:
return 0;
default:
printf("Invalid choice. Please select again.\n");
}
}
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![whl](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)