便利店商品管理系統(tǒng)(C語言版)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
? ? int id;? ? ? ? ? ? ?// 商品編號
? ? char name[20];? ? ? // 商品名稱
? ? float price;? ? ? ? // 商品價格
? ? int quantity;? ? ? ?// 商品數(shù)量
} Product;
// 添加新商品到庫存列表
void addProduct(Product* productList, int* count) {
? ? printf("添加新品:\n");
? ? productList[*count].id = *count + 1;
? ? printf("請輸入商品名字:");
? ? scanf("%s", productList[*count].name);
? ??
? ? printf("請輸入價格:");
? ? scanf("%f", &productList[*count].price);
? ? printf("請輸入數(shù)量:");
? ? scanf("%d", &productList[*count].quantity);
? ? (*count)++;
? ? printf("添加成功!\n");
}
// 顯示庫存列表
void displayProducts(Product* productList, int count) {
? ? printf("庫存列表:\n\n");
? ? printf("編號\t名稱\t價格\t數(shù)量\n");
? ? for (int i = 0; i < count; i++) {
? ? ? ? printf("%d\t%s\t%.1f\t%d\n", productList[i].id, productList[i].name,?
? ? ? ? ? ? productList[i].price, productList[i].quantity);
? ? }
}
// 刪除指定編號的商品
void removeProduct(Product* productList, int* count) {
? ? int productId;
? ? displayProducts(productList, *count);
? ? printf("輸入編號刪除:");
? ? scanf("%d", &productId);
? ? if (productId <= 0 || productId > *count) {
? ? ? ? printf("無效編號!\n");
? ? ? ? return;
? ? }
? ? for (int i = productId-1; i < *count-1; i++) {
? ? ? ? productList[i] = productList[i+1];
? ? }
? ? (*count)--;
? ? printf("刪除成功!\n");
}
// 修改指定編號的商品
void updateProduct(Product* productList, int count) {
? ? int productId;
? ? displayProducts(productList, count);
? ? printf("輸入編號修改:");
? ? scanf("%d", &productId);
? ? if (productId <= 0 || productId > count) {
? ? ? ? printf("無效編號!\n");
? ? ? ? return;
? ? }
? ? printf("請輸入名稱:");
? ? scanf("%s", productList[productId-1].name);
? ? printf("請輸入價格:");
? ? scanf("%f", &productList[productId-1].price);
? ? printf("請輸入數(shù)量:");
? ? scanf("%d", &productList[productId-1].quantity);
? ? printf("修改成功!\n");
}
// 超市管理系統(tǒng)主函數(shù)
int main() {
? ? Product productList[100]; // 最多存儲100個商品
? ? int productCount = 0;? ? ?// 實際商品數(shù)量
? ? int choice;
? ? while (1) {
? ? ? ? printf("------------------\n");
? ? ? ? printf("1. 添加新商品\n");
? ? ? ? printf("2. 查看庫存列表\n");
? ? ? ? printf("3. 刪除指定商品\n");
? ? ? ? printf("4. 修改指定商品\n");
? ? ? ? printf("5. 退出程序\n");
? ? ? ? printf("------------------\n");
? ? ? ? printf("請選擇您的操作:");
? ? ? ? scanf("%d", &choice);
? ? ? ? switch (choice) {
? ? ? ? ? ? case 1:
? ? ? ? ? ? ? ? addProduct(productList, &productCount);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 2:
? ? ? ? ? ? ? ? displayProducts(productList, productCount);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 3:
? ? ? ? ? ? ? ? removeProduct(productList, &productCount);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 4:
? ? ? ? ? ? ? ? updateProduct(productList, productCount);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case 5:
? ? ? ? ? ? ? ? printf("感謝使用!\n");
? ? ? ? ? ? ? ? exit(0);
? ? ? ? ? ? default:
? ? ? ? ? ? ? ? printf("無效選擇!\n");
? ? ? ? }
? ? }
? ? return 0;
}