用c语言写一个商品信息储存模块
时间: 2023-05-15 16:05:18 浏览: 57
好的,以下是一个简单的商品信息储存模块的 C 语言代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PRODUCTS 100
typedef struct {
char name[50];
float price;
int quantity;
} Product;
Product products[MAX_PRODUCTS];
int num_products = 0;
void add_product(char *name, float price, int quantity) {
if (num_products >= MAX_PRODUCTS) {
printf("Error: too many products\n");
return;
}
Product p;
strcpy(p.name, name);
p.price = price;
p.quantity = quantity;
products[num_products++] = p;
}
void print_products() {
printf("Name\tPrice\tQuantity\n");
for (int i = 0; i < num_products; i++) {
printf("%s\t%.2f\t%d\n", products[i].name, products[i].price, products[i].quantity);
}
}
int main() {
add_product("Apple", 1.50, 10);
add_product("Banana", 0.50, 20);
add_product("Orange", 1.00, 15);
print_products();
return 0;
}
这个模块定义了一个 Product 结构体来储存商品信息,包括名称、价格和数量。它还定义了一个 add_product 函数来添加新的商品,以及一个 print_products 函数来打印所有商品的信息。在 main 函数中,我们添加了三个商品并打印它们的信息。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![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)