我想用c语言写一个完整功能的物流管理系统,要具有增删改查四个函数,你有什么好的建议吗?请给我相应的完整的代码案例
时间: 2024-02-21 10:01:38 浏览: 87
当然,我可以为您提供一些建议和代码案例。首先,您需要使用C语言中的结构体来表示物流信息,例如:
```c
typedef struct {
int id; // 物流编号
char name[50]; // 物流名称
char address[100]; // 物流地址
float weight; // 物流重量
} Logistics;
```
然后,您需要实现四个基本函数:增加、删除、修改和查询。下面是完整的代码案例:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int id; // 物流编号
char name[50]; // 物流名称
char address[100]; // 物流地址
float weight; // 物流重量
} Logistics;
// 添加物流信息
void addLogistics(Logistics *logistics, int *count) {
printf("请输入物流编号:");
scanf("%d", &logistics[*count].id);
printf("请输入物流名称:");
scanf("%s", logistics[*count].name);
printf("请输入物流地址:");
scanf("%s", logistics[*count].address);
printf("请输入物流重量:");
scanf("%f", &logistics[*count].weight);
(*count)++;
printf("添加成功!\n");
}
// 删除物流信息
void deleteLogistics(Logistics *logistics, int *count) {
int id, i, flag = 0;
printf("请输入要删除的物流编号:");
scanf("%d", &id);
for (i = 0; i < *count; i++) {
if (logistics[i].id == id) {
flag = 1;
break;
}
}
if (flag) {
for (i = i; i < *count - 1; i++) {
logistics[i] = logistics[i+1];
}
(*count)--;
printf("删除成功!\n");
} else {
printf("未找到该物流信息!\n");
}
}
// 修改物流信息
void modifyLogistics(Logistics *logistics, int count) {
int id, i, flag = 0;
printf("请输入要修改的物流编号:");
scanf("%d", &id);
for (i = 0; i < count; i++) {
if (logistics[i].id == id) {
flag = 1;
break;
}
}
if (flag) {
printf("请输入物流名称:");
scanf("%s", logistics[i].name);
printf("请输入物流地址:");
scanf("%s", logistics[i].address);
printf("请输入物流重量:");
scanf("%f", &logistics[i].weight);
printf("修改成功!\n");
} else {
printf("未找到该物流信息!\n");
}
}
// 查询物流信息
void queryLogistics(Logistics *logistics, int count) {
int id, i, flag = 0;
printf("请输入要查询的物流编号:");
scanf("%d", &id);
for (i = 0; i < count; i++) {
if (logistics[i].id == id) {
flag = 1;
break;
}
}
if (flag) {
printf("物流编号:%d\n", logistics[i].id);
printf("物流名称:%s\n", logistics[i].name);
printf("物流地址:%s\n", logistics[i].address);
printf("物流重量:%f\n", logistics[i].weight);
} else {
printf("未找到该物流信息!\n");
}
}
int main() {
Logistics logistics[100];
int count = 0, choice;
while (1) {
printf("请选择操作:\n");
printf("1. 添加物流信息\n");
printf("2. 删除物流信息\n");
printf("3. 修改物流信息\n");
printf("4. 查询物流信息\n");
printf("5. 退出程序\n");
printf("请输入操作编号:");
scanf("%d", &choice);
switch (choice) {
case 1:
addLogistics(logistics, &count);
break;
case 2:
deleteLogistics(logistics, &count);
break;
case 3:
modifyLogistics(logistics, count);
break;
case 4:
queryLogistics(logistics, count);
break;
case 5:
exit(0);
default:
printf("输入有误,请重新输入!\n");
}
}
return 0;
}
```
希望这个代码案例可以帮助到您!
阅读全文