一个售货柜员机,内有多种商品供人购买(商品种类不超过100个),每种商品有 名称、数量、价等信息。编程完成n种商品信息的输入输出。 注意:输入时,第一行输入商品种类个数n,第二行依次输入商品的名称,数量,单价。 输出时,输出商品名称、数量、单价(保留1位小数)和总价(等于单价*数量,保留2位小 数),不同数字之间有一个空格,用C语言完成
时间: 2023-09-07 15:12:46 浏览: 47
以下是C语言代码实现:
```c
#include <stdio.h>
#include <string.h>
#define MAX_PRODUCTS 100
struct Product {
char name[20];
int quantity;
float price;
};
int main() {
struct Product products[MAX_PRODUCTS];
int n, i;
float total_price;
printf("请输入商品种类个数:");
scanf("%d", &n);
printf("请输入商品名称、数量、单价:\n");
for (i = 0; i < n; i++) {
scanf("%s %d %f", products[i].name, &products[i].quantity, &products[i].price);
}
printf("商品名称\t数量\t单价\t总价\n");
for (i = 0; i < n; i++) {
total_price = products[i].quantity * products[i].price;
printf("%s\t\t%d\t%.1f\t%.2f\n", products[i].name, products[i].quantity, products[i].price, total_price);
}
return 0;
}
```
代码中使用了一个 `Product` 结构体来存储每个商品的信息,包括名称、数量和单价。在主函数中,先读入商品种类个数,再依次读入每个商品的信息。最后输出每个商品的名称、数量、单价和总价。注意输出时保留小数点后一位或两位。
相关推荐
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)