#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define STACK_SIZE 4
#define QUEUE_SIZE 100
#define OK 0
#define ERROR -1
#define MALLOC_ERROR -2
int MARK;
int RUN;
int COUNT;
int Get_time(void);
/*****************************我是淫荡分割线******************************/
//队列部分
typedef struct
{
int data[QUEUE_SIZE][2];
int rear, front;
}SeqQueue;
// 置空队列
int InitQueue(SeqQueue *q)
{
if (q == NULL)
{
return ERROR;
}
q->rear = 0;
q->front = 0;
return OK;
}
// 判断是否空队
int QueueEmpty(SeqQueue *q)
{
if (q == NULL)
{
return ERROR;
}
return q->rear == q->front;
}
// 判断是否队满
int QueueFull(SeqQueue *q)
{
if (q == NULL)
{
return ERROR;
}
return (q->rear+1)%QUEUE_SIZE == q->front;
}
// 入队
int EnQueue(SeqQueue *q, int data, int time)
{
if (q == NULL)
{
return ERROR;
}
if (QueueFull(q))
{
return ERROR;
}
q->rear = (q->rear+1) % QUEUE_SIZE;
q->data[q->rear][0] = data;
q->data[q->rear][1] = time;
return OK;
}
// 出队
int DeQueue(SeqQueue *q, int *data, int *time)
{
if (q == NULL)
{
return ERROR;
}
if (QueueEmpty(q))
{
return ERROR;
}
*data = q->data[(q->front+1) % QUEUE_SIZE][0];
*time = q->data[(q->front+1) % QUEUE_SIZE][1];
q->front = (q->front+1) % QUEUE_SIZE;
return q->data[q->front][0];
}
// 获取队头元素
int GetFront(SeqQueue *q)
{
if (q == NULL)
{
return ERROR;
}
if (QueueEmpty(q))
{
return ERROR;
}
int pos = (q->front+1) % QUEUE_SIZE;
return q->data[pos][0];
}
/*****************************我是淫荡分割线******************************/
//栈部分
typedef struct
{
int data[STACK_SIZE][2]; // 栈数组
int top; // 栈顶下标
}SeqStack;
// 置空栈
int InitStack (SeqStack *s)
{
if (s == NULL)
{
return ERROR;
}
s->top = -1;
return OK;
}
// 判断空栈
int StackEmpty(SeqStack *s)
{
if (s == NULL)
{
return ERROR; // 错误返回-1
}
return s->top == -1; // 相等返回1 不相等返回0
}
// 判断满栈
int StackFull(SeqStack *s)
{
if (s == NULL)
{
return ERROR;
}
return s->top == STACK_SIZE-1;
}
// 压栈
int Push(SeqStack *s, int data, int time)
{
if (s == NULL)
{
return ERROR;
}
// 判断是否满栈
if (StackFull(s))
{
return ERROR;
}
s->top++;
s->data[s->top][0] = data;
s->data[s->top][1] = time;
// s->data[++s->top] = data;
return OK;
}
// 出栈
int Pop(SeqStack *s, int *data, int *time)
{
if (s == NULL)
{
return ERROR;
}
// 判断是否空栈
if (StackEmpty(s))
{
return ERROR;
}
*data = s->data[s->top][0];
*time = s->data[s->top][1];
s->top--;
return data[0];
}
// 获取栈顶元素
int GetTop(SeqStack *s)
{
if (s == NULL)
{
return ERROR;
}
// 判断是否空栈
if (StackEmpty(s))
{
return ERROR;
}
return s->data[s->top][0];
}
/*****************************我是淫荡分割线******************************/
//不可描述部分
int Home(void)
{
printf("\t##########################################################\n");
printf("\t## 停车场 ##\n");
printf("\t## ##\n");
printf("\t## PARK ---------------------停车 ##\n");
printf("\t## LEAVE ---------------------离开 ##\n");
printf("\t## DISPLAY---------------------查看停车场状况 ##\n");
printf("\t## QUIT ---------------------退出 ##\n");
printf("\t## ##\n");
printf("\t## 作者:陈志辉 ##\n");
printf("\t##########################################################\n");
}
void Initsystem(void)
{
MARK = 0;
RUN = 1;
}
void Enter(void)
{
char a;
printf("请按任意键继续:");
a=getchar();
printf("\n");
}
void Quit(void)
{
RUN = 0;
}
//识别输入的命令
int Scan_order(void)
{
char a[11];
char *p1 = "PARK";
char *p2 = "LEAVE";
char *p3 = "DISPLAY";
char *p4 = "QUIT";
scanf("%11s",a);
int i;
if(a[11] == '\0')
{
return -1;//长度越界!
}
else if(strcmp(a,p1) == 0)
{
MARK = 1;
}
else if(strcmp(a,p2) == 0)
{
MARK = 2;
}
else if(strcmp(a,p3) == 0)
{
MARK = 3;
}
else if(strcmp(a,p4) == 0)
{
MARK = 4;
}
else
{
return -1;//未找到命令
}
return 0;
}
//获取时间
int Get_time(void)//六位数组
{
/* int *temp =p; */
time_t now ;
struct tm *t ;
time(&now) ;
t = localtime(&now);
/* *(temp++) = t->tm_year+1900;
*(temp++) = t->tm_mon+1;
*(temp++) = t->tm_mday;
*(temp++) = t->tm_hour;
*(temp++) = t->tm_min;
*temp = t->tm_sec; */
return (t->tm_mon+1)*1000000+t->tm_mday*10000+t->tm_hour*100+t->tm_min;
}
/*****************************我是淫荡分割线******************************/
//主函数部分
int main()
{
Initsystem();
SeqQueue queue; //队列变量
if (InitQueue(&queue) != OK)
{
return -1;
}
SeqStack stack; // 停车栈结构体变量
if (InitStack(&stack) != OK)
{
return -1;
}
SeqStack stackr; // 让路栈结构体变量
if (InitStack(&stackr) != OK)
{
return -1;
}
int i;
int data[2];
int temp;;
while(RUN)
{
switch(MARK)
{
case 0://主界面
{
//Enter();
//system("clear");
Home();
printf("\t停车场共%d个停车位,当前停车场共有%d辆车,等待区共有%d辆车\n",STACK_SIZE,stack.top+1,(queue.rear+QUEUE_SIZE-queue.front)%QUEUE_SIZE);
printf("请输入命令:");
while(Scan_order())
{
COUNT++;
printf("已输错%d次!!\n",COUNT);
if(COUNT > 4)
{
printf("系统即将退出!!!\n");
RUN = 0;
break;
}
for(i = COUNT ;i > 0 ;i--)
{
printf("等待%d秒后输入...\n",i);
sleep(1);
}
printf("请输入命令:");
}
COUNT = 0;
break;
}
case 1://停车
{
printf("请输入车号:");
scanf("%d",&data[0]);
if (StackFull(&stack))
{
if(QueueFull(&queue))
{
printf("停车场、候车场已满!!!\n");
printf("欢迎下次光临,再见!!!\n");
MARK = 0;
break;
}
printf("停车场已满,进入候车场排队等候\n");
EnQueue(&queue ,data[0],Get_time());
printf("%d% d当前有%d辆车在候车队列中\n",queue.front,queue.rear,(queue.rear+QUEUE_SIZE-queue.front)%QUEUE_SIZE);
}
else
{
printf("你的爱车已经可以进入停车场\n");
Push(&stack ,data[0],Get_time());
}
MARK = 0;
break;
}
case 2://离开
{
printf("请输入车号:");
scanf("%d",&temp);
while(GetTop(&stack) != temp && !StackEmpty(&stack))
{
Pop(&stack,&data[0],&data[1]);
Push(&stackr,data[0],data[1]);
}
if(StackEmpty(&stack))
{
printf("车库中不存在您输入的车号!\n");
MARK = 0;
break;
}
Pop(&stack,&data[0],&data[1]);
i = (Get_time()/100%100-data[1]/100%100)*60 + Get_time()%100-data[1]%100;
if(!i)//不满一分钟按一分钟计算
{
i = 1;
}
printf("停车时间:%d小时%d分钟\n",i/60,i%60);
printf("停车收费:%d RMB\n",100*i);
while(!StackEmpty(&stackr))
{
Pop(&stackr,&data[0],&data[1]);
Push(&stack,data[0],data[1]);
}
while(!StackFull(&stack) && !QueueEmpty(&queue))
{
DeQueue(&queue,&data[0],&data[1]);
Push(&stack,data[0],data[1]);
}
MARK = 0;
break;
}
case 3://查看停车场状况
{
i = 0;
printf("当前有%d辆车在停车场中:\n",stack.top+1);
while(i <= stack.top)
{
printf("%d\t",stack.data[i][0]);
printf("%d\n",stack.data[i][1]);
i ++;
}
i = 1;
printf("当前有%d辆车在候车队列中\n",(queue.rear+QUEUE_SIZE-queue.front)%QUEUE_SIZE);
while(queue.rear != queue.front + i - 1)
{
printf("%d\t",queue.data[(queue.front+i)%QUEUE_SIZE][0]);
printf("%d\n",queue.data[(queue.front+i)%QUEUE_SIZE][1]);
i ++;
}
MARK = 0;
break;
}
case 4://退出
{
RUN = 0;
break;
}
default:
{
break;
}
}
}
return 0;
}
#include <stdlib.h>
#include <time.h>
#define STACK_SIZE 4
#define QUEUE_SIZE 100
#define OK 0
#define ERROR -1
#define MALLOC_ERROR -2
int MARK;
int RUN;
int COUNT;
int Get_time(void);
/*****************************我是淫荡分割线******************************/
//队列部分
typedef struct
{
int data[QUEUE_SIZE][2];
int rear, front;
}SeqQueue;
// 置空队列
int InitQueue(SeqQueue *q)
{
if (q == NULL)
{
return ERROR;
}
q->rear = 0;
q->front = 0;
return OK;
}
// 判断是否空队
int QueueEmpty(SeqQueue *q)
{
if (q == NULL)
{
return ERROR;
}
return q->rear == q->front;
}
// 判断是否队满
int QueueFull(SeqQueue *q)
{
if (q == NULL)
{
return ERROR;
}
return (q->rear+1)%QUEUE_SIZE == q->front;
}
// 入队
int EnQueue(SeqQueue *q, int data, int time)
{
if (q == NULL)
{
return ERROR;
}
if (QueueFull(q))
{
return ERROR;
}
q->rear = (q->rear+1) % QUEUE_SIZE;
q->data[q->rear][0] = data;
q->data[q->rear][1] = time;
return OK;
}
// 出队
int DeQueue(SeqQueue *q, int *data, int *time)
{
if (q == NULL)
{
return ERROR;
}
if (QueueEmpty(q))
{
return ERROR;
}
*data = q->data[(q->front+1) % QUEUE_SIZE][0];
*time = q->data[(q->front+1) % QUEUE_SIZE][1];
q->front = (q->front+1) % QUEUE_SIZE;
return q->data[q->front][0];
}
// 获取队头元素
int GetFront(SeqQueue *q)
{
if (q == NULL)
{
return ERROR;
}
if (QueueEmpty(q))
{
return ERROR;
}
int pos = (q->front+1) % QUEUE_SIZE;
return q->data[pos][0];
}
/*****************************我是淫荡分割线******************************/
//栈部分
typedef struct
{
int data[STACK_SIZE][2]; // 栈数组
int top; // 栈顶下标
}SeqStack;
// 置空栈
int InitStack (SeqStack *s)
{
if (s == NULL)
{
return ERROR;
}
s->top = -1;
return OK;
}
// 判断空栈
int StackEmpty(SeqStack *s)
{
if (s == NULL)
{
return ERROR; // 错误返回-1
}
return s->top == -1; // 相等返回1 不相等返回0
}
// 判断满栈
int StackFull(SeqStack *s)
{
if (s == NULL)
{
return ERROR;
}
return s->top == STACK_SIZE-1;
}
// 压栈
int Push(SeqStack *s, int data, int time)
{
if (s == NULL)
{
return ERROR;
}
// 判断是否满栈
if (StackFull(s))
{
return ERROR;
}
s->top++;
s->data[s->top][0] = data;
s->data[s->top][1] = time;
// s->data[++s->top] = data;
return OK;
}
// 出栈
int Pop(SeqStack *s, int *data, int *time)
{
if (s == NULL)
{
return ERROR;
}
// 判断是否空栈
if (StackEmpty(s))
{
return ERROR;
}
*data = s->data[s->top][0];
*time = s->data[s->top][1];
s->top--;
return data[0];
}
// 获取栈顶元素
int GetTop(SeqStack *s)
{
if (s == NULL)
{
return ERROR;
}
// 判断是否空栈
if (StackEmpty(s))
{
return ERROR;
}
return s->data[s->top][0];
}
/*****************************我是淫荡分割线******************************/
//不可描述部分
int Home(void)
{
printf("\t##########################################################\n");
printf("\t## 停车场 ##\n");
printf("\t## ##\n");
printf("\t## PARK ---------------------停车 ##\n");
printf("\t## LEAVE ---------------------离开 ##\n");
printf("\t## DISPLAY---------------------查看停车场状况 ##\n");
printf("\t## QUIT ---------------------退出 ##\n");
printf("\t## ##\n");
printf("\t## 作者:陈志辉 ##\n");
printf("\t##########################################################\n");
}
void Initsystem(void)
{
MARK = 0;
RUN = 1;
}
void Enter(void)
{
char a;
printf("请按任意键继续:");
a=getchar();
printf("\n");
}
void Quit(void)
{
RUN = 0;
}
//识别输入的命令
int Scan_order(void)
{
char a[11];
char *p1 = "PARK";
char *p2 = "LEAVE";
char *p3 = "DISPLAY";
char *p4 = "QUIT";
scanf("%11s",a);
int i;
if(a[11] == '\0')
{
return -1;//长度越界!
}
else if(strcmp(a,p1) == 0)
{
MARK = 1;
}
else if(strcmp(a,p2) == 0)
{
MARK = 2;
}
else if(strcmp(a,p3) == 0)
{
MARK = 3;
}
else if(strcmp(a,p4) == 0)
{
MARK = 4;
}
else
{
return -1;//未找到命令
}
return 0;
}
//获取时间
int Get_time(void)//六位数组
{
/* int *temp =p; */
time_t now ;
struct tm *t ;
time(&now) ;
t = localtime(&now);
/* *(temp++) = t->tm_year+1900;
*(temp++) = t->tm_mon+1;
*(temp++) = t->tm_mday;
*(temp++) = t->tm_hour;
*(temp++) = t->tm_min;
*temp = t->tm_sec; */
return (t->tm_mon+1)*1000000+t->tm_mday*10000+t->tm_hour*100+t->tm_min;
}
/*****************************我是淫荡分割线******************************/
//主函数部分
int main()
{
Initsystem();
SeqQueue queue; //队列变量
if (InitQueue(&queue) != OK)
{
return -1;
}
SeqStack stack; // 停车栈结构体变量
if (InitStack(&stack) != OK)
{
return -1;
}
SeqStack stackr; // 让路栈结构体变量
if (InitStack(&stackr) != OK)
{
return -1;
}
int i;
int data[2];
int temp;;
while(RUN)
{
switch(MARK)
{
case 0://主界面
{
//Enter();
//system("clear");
Home();
printf("\t停车场共%d个停车位,当前停车场共有%d辆车,等待区共有%d辆车\n",STACK_SIZE,stack.top+1,(queue.rear+QUEUE_SIZE-queue.front)%QUEUE_SIZE);
printf("请输入命令:");
while(Scan_order())
{
COUNT++;
printf("已输错%d次!!\n",COUNT);
if(COUNT > 4)
{
printf("系统即将退出!!!\n");
RUN = 0;
break;
}
for(i = COUNT ;i > 0 ;i--)
{
printf("等待%d秒后输入...\n",i);
sleep(1);
}
printf("请输入命令:");
}
COUNT = 0;
break;
}
case 1://停车
{
printf("请输入车号:");
scanf("%d",&data[0]);
if (StackFull(&stack))
{
if(QueueFull(&queue))
{
printf("停车场、候车场已满!!!\n");
printf("欢迎下次光临,再见!!!\n");
MARK = 0;
break;
}
printf("停车场已满,进入候车场排队等候\n");
EnQueue(&queue ,data[0],Get_time());
printf("%d% d当前有%d辆车在候车队列中\n",queue.front,queue.rear,(queue.rear+QUEUE_SIZE-queue.front)%QUEUE_SIZE);
}
else
{
printf("你的爱车已经可以进入停车场\n");
Push(&stack ,data[0],Get_time());
}
MARK = 0;
break;
}
case 2://离开
{
printf("请输入车号:");
scanf("%d",&temp);
while(GetTop(&stack) != temp && !StackEmpty(&stack))
{
Pop(&stack,&data[0],&data[1]);
Push(&stackr,data[0],data[1]);
}
if(StackEmpty(&stack))
{
printf("车库中不存在您输入的车号!\n");
MARK = 0;
break;
}
Pop(&stack,&data[0],&data[1]);
i = (Get_time()/100%100-data[1]/100%100)*60 + Get_time()%100-data[1]%100;
if(!i)//不满一分钟按一分钟计算
{
i = 1;
}
printf("停车时间:%d小时%d分钟\n",i/60,i%60);
printf("停车收费:%d RMB\n",100*i);
while(!StackEmpty(&stackr))
{
Pop(&stackr,&data[0],&data[1]);
Push(&stack,data[0],data[1]);
}
while(!StackFull(&stack) && !QueueEmpty(&queue))
{
DeQueue(&queue,&data[0],&data[1]);
Push(&stack,data[0],data[1]);
}
MARK = 0;
break;
}
case 3://查看停车场状况
{
i = 0;
printf("当前有%d辆车在停车场中:\n",stack.top+1);
while(i <= stack.top)
{
printf("%d\t",stack.data[i][0]);
printf("%d\n",stack.data[i][1]);
i ++;
}
i = 1;
printf("当前有%d辆车在候车队列中\n",(queue.rear+QUEUE_SIZE-queue.front)%QUEUE_SIZE);
while(queue.rear != queue.front + i - 1)
{
printf("%d\t",queue.data[(queue.front+i)%QUEUE_SIZE][0]);
printf("%d\n",queue.data[(queue.front+i)%QUEUE_SIZE][1]);
i ++;
}
MARK = 0;
break;
}
case 4://退出
{
RUN = 0;
break;
}
default:
{
break;
}
}
}
return 0;
}