商品进销存管理程序 C语言代码多一点
时间: 2023-11-16 12:21:50 浏览: 35
以下是一个简单的商品进销存管理程序的C语言代码:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Product {
char name[20];
int quantity;
float price;
};
void addProduct(struct Product products[], int *count) {
printf("Enter product name: ");
scanf("%s", products[*count].name);
printf("Enter product quantity: ");
scanf("%d", &products[*count].quantity);
printf("Enter product price: ");
scanf("%f", &products[*count].price);
(*count)++;
printf("Product added successfully!\n");
}
void displayProducts(struct Product products[], int count) {
if(count == 0) {
printf("No products found!\n");
return;
}
printf("Products:\n");
for(int i=0; i<count; i++) {
printf("%d. Name: %s, Quantity: %d, Price: %.2f\n", i+1, products[i].name, products[i].quantity, products[i].price);
}
}
void sellProduct(struct Product products[], int count) {
if(count == 0) {
printf("No products found!\n");
return;
}
char name[20];
int quantity;
printf("Enter product name: ");
scanf("%s", name);
for(int i=0; i<count; i++) {
if(strcmp(products[i].name, name) == 0) {
printf("Enter product quantity: ");
scanf("%d", &quantity);
if(products[i].quantity >= quantity) {
products[i].quantity -= quantity;
printf("Product sold successfully!\n");
return;
}
else {
printf("Not enough quantity available!\n");
return;
}
}
}
printf("Product not found!\n");
}
int main() {
struct Product products[100];
int count = 0;
int choice;
while(1) {
printf("1. Add product\n");
printf("2. Display products\n");
printf("3. Sell product\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
addProduct(products, &count);
break;
case 2:
displayProducts(products, count);
break;
case 3:
sellProduct(products, count);
break;
case 4:
exit(0);
default:
printf("Invalid choice!\n");
}
}
return 0;
}
这个程序使用了一个结构体来存储产品的名称、数量和价格。它提供了三个操作:添加产品,显示产品和销售产品。程序通过一个无限循环来不断接收用户的操作选择,直到用户选择退出。