用C++的printf scanf 写一个带有删除快递 增加快递 管理员 的快递管理系统代码
时间: 2024-02-21 15:59:12 浏览: 35
好的,这里提供一个使用 `printf` 和 `scanf` 的快递管理系统代码,同样实现了增加快递和删除快递的功能,管理员密码默认为 "123456"。
```cpp
#include <cstdio>
#include <cstring>
const int MAXN = 1005;
// 定义快递信息结构体
struct ExpressInfo {
char id[25]; // 快递单号
char name[25]; // 收件人姓名
char phone[25]; // 收件人电话
};
// 定义快递列表结构体
struct ExpressList {
ExpressInfo info; // 快递信息
int next; // 下一个节点下标
} list[MAXN];
// 定义快递管理系统类
class ExpressManager {
private:
int head; // 快递列表头节点下标
char password[25]; // 管理员密码
int size; // 快递列表长度
public:
// 构造函数
ExpressManager() {
head = -1;
strcpy(password, "123456");
size = 0;
}
// 增加快递
void addExpress() {
if (size >= MAXN) {
printf("快递列表已满,无法添加新的快递!\n");
return;
}
// 输入快递信息
ExpressInfo info;
printf("请输入快递单号:");
scanf("%s", info.id);
printf("请输入收件人姓名:");
scanf("%s", info.name);
printf("请输入收件人电话:");
scanf("%s", info.phone);
// 创建快递节点
list[size].info = info;
list[size].next = -1;
// 将节点加入快递列表
if (head == -1) {
head = size;
} else {
int p = head;
while (list[p].next != -1) {
p = list[p].next;
}
list[p].next = size;
}
size++;
printf("快递添加成功!\n");
}
// 删除快递
void deleteExpress() {
char id[25];
printf("请输入要删除的快递单号:");
scanf("%s", id);
// 查找快递节点
int p = head;
int pre = -1;
while (p != -1) {
if (strcmp(list[p].info.id, id) == 0) {
break;
}
pre = p;
p = list[p].next;
}
if (p == -1) {
printf("没有找到该快递!\n");
} else {
// 判断是否需要管理员权限
char inputPassword[25];
if (pre == -1) { // 删除头节点需要管理员权限
printf("请输入管理员密码:");
scanf("%s", inputPassword);
if (strcmp(inputPassword, password) != 0) {
printf("管理员密码错误,无法删除该快递!\n");
return;
}
head = list[p].next;
} else { // 删除非头节点不需要管理员权限
list[pre].next = list[p].next;
}
printf("快递删除成功!\n");
size--;
}
}
};
int main() {
ExpressManager manager;
while (true) {
printf("请选择操作:1.增加快递 2.删除快递 3.退出\n");
int choice;
scanf("%d", &choice);
switch (choice) {
case 1:
manager.addExpress();
break;
case 2:
manager.deleteExpress();
break;
case 3:
return 0;
default:
printf("输入有误,请重新输入!\n");
}
}
return 0;
}
```
注意,在使用 `scanf` 输入字符串时,需要保证输入的字符串长度不超过数组长度减一,以留出一个位置存放字符串结尾的 `\0` 字符。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)