c语言代码实现有一个售货柜员机,内有多种商品供人购买(商品种类不超过100个),每种商品有 名称、数量、价等信息。编程完成n种商品信息的输入输出。 注意:输入时,第一行输入商品种类个数n,第二行依次输入商品的名称,数量,单价。 输出时,输出商品名称、数量、单价(保留1位小数)和总价(等于单价*数量,保留2位小 数),不同数字之间有一个空格。
时间: 2024-02-13 13:03:57 浏览: 20
好的,以下是 C 语言的代码实现:
```c
#include <stdio.h>
int main()
{
int n;
scanf("%d", &n); // 商品种类个数
char name[100]; // 商品名称
int num; // 商品数量
float price; // 商品单价
float total; // 商品总价
// 循环读入商品信息并输出
for (int i = 0; i < n; i++) {
scanf("%s %d %f", name, &num, &price);
total = num * price;
printf("%s %d %.1f %.2f\n", name, num, price, total);
}
return 0;
}
```
这段代码首先读入商品种类个数 `n`,然后循环读入每个商品的名称、数量和单价,并同时计算出总价。最后使用 `printf` 函数输出每个商品的名称、数量、单价和总价,其中单价和总价均使用格式化字符串控制输出的小数位数。注意,商品名称使用字符数组 `name` 存储。
相关问题
用C语言实现有一个售货柜员机,内有多种商品供人购买(商品种类不超过100个),每种商品有名称、数量、价等信息。编程完成n种商品信息的输入输出。
以下是一个简单的实现,包括商品信息的输入、输出和查询功能。请注意,此代码仅供参考,可能需要根据实际情况进行修改。
```c
#include <stdio.h>
#include <string.h>
#define MAX_ITEMS 100
struct item {
char name[20];
int quantity;
float price;
};
struct item items[MAX_ITEMS];
int num_items = 0;
void add_item() {
if (num_items == MAX_ITEMS) {
printf("Error: too many items\n");
return;
}
struct item new_item;
printf("Enter name: ");
scanf("%s", new_item.name);
printf("Enter quantity: ");
scanf("%d", &new_item.quantity);
printf("Enter price: ");
scanf("%f", &new_item.price);
items[num_items] = new_item;
num_items++;
}
void print_items() {
printf("%-20s %-10s %-10s\n", "Name", "Quantity", "Price");
for (int i = 0; i < num_items; i++) {
printf("%-20s %-10d %-10.2f\n", items[i].name, items[i].quantity, items[i].price);
}
}
void find_item() {
char name[20];
printf("Enter name: ");
scanf("%s", name);
for (int i = 0; i < num_items; i++) {
if (strcmp(items[i].name, name) == 0) {
printf("%-20s %-10d %-10.2f\n", items[i].name, items[i].quantity, items[i].price);
return;
}
}
printf("Item not found\n");
}
int main() {
int choice;
do {
printf("\n1. Add item\n");
printf("2. Print items\n");
printf("3. Find item\n");
printf("4. Quit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_item();
break;
case 2:
print_items();
break;
case 3:
find_item();
break;
case 4:
printf("Goodbye\n");
break;
default:
printf("Invalid choice\n");
}
} while (choice != 4);
return 0;
}
```
在此实现中,我们使用了一个结构体 `item` 来代表每个商品,它包含了名称、数量和价格等信息。我们使用一个全局数组 `items` 来存储所有商品,以及一个变量 `num_items` 来记录商品数量。函数 `add_item` 可以让用户输入一个新商品的信息,并将其添加到 `items` 数组中。函数 `print_items` 可以输出所有商品的信息。函数 `find_item` 可以让用户输入一个商品名称,并在 `items` 数组中查找该商品,如果找到则输出其信息,否则输出错误信息。最后,我们使用一个循环来实现菜单选择功能,让用户可以不断地进行添加、查询和输出操作,直到选择退出程序。
C语言有一个售货柜员机,内有多种商品供人购买(商品种类不超过100个),每种商品有名称、数量、价等信息。编程完成n种商品信息的输入输出。
下面是一个简单的C语言程序,可以实现n种商品信息的输入输出:
```c
#include <stdio.h>
#define MAX_PRODUCTS 100
struct product {
char name[50];
int quantity;
double price;
};
int main() {
int n;
struct product products[MAX_PRODUCTS];
printf("请输入商品种类数(不超过100):");
scanf("%d", &n);
printf("请输入每种商品的信息:\n");
for (int i = 0; i < n; i++) {
printf("请输入第%d种商品的名称:", i + 1);
scanf("%s", products[i].name);
printf("请输入第%d种商品的数量:", i + 1);
scanf("%d", &products[i].quantity);
printf("请输入第%d种商品的价格:", i + 1);
scanf("%lf", &products[i].price);
}
printf("输出每种商品的信息:\n");
for (int i = 0; i < n; i++) {
printf("第%d种商品的名称:%s\n", i + 1, products[i].name);
printf("第%d种商品的数量:%d\n", i + 1, products[i].quantity);
printf("第%d种商品的价格:%g\n\n", i + 1, products[i].price);
}
return 0;
}
```
程序中使用了一个结构体 `product` 来保存每种商品的信息,其中包括商品名称、数量和价格。程序先从用户处获取商品种类数 `n`,然后循环获取每种商品的信息并保存到 `products` 数组中,最后再循环输出每种商品的信息。
相关推荐
![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)