#include <stdio.h> #include <string.h> #include <stdlib.h> #include <ctype.h>
#define MAX_NAME_LEN 50 #define MAX_TYPE_LEN 20 #define FILENAME "products.dat" #define MAX_PRODUCTS 1000
typedef struct { int id; char name[MAX_NAME_LEN]; double price; int num; char type[MAX_TYPE_LEN]; } Product;
Product products[MAX_PRODUCTS]; int product_count = 0;
void load_data(); void save_data(); void display_menu(); void add_product(); void delete_product(); void modify_product(); void search_product(); void statistics(); void display_product(Product p); int find_product_by_id(int id); int find_product_by_name(char *name); void sort_by_price();
int main() { load_data();
int choice;
do{
display_menu();
printf("请输入选项(0-6): ");
scanf("%d",&choice);
getchar();
switch(choice){
case 1:
add_product();
break;
case 2:
search_product();
break;
case 3:
modify_product();
break;
case 4:
delete_product();
break;
case 5:
statistics();
break;
case 6:
printf("系统已退出 \n");
break;
default:
printf("请重新输入 \n");
}
}while(choice != 6);
save_data();
return 0;
}
void load_data() { FILE *fp = fopen(FILENAME,"rb"); if(fp == NULL) { printf("生成错误,将重新生成 \n"); return; } product_count = fread(products, sizeof(Product), MAX_PRODUCTS, fp); fclose(fp); printf("成功加载 %d 条商品记录\n", product_count); }
void save_data() { FILE *fp = fopen(FILENAME,"wb"); if(fp == NULL) { printf("无法保存数据文件\n"); return; } fwrite(products,product_count,MAX_PRODUCTS,fp); fclose(fp); printf("成功存储 %d 条商品记录\n", product_count); }
void display_menu() { printf("\n======商品管理系统======\n"); printf("1.添加商品\n"); printf("2.查询商品\n"); printf("3.修改商品\n"); printf("4.删除商品\n"); printf("5.统计商品\n"); printf("6.退出系统\n"); printf("=========================\n"); return; }
void display_product(Product p) { printf("请输入商品编号:%d\n",p.id); printf("请输入商品名称:%s\n",p.name); printf("请输入商品单价:%.2f\n",p.price); printf("请输入商品库存:%d\n",p.num); printf("请输入商品类型:%s\n",p.type); printf("库存价值:%.2f\n", p.num * p.price); }
void add_product() { if(product_count >= MAX_PRODUCTS){ printf("商品已达到上限\n"); return; } printf("\n====添加商品====\n");
printf("请输入商品编号: ");
Product new_product;
scanf("%d",&new_product.id);
while(getchar() != '\n');
if(find_product_by_id(new_product.id) != -1){
printf("该商品已经存在\n");
return;
}
printf("请输入商品名称: ");
fgets(new_product.name,MAX_NAME_LEN,stdin);
new_product.name[strcspn(new_product.name, "\n")] = '\0';
printf("请输入商品价格: ");
scanf("%lf",&new_product.price);
printf("请输入商品库存: ");
scanf("%d",&new_product.num);
getchar();
printf("请输入商品类型: ");
fgets(new_product.type,MAX_TYPE_LEN,stdin);
new_product.type[strcspn(new_product.type,"\n")] = '\0';
products[product_count++] = new_product;
printf("商品添加成功\n");
}
void search_product() { int id, choice; char name[MAX_NAME_LEN]; printf("\n====商品名称===========\n"); printf("1.按编号查询\n"); printf("2.按名称查询\n"); printf("选择查询方式\n"); scanf("%d",&choice); getchar(); int index = -1; switch(choice) { case 1: printf("请输入商品编号:\n"); scanf("%d",&id); index = find_product_by_id(id); break; case 2: printf("请输入商品名称\n"); fgets(name,MAX_NAME_LEN,stdin); name[strcspn(name, "\n")] = '\0'; index = find_product_by_name(name); break; default: printf("无效输入\n"); return; } if(index != -1) { printf("找到商品\n"); display_product(products[index]); } else{ printf("查找失败\n"); } }
void modify_product() { int id, operation, quantity; printf("\n===修改商品库存===\n"); printf("商品编号\n"); scanf("%d",&id); getchar();
int index = find_product_by_id(id);
if(index == -1)
{
printf("未查找到该商品\n");
return;
}
printf("当前库存:%d\n",products[index].num);
printf("请选择查询方式:\n");
printf("1.进货\n");
printf("2.出货\n");
printf("请选择操作\n");
scanf("%d",&operation);
printf("请选择数量\n");
scanf("%d",&quantity);
if(operation == 1){
products[index].num += quantity;
printf("进货成功,当前库存 %d\n ",products[index].num);
}
else if(operation == 2){
if(products[index].num < quantity){
printf("当前库存不足\n");
}
else {
products[index].num -= quantity;
printf("出货成功,当前剩余 %d\n",products[index].num);
}
}else{ printf("该操作无效\n"); } save_data(); }
void delete_product() { int id; char name[MAX_NAME_LEN];
printf("\n===删除商品===\n");
printf("输入编号: ");
scanf("%d",&id);
getchar();
int index = find_product_by_id(id);
if(index == -1){
printf("未找到该商品\n");
return;
}
printf("请输入商品名称\n");
fgets(name,MAX_NAME_LEN,stdin);
name[strcspn(name, "\n")] = '\0';
if(strcmp(products[index].name,name) != 0){
printf("商品名称不相同,取消删除商品!\n");
return;
}
printf("将要删除的商品\n");
display_product(products[index]);
printf("是否删除?(y/n): ");
char confirm;
scanf("%c",&confirm);
getchar();
if(confirm == 'Y' || confirm == 'y'){
for(int i = index; i < product_count - 1; i++){
products[i] = products[i + 1];}
product_count--;
printf("该商品已删除\n");
save_data();
}else{
printf("该操作无效,已取消\n");
}
}
void statistics() { int choice; printf("\n===统计商品===\n"); printf("1.按商品类型排序\n"); printf("2.统计总数量\n"); printf("3.按类型排序统计\n"); scanf("%d",&choice); getchar(); switch(choice){ case 1:{ char type[MAX_TYPE_LEN]; printf("输入要统计的商品\n"); fgets(type,MAX_TYPE_LEN,stdin); type[strcspn(type,"\n")] ='\0'; int count = 0; float total_value = 0.0f; printf("类型为 '%s' 的商品\n",type); printf("-%8s -%20s -%10s -%8s -%15s","编号","名称","单价","库存","总价值"); for(int i = 0; i < product_count; i++){ if(strcmp(products[i].type,type) == 0){ float value = products[i].price * products[i].num; printf("-%8d -%20s -%10.2f -%8d -%15.2f\n",products[i].id,products[i].name,products[i].price,products[i].num,value); count++; total_value += value; } } printf("类型为 '%s', 的商品数量: %d\n",type,count); printf("总库存价值:%.2f\n",total_value); break; } case 2: printf("\n所有商品统计:\n"); printf("商品总数: %d\n", product_count);
float total_value = 0;
for (int i = 0; i < product_count; i++) {
total_value += products[i].price * products[i].num;
}
printf("总库存价值: %.2f\n", total_value);
break;
case 3:
sort_by_price();
break;
default:
printf("无效操作\n");
}
}
void sort_by_price() { char type[MAX_TYPE_LEN]; printf("输入要排序的商品类型: \n"); fgets(type,MAX_TYPE_LEN,stdin); type[strcspn(type,"\n")] = '\0';
Product temp[MAX_PRODUCTS];
int temp_count = 0;
for(int i = 0; i < product_count; i++){
if(strcmp(products[i].type,type) == 0){
temp[temp_count++] = products[i];
}
}
if(temp_count == 0){
printf("未找到类型为 '%s' 的商品",type);
return;
}
for(int i = 0; i < temp_count; i++){
for(int j = 0; j < temp_count - i - 1; j++){
if(temp[j].price > temp[j + 1].price){
Product swap = temp[j];
temp[j] = temp[j + 1];
temp[j + 1] = swap;
}
}
}
printf("\n类型 '%s' 的商品按单价排序结果:\n", type);
printf("%-8s %-20s %-10s %-8s %-15s\n",
"编号", "名称", "单价", "库存", "类型");
for (int i = 0; i < temp_count; i++) {
printf("%-8d %-20s %-10.2f %-8d %-15s\n",
temp[i].id, temp[i].name, temp[i].price,
temp[i].num, temp[i].type);
}
}
int find_product_by_id(int id)
{
for(int i = 0; i < product_count; i++){
if(products[i].id == id){
return i;
}
}
return -1;
}
int find_product_by_name(char *name) { for(int i = 0; i < product_count; i++){ if(strcmp(products[i].name,name) == 0){ return i; } } return -1; }