帮我C语言写一个商品销售系统
时间: 2023-05-19 14:00:53 浏览: 59
好的,我可以为您提供一个基本的商品销售系统的代码框架,您可以根据您的需求进行修改和完善。以下是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PRODUCTS 100
typedef struct {
char name[50];
int quantity;
float price;
} Product;
Product products[MAX_PRODUCTS];
int num_products = 0;
void add_product(char *name, int quantity, float price) {
if (num_products >= MAX_PRODUCTS) {
printf("Error: too many products\n");
return;
}
Product p;
strcpy(p.name, name);
p.quantity = quantity;
p.price = price;
products[num_products++] = p;
}
void list_products() {
printf("Name\tQuantity\tPrice\n");
for (int i = 0; i < num_products; i++) {
printf("%s\t%d\t\t%.2f\n", products[i].name, products[i].quantity, products[i].price);
}
}
void sell_product(char *name, int quantity) {
for (int i = 0; i < num_products; i++) {
if (strcmp(products[i].name, name) == 0) {
if (products[i].quantity < quantity) {
printf("Error: not enough stock\n");
return;
}
products[i].quantity -= quantity;
printf("Sold %d %s for %.2f\n", quantity, name, quantity * products[i].price);
return;
}
}
printf("Error: product not found\n");
}
int main() {
add_product("Apple", 10, 2.5);
add_product("Banana", 20, 1.5);
add_product("Orange", 15, 3.0);
list_products();
sell_product("Apple", 5);
list_products();
return 0;
}
希望这个代码框架能够帮助您实现您的商品销售系统。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)