哈喽,我是ohoh老师,在国外网看到了一个这样的代码:(已经把英文注释换成中文)。觉得可以用来写个简易的货物管理系统,适合中学生。
#include <stdio.h>
// 货物信息结构体
typedef struct {
char name[20];
float weight; // 重量(kg)
char category[20]; // 货物类别
int quantity; // 数量
} Item;
// 分拣区域信息
typedef struct {
char areaName[30];
float totalWeight;
int itemCount;
} SortingArea;
// 显示货物清单
void displayItems() {
printf("
=== 货物清单 ===
");
printf("1. 电子产品 - 手机 (0.2kg)
");
printf("2. 食品 - 苹果 (1.0kg)
");
printf("3. 服装 - T恤 (0.3kg)
");
printf("4. 书籍 - 教材 (0.8kg)
");
printf("5. 家电 - 微波炉 (12.0kg)
");
}
// 获取货物信息
Item getItemInfo(int itemCode) {
Item item = {"", 0.0, "", 0};
switch(itemCode) {
case 1:
strcpy(item.name, "手机");
item.weight = 0.2;
strcpy(item.category, "电子产品");
break;
case 2:
strcpy(item.name, "苹果");
item.weight = 1.0;
strcpy(item.category, "食品");
break;
case 3:
strcpy(item.name, "T恤");
item.weight = 0.3;
strcpy(item.category, "服装");
break;
case 4:
strcpy(item.name, "教材");
item.weight = 0.8;
strcpy(item.category, "书籍");
break;
case 5:
strcpy(item.name, "微波炉");
item.weight = 12.0;
strcpy(item.category, "家电");
break;
default:
strcpy(item.name, "未知货物");
item.weight = 0.0;
strcpy(item.category, "其他");
}
return item;
}
// 分拣货物到不同区域
void sortItemToArea(Item item, SortingArea areas[], int areaCount) {
printf("
=== 选择分拣区域 ===
");
printf("1. 电子产品区
");
printf("2. 食品区
");
printf("3. 服装区
");
printf("4. 书籍区
");
printf("5. 家电区
");
printf("6. 其他物品区
");
int areaChoice;
printf("请选择分拣区域 (1-6): ");
scanf("%d", &areaChoice);
if(areaChoice >= 1 && areaChoice <= areaCount) {
areas[areaChoice-1].totalWeight += item.weight * item.quantity;
areas[areaChoice-1].itemCount += item.quantity;
printf("✓ 已分拣 %d 件%s到%s
",
item.quantity, item.name, areas[areaChoice-1].areaName);
} else {
printf("无效的区域选择!
");
}
}
// 显示分拣统计
void displaySortingStats(SortingArea areas[], int areaCount) {
printf("
=== 分拣统计 ===
");
float totalWeight = 0;
int totalItems = 0;
for(int i = 0; i < areaCount; i++) {
printf("%s: %.1fkg, %d件货物
",
areas[i].areaName, areas[i].totalWeight, areas[i].itemCount);
totalWeight += areas[i].totalWeight;
totalItems += areas[i].itemCount;
}
printf("
总计: %.1fkg, %d件货物
", totalWeight, totalItems);
}
// 初始化分拣区域
void initializeAreas(SortingArea areas[]) {
strcpy(areas[0].areaName, "电子产品区");
strcpy(areas[1].areaName, "食品区");
strcpy(areas[2].areaName, "服装区");
strcpy(areas[3].areaName, "书籍区");
strcpy(areas[4].areaName, "家电区");
strcpy(areas[5].areaName, "其他物品区");
for(int i = 0; i < 6; i++) {
areas[i].totalWeight = 0.0;
areas[i].itemCount = 0;
}
}
int main() {
SortingArea areas[6];
initializeAreas(areas);
int choice;
printf("=== 智能货物分拣系统 ===
");
do {
printf("
=== 主菜单 ===
");
printf("1. 显示货物清单
");
printf("2. 开始分拣
");
printf("3. 查看分拣统计
");
printf("4. 退出系统
");
printf("请选择操作: ");
scanf("%d", &choice);
switch(choice) {
case 1:
displayItems();
break;
case 2: {
int itemCode, quantity;
displayItems();
printf("
请输入货物编号: ");
scanf("%d", &itemCode);
if(itemCode < 1 || itemCode > 5) {
printf("无效的货物编号!
");
break;
}
printf("请输入数量: ");
scanf("%d", &quantity);
if(quantity <= 0) {
printf("无效的数量!
");
break;
}
Item currentItem = getItemInfo(itemCode);
currentItem.quantity = quantity;
sortItemToArea(currentItem, areas, 6);
break;
}
case 3:
displaySortingStats(areas, 6);
break;
case 4:
printf("感谢使用货物分拣系统!
");
break;
default:
printf("无效选择,请重新输入!
");
}
} while(choice != 4);
return 0;
}
print("=== 简易货物分拣系统 ===
")
# 货物清单
print("货物清单:")
print("1. 手机 (电子产品)")
print("2. 苹果 (食品)")
print("3. 衣服 (服装)")
print("4. 书本 (书籍)")
print("5. 微波炉 (家电)")
# 分拣区域
areas = {
"电子产品区": 0,
"食品区": 0,
"服装区": 0,
"书籍区": 0,
"家电区": 0
}
# 主程序
while True:
print("
=== 菜单 ===")
print("1. 分拣货物")
print("2. 查看分拣情况")
print("3. 退出")
choice = input("请选择(1-3): ")
if choice == "1":
# 分拣货物
item_num = input("请输入货物编号(1-5): ")
if item_num in ["1", "2", "3", "4", "5"]:
# 根据货物编号确定货物类型
if item_num == "1":
area = "电子产品区"
item_name = "手机"
elif item_num == "2":
area = "食品区"
item_name = "苹果"
elif item_num == "3":
area = "服装区"
item_name = "衣服"
elif item_num == "4":
area = "书籍区"
item_name = "书本"
else: # item_num == "5"
area = "家电区"
item_name = "微波炉"
# 记录分拣
areas[area] += 1
print(f"成功分拣 {item_name} 到 {area}")
else:
print("编号错误,请重新输入!")
elif choice == "2":
# 查看分拣情况
print("
=== 分拣情况 ===")
total = 0
for area, count in areas.items():
print(f"{area}: {count}件")
total += count
print(f"总计: {total}件货物")
elif choice == "3":
# 退出程序
print("谢谢使用货物分拣系统!")
break
else:
print("输入错误,请重新选择!")
© 版权声明
文章版权归作者所有,未经允许请勿转载。
相关文章
暂无评论...




