帮我用C语言写一个商品销售系统
时间: 2023-05-19 10:00:51 浏览: 147
好的,我可以为您提供一些关于商品销售系统的C语言代码。以下是一个简单的示例:
#include <stdio.h>
#include <stdlib.h>
struct product {
char name[50];
int quantity;
float price;
};
int main() {
int choice, i, n;
float total = 0;
struct product p[50];
printf("Enter the number of products: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("Enter the name of product %d: ", i+1);
scanf("%s", p[i].name);
printf("Enter the quantity of product %d: ", i+1);
scanf("%d", &p[i].quantity);
printf("Enter the price of product %d: ", i+1);
scanf("%f", &p[i].price);
}
printf("\n\n");
printf("1. Display all products\n");
printf("2. Calculate total price\n");
printf("3. Exit\n");
do {
printf("\nEnter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("\n\n");
printf("Name\tQuantity\tPrice\n");
for (i = 0; i < n; i++) {
printf("%s\t%d\t\t%.2f\n", p[i].name, p[i].quantity, p[i].price);
}
break;
case 2:
total = 0;
for (i = 0; i < n; i++) {
total += p[i].quantity * p[i].price;
}
printf("\n\n");
printf("Total price: %.2f\n", total);
break;
case 3:
printf("\n\n");
printf("Exiting program...\n");
exit(0);
break;
default:
printf("\n\n");
printf("Invalid choice. Please try again.\n");
break;
}
} while (choice != 3);
return 0;
}
这个程序可以让您输入商品的名称、数量和价格,并提供以下选项:
1. 显示所有产品
2. 计算总价
3. 退出
您可以根据需要进行修改和扩展。希望这可以帮助您开始编写自己的商品销售系统!
阅读全文