本帖最后由 Cool_Breeze 于 2020-8-24 15:28 编辑
# 当练习题做了/*
# 还有许多功能没有写!
# BY Cool_Breeze
# 2020/08/24
# GCC .c
*/
#include<stdio.h>
#include<malloc.h>
typedef unsigned char uchar;
typedef unsigned int uint;
#define N 10 // 格子数量
uchar ABLE = 0; //可用箱子数量
typedef struct GRID
{
uchar id;
uchar status;
uint password;
struct GRID* next;
}GRIDV,*GRIDP;
// 初始化储物柜格子
GRIDP __init__(void);
// 打印格子信息
// check = 1 只打印可用数量
// check = 2 打印格子信息和可用数量
// check = 0 检查可用格子数量
void print_grid(GRIDP grid, uchar check);
// 操作菜单
uchar operation_menu(GRIDP head);
// 存物品
void store_items(GRIDP);
// 取物品
uchar get_items(GRIDP head, int* passwd);
// 管理员
void adminstrator(GRIDP grid);
int main(void)
{
//system("chcp 936");
GRIDP locker = __init__();
print_grid(locker, 1);
operation_menu(locker);
return 0;
}
GRIDP __init__(void)
{
uchar i = 0;
GRIDP head, cnter, grid;
for (; i<N; i++)
{
grid = (GRIDP)malloc(sizeof(GRIDV));
if (i == 0)
{
head = cnter = grid;
grid->id = i + 1;
grid->status = 0;
grid->password = 0;
grid->next = NULL;
}
else
{
cnter->next = grid;
grid->id = i + 1;
grid->status = 0;
grid->password = 0;
grid->next = NULL;
cnter = grid;
}
}
return head;
}
void print_grid(GRIDP grid, uchar check)
{
ABLE = 0;
uchar i = 0;
for (; i<N; i++)
{
if (grid->status == 0) ABLE++;
if (check == 2)
printf("格子号:%d\t物品状态:%d\n", grid->id, grid->status);
grid = grid->next;
}
if (check) printf("可用格子数量为:%d\n", ABLE);
}
uchar operation_menu(GRIDP head)
{
uchar i = 0;
uchar number = 0;
uchar ad_passwd[6] = {0};
uchar ad_pd_ok[6] = {'0','0','0','0','8','6'}; // 管理员密码
uint passwd[2] = {0};
while (1)
{
printf("\n1. 投硬币\t2. 取走物品\t3.查询可用格子数量\t0. 退出\n请选择:");
scanf("%d", &number);
getchar();
switch (number)
{
case 1:
print_grid(head, 0);
if (!ABLE)
{
printf("储物柜已满!\n");
break;
}
else
{
store_items(head);
break;
}
case 2:
while (1)
{
*passwd = 0;
*(passwd+1) = 0;
printf("请输入格子号:");
scanf("%d", passwd);
getchar();
printf("请输入密码:");
scanf("%d", passwd+1);
getchar();
if (get_items(head, passwd))
{
printf("输入密码或者格子号错误!请重新选择\n");
break;
}
break;
}
break;
case 3:
print_grid(head, 1);
break;
case 5:
printf("请输入管理员密码:");
scanf("%s", ad_passwd);
getchar();
for (i=0; i<6; i++)
{
if (*(ad_passwd + i ) == *(ad_pd_ok + i ))
continue;
else
{
printf("管理员密码错误!请重新选择\n");
break;
}
}
if (i == 6)
{
adminstrator(head);
break;
}
break;
case 0:
return 0;
}
}
return 0;
}
void store_items(GRIDP grid)
{
// 密码就不写了
uchar i = 0;
for (; i<N; i++)
{
if (grid->status == 0)
{
grid->status = 1;
grid->password = 9342;
printf("格子号:%d\t密码:%d\n", grid->id, 9342);
break;
}
grid = grid->next;
}
}
uchar get_items(GRIDP grid, int* passwd)
{
uchar i = 0;
for (; i<N; i++)
{
if (grid->id == *passwd && grid->password == *(passwd+1))
{
grid->status = 0;
printf("格子号:%d 已打开\n", grid->id);
return 0;
}
grid = grid->next;
}
return 1;
}
void adminstrator(GRIDP grid)
{
GRIDP head = grid;
uchar i = 0;
for (; i<N; i++)
{
if (grid->status)
{
printf("请检查格子号:%d 是否存在物品\n", grid->id);
}
grid = grid->next;
}
printf("储物柜格子已经全部打开!\n");
// 默认管理员会清空全部格子
grid = head;
for (i=0; i<N; i++)
{
grid->status = 0;
grid = grid->next;
}
}
|