c语言实现书店存货清单代码
时间: 2024-06-18 15:02:15 浏览: 9
书店存货清单可以使用C语言来实现,主要思路是利用结构体来定义书籍的属性,并将所有书籍存储在一个数组中。以下是一个简单的示例代码:
```
#include <stdio.h>
#include <string.h>
#define MAX_BOOKS 100
struct Book {
char title[50];
char author[50];
int year;
float price;
};
int main() {
struct Book books[MAX_BOOKS];
int num_books = 0;
// 添加书籍
strcpy(books[num_books].title, "C Programming Language");
strcpy(books[num_books].author, "Brian W. Kernighan, Dennis M. Ritchie");
books[num_books].year = 1978;
books[num_books].price = 22.99;
num_books++;
strcpy(books[num_books].title, "The C++ Programming Language");
strcpy(books[num_books].author, "Bjarne Stroustrup");
books[num_books].year = 1985;
books[num_books].price = 32.99;
num_books++;
// 打印所有书籍
for (int i = 0; i < num_books; i++) {
printf("Title: %s\n", books[i].title);
printf("Author: %s\n", books[i].author);
printf("Year: %d\n", books[i].year);
printf("Price: %.2f\n", books[i].price);
printf("\n");
}
return 0;
}
```
以上代码定义了一个`Book`结构体,包含了书籍的标题、作者、出版年份和价格。然后定义了一个最大容量为`MAX_BOOKS`的数组来存储所有的书籍。在`main()`函数中,我们添加了两本书,并通过循环打印出了所有书籍的信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](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)