停车场管理系统 普通用户和管理层用户 c语言
时间: 2023-09-10 22:14:59 浏览: 42
好的,停车场管理系统可以分为普通用户和管理层用户两个角色,以下是一份基于C语言的简单实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_SPOTS 50 // 最大停车位数量
#define MAX_NAME_LEN 20 // 最大用户名长度
typedef struct {
int id; // 停车位编号
char owner[MAX_NAME_LEN]; // 车主姓名
int occupied; // 是否被占用,0代表未被占用,1代表已被占用
} ParkingSpot;
void initParkingSpots(ParkingSpot spots[], int count) {
for (int i = 0; i < count; i++) {
spots[i].id = i + 1;
spots[i].occupied = 0;
strcpy(spots[i].owner, "");
}
}
void displayParkingSpots(ParkingSpot spots[], int count) {
printf("停车位\t车主\n");
for (int i = 0; i < count; i++) {
printf("%d\t%s\n", spots[i].id, spots[i].owner);
}
}
void park(ParkingSpot spots[], int count) {
int id;
char name[MAX_NAME_LEN];
printf("请输入您要停放的车位编号:");
scanf("%d", &id);
if (id < 1 || id > count) {
printf("输入的停车位编号不合法!\n");
return;
}
if (spots[id - 1].occupied == 1) {
printf("该停车位已被占用!\n");
return;
}
printf("请输入车主姓名:");
scanf("%s", name);
spots[id - 1].occupied = 1;
strcpy(spots[id - 1].owner, name);
printf("停车成功!\n");
}
void unpark(ParkingSpot spots[], int count) {
int id;
printf("请输入您要离开的车位编号:");
scanf("%d", &id);
if (id < 1 || id > count) {
printf("输入的停车位编号不合法!\n");
return;
}
if (spots[id - 1].occupied == 0) {
printf("该停车位未被占用!\n");
return;
}
spots[id - 1].occupied = 0;
strcpy(spots[id - 1].owner, "");
printf("离开成功!\n");
}
void login() {
char username[MAX_NAME_LEN];
char password[MAX_NAME_LEN];
printf("请输入用户名:");
scanf("%s", username);
printf("请输入密码:");
scanf("%s", password);
// 根据用户名和密码进行登录验证,这里简化为直接比较
if (strcmp(username, "admin") == 0 && strcmp(password, "123456") == 0) {
printf("管理员登录成功!\n");
} else {
printf("用户名或密码错误!\n");
}
}
int main() {
ParkingSpot spots[MAX_SPOTS];
int choice;
int count = 10; // 停车位数量
initParkingSpots(spots, count);
while (1) {
printf("\n请选择您要进行的操作:\n");
printf("1. 查看停车位\n");
printf("2. 停车\n");
printf("3. 离开\n");
printf("4. 管理员登录\n");
printf("5. 退出\n");
printf("请输入操作编号:");
scanf("%d", &choice);
switch (choice) {
case 1:
displayParkingSpots(spots, count);
break;
case 2:
park(spots, count);
break;
case 3:
unpark(spots, count);
break;
case 4:
login();
break;
case 5:
exit(0);
default:
printf("请选择正确的操作编号!\n");
}
}
return 0;
}
```
该程序实现了以下功能:
1. 查看停车位情况
2. 停车
3. 离开
4. 管理员登录
5. 退出
普通用户可以进行1、2、3操作,而管理员需要先进行4操作进行登录后才能进行其他操作,比如增加停车位、删除停车位等等。