图书馆管理系统c数组
时间: 2024-02-27 13:49:13 浏览: 54
以下是两个关于图书馆管理系统的C语言数组的例子:
1. 使用结构体数组实现的图书馆管理系统:
```c
#include <stdio.h>
#include <string.h>
#define MAX_BOOKS 100
struct Book {
char title[100];
char author[100];
int year;
};
int main() {
struct Book library[MAX_BOOKS];
int numBooks = 0;
// 添加书籍
strcpy(library[numBooks].title, "Book 1");
strcpy(library[numBooks].author, "Author 1");
library[numBooks].year = 2020;
numBooks++;
// 输出所有书籍
for (int i = 0; i < numBooks; i++) {
printf("Title: %s\n", library[i].title);
printf("Author: %s\n", library[i].author);
printf("Year: %d\n", library[i].year);
printf("\n");
}
return 0;
}
```
2. 使用结构体数组实现的图书馆管理系统(包含输入、排序和浏览功能):
```c
#include <stdio.h>
#include <string.h>
#define MAX_BOOKS 100
struct Book {
char title[100];
char author[100];
int year;
};
void sortBooks(struct Book library[], int numBooks) {
// 使用冒泡排序按照书名对书籍进行排序
for (int i = 0; i < numBooks - 1; i++) {
for (int j = 0; j < numBooks - i - 1; j++) {
if (strcmp(library[j].title, library[j + 1].title) > 0) {
struct Book temp = library[j];
library[j] = library[j + 1];
library[j + 1] = temp;
}
}
}
}
int main() {
struct Book library[MAX_BOOKS];
int numBooks = 0;
// 输入书籍信息
printf("Enter book title: ");
scanf("%s", library[numBooks].title);
printf("Enter book author: ");
scanf("%s", library[numBooks].author);
printf("Enter book year: ");
scanf("%d", &library[numBooks].year);
numBooks++;
// 排序书籍
sortBooks(library, numBooks);
// 浏览所有书籍
for (int i = 0; i < numBooks; i++) {
printf("Title: %s\n", library[i].title);
printf("Author: %s\n", library[i].author);
printf("Year: %d\n", library[i].year);
printf("\n");
}
return 0;
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.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)