有一个售货柜员机,内有多种商品供人购买(商品种类不超过100个),每种商品有 名称、数量、价等信息。编程完成n种商品信息的输入输出。 注意:输入时,第一行输入商品种类个数n,第二行依次输入商品的名称,数量,单价。 输出时,输出商品名称、数量、单价(保留1位小数)和总价(等于单价*数量,保留2位小 数),不同数字之间有一个空格。
时间: 2024-02-23 22:58:44 浏览: 11
好的,我可以为您提供一个简单的C语言程序来实现这个功能。首先,我们需要定义一个结构体来表示每种商品的信息,如下所示:
```c
typedef struct {
char name[50]; // 商品名称
int quantity; // 商品数量
float price; // 商品价格
} Product;
```
然后,我们可以在程序中定义一个数组来存储所有商品的信息,如下所示:
```c
Product products[100];
```
接下来,我们可以编写一个函数来输入每种商品的信息,并将其存储到数组中,如下所示:
```c
void inputProducts(int n) {
for (int i = 0; i < n; i++) {
printf("请输入第%d种商品的名称、数量和价格:\n", i + 1);
scanf("%s %d %f", products[i].name, &products[i].quantity, &products[i].price);
}
}
```
然后,我们可以编写一个函数来输出所有商品的信息,如下所示:
```c
void outputProducts(int n) {
for (int i = 0; i < n; i++) {
printf("%s %d %.1f %.2f\n", products[i].name, products[i].quantity, products[i].price, products[i].quantity * products[i].price);
}
}
```
在主函数中,我们需要先读入商品的个数n,然后调用输入函数和输出函数,如下所示:
```c
int main() {
int n;
printf("请输入商品的种类数:\n");
scanf("%d", &n);
inputProducts(n);
outputProducts(n);
return 0;
}
```
完整的程序如下所示:
```c
#include <stdio.h>
typedef struct {
char name[50];
int quantity;
float price;
} Product;
Product products[100];
void inputProducts(int n) {
for (int i = 0; i < n; i++) {
printf("请输入第%d种商品的名称、数量和价格:\n", i + 1);
scanf("%s %d %f", products[i].name, &products[i].quantity, &products[i].price);
}
}
void outputProducts(int n) {
for (int i = 0; i < n; i++) {
printf("%s %d %.1f %.2f\n", products[i].name, products[i].quantity, products[i].price, products[i].quantity * products[i].price);
}
}
int main() {
int n;
printf("请输入商品的种类数:\n");
scanf("%d", &n);
inputProducts(n);
outputProducts(n);
return 0;
}
```
注意,输出时要按照题目要求保留相应的小数位数。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)