struct写一个储存商品信息模块
时间: 2023-05-14 20:06:12 浏览: 84
好的,以下是一个简单的储存商品信息的 struct:
struct Product {
int id;
char name[50];
float price;
int quantity;
};
你可以使用这个 struct 来储存商品的 ID、名称、价格和数量等信息。
相关问题
用struct写一个商品信息储存模块
好的,以下是一个使用 struct 储存商品信息的示例代码:
```c
#include <stdio.h>
#include <string.h>
#define MAX_NAME_LEN 50
#define MAX_DESC_LEN 100
struct product {
int id;
char name[MAX_NAME_LEN];
char description[MAX_DESC_LEN];
float price;
};
int main() {
struct product p;
// 读入商品信息
printf("请输入商品编号:");
scanf("%d", &p.id);
printf("请输入商品名称:");
scanf("%s", p.name);
printf("请输入商品描述:");
scanf("%s", p.description);
printf("请输入商品价格:");
scanf("%f", &p.price);
// 输出商品信息
printf("商品编号:%d\n", p.id);
printf("商品名称:%s\n", p.name);
printf("商品描述:%s\n", p.description);
printf("商品价格:%f\n", p.price);
return 0;
}
```
这个程序定义了一个 `product` 结构体,包含了商品的编号、名称、描述和价格。在 `main` 函数中,我们先定义了一个 `product` 类型的变量 `p`,然后通过 `scanf` 函数读入用户输入的商品信息。最后,我们再通过 `printf` 函数输出商品信息。
希望这个示例代码能够帮到你!
用c语言写一个商品信息储存模块
好的,以下是一个简单的商品信息储存模块的 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 函数中,我们添加了三个商品并打印它们的信息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)