停车场

来源:互联网 发布:.cx域名注册 编辑:程序博客网 时间:2024/04/26 03:21
#include<stdio.h>
#include<time.h>
#define OK        0   
#define ERROR    -1  


int gettime()
{
struct tm *ptr;
time_t t;
t=time(NULL);
return t;
}
int interface()
{
printf("\t\t**********Welcom to our parking**********\n");
printf("\t\t1:\tpark\n");
printf("\t\t2:\tleave\n");
printf("\t\t3:\tdisplay\n");
printf("\t\t4:\tquit\n");
}
//stop area
struct tcq
{
int tcq[20];
int tcqtime[20];
int top;
};
//set stop area to empty
int tcq_InitStack (struct tcq *s)
{
if (s == NULL)
{
return ERROR;
}
s->top = -1;
return OK;
}


int tcq_StackEmpty(struct tcq *s)
{
if (s == NULL)
{
return ERROR;      // 错误返回-1  
}

return s->top == -1;   // 相等返回1 不相等返回0
}


int tcq_StackFull(struct tcq *s)
{
if (s == NULL)
{
return ERROR;
}

return s->top == 19;
}


int tcq_Push(struct tcq *s, int data)
{
if (s == NULL)
{
return ERROR;
}
// 判断是否满栈
if (tcq_StackFull(s))
{
return ERROR;
}
s->top++;
s->tcq[s->top] = data;
s->tcqtime[s->top] = gettime();
// printf("time is%d\n",s->tcqtime[s->top]);
printf("success,and the cparking area number is%d\tthe car number is%d\n",s->top,s->tcq[s->top]);
return OK;
}
//back to hcq
int tcq_Pop(struct tcq *s)
{
if (s == NULL)
{
return ERROR;
}
// 判断是否空栈
if (tcq_StackEmpty(s))
{
return ERROR;
}
// printf("the back top is%d\nand the car number is%d\n",s->top,s->tcq[s->top]);
int data = s->tcq[s->top--];
// printf("%d\n",s->tcq[s->top]);
return data;
}


//wait area
struct hcq
{
int hcq[20];
int front1;
int rear1;
};
//set hcq to empty
int hcq_InitQueue(struct hcq *q)
{
if (q == NULL)
{
return ERROR;
}
q->rear1 = 0;
q->front1 = 0;
return OK;
}
int hcq_QueueEmpty(struct hcq *q)
{
if (q == NULL)
{
return ERROR;
}

return q->rear1 == q->front1;
}
//full or not
int hcq_QueueFull(struct hcq *q)
{
if (q == NULL)
{
return ERROR;
}

return (q->rear1+1)%20 == q->front1;
}
//input
int hcq_EnQueue(struct hcq *q, int data)
{
if (q == NULL)
{
return ERROR;
}

if (hcq_QueueFull(q))
{
return ERROR;
}

q->rear1 = (q->rear1+1) % 20;
q->hcq[q->rear1] = data;
printf("hcq success\n");
return OK;
}
int hcq_DeQueue(struct hcq *q)
{
if (q == NULL)
{
return ERROR;
}

if (hcq_QueueEmpty(q))
{
return ERROR;
}

q->front1 = (q->front1+1) % 20;

return q->hcq[q->front1];
}
//back area
struct hcdd
{
int hcdd[20];
int top0;

};
//for hcdd to empty
int hcdd_InitStack (struct hcdd *s)
{
if (s == NULL)
{
return ERROR;
}

s->top0 = -1;

return OK;
}
int hcdd_StackEmpty(struct hcdd *s)
{
if (s == NULL)
{
return ERROR;      
}

return s->top0 == -1;   
}
int hcdd_StackFull(struct hcdd *s)
{
if (s == NULL)
{
return ERROR;
}

return s->top0 == 19;
}
int hcdd_Push(struct hcdd *s, int data)
{
if (s == NULL)
{
return ERROR;
}
// 判断是否满栈
if (hcdd_StackFull(s))
{
return ERROR;
}
s->hcdd[++s->top0] = data;
// printf("the number in buffer area is%d\n",data);
return OK;
}
int hcdd_Pop(struct hcdd *s)
{
if (s == NULL)
{
return ERROR;
}

// 判断是否空栈
if (hcdd_StackEmpty(s))
{
return ERROR;
}

int data = s->hcdd[s->top0--];
printf("the car number in buffer area is%d\n",data);
return data;
}
int Push(struct tcq *s)
{
if (s == NULL)
{
return ERROR;
}
// 判断是否满栈
if (tcq_StackFull(s))
{
return ERROR;
}
s->tcq[s->top] = 21;
printf("then the back car data\n");
return OK;
}
int research(struct tcq *s)
{
if (s == NULL)
{

return ERROR;
}
if(s->top == -1)
{
printf("no car\n");
}
int f=gettime();
int i;
int alltime;
int hourtime;
int minutetime;
for(i=s->top;i>=0;i--)
{
printf("the car number%d\n",s->tcq[i]);
alltime=f-(s->tcqtime[i]);
hourtime=alltime/3600;
minutetime=(alltime%3600)/60;
printf("the parking time is%dhour%dminute\n",hourtime,minutetime);

}
return OK;

int searchtime(struct tcq *s)
{
if (s == NULL)
{
return ERROR;
}
int f=gettime();
int alltime;
int hourtime;
int minutetime;
alltime=f-(s->tcqtime[s->top]);
hourtime=alltime/3600;
minutetime=(alltime%3600)/60;
printf("the time of this car stoped in our parking is%dhour%dminute\n",hourtime,minutetime);
    return OK;
}
int main()
{
struct tcq a;
tcq_InitStack (&a);
struct hcq b;
hcq_InitQueue (&b);
struct hcdd c;
hcdd_InitStack (&c);
char m[2];          //for case
int i = 0;          //for tcq
int j = 0;          //for hcq
int k;          //fo back car number
int d;              //for the number after back car number
while(1)
{
interface();
printf("please input your choice\n");
scanf("%s",m);
switch(m[0])
{
case '1':
{
// printf("help\n");
if(tcq_StackFull(&a))
{
printf("has full,please wait\n");
if(hcq_QueueFull(&b)==0)
{
j++;
hcq_EnQueue(&b, j);
printf("hcq success\n");
}
}
else
{
i++;
tcq_Push(&a, i);
printf("success\n");
}
};
break;
case '2':
{
int e;
printf("please input the car number:\n");
scanf("%d",&k);
for(d=i;d>k;d--)
{
e=tcq_Pop(&a);
// printf("%d\n",e);
hcdd_Push(&c, e);
}
// printf("then is the back car number\n");
searchtime(&a);
tcq_Pop(&a);
/* if(hcq_QueueEmpty(&b) == 0)
{
hcq_DeQueue(&b);
}
else
{*/
// Push(&a);
// }
for(d=i;d>k;d--)
{
e=hcdd_Pop(&c);
tcq_Push(&a, e);
}
if(hcq_QueueEmpty(&b) == 0)
{
i++;
hcq_DeQueue(&b);
tcq_Push(&a, i);
}
};
break;
case '3':
{
// while(tcq_Pop(&a))
// {
research(&a);
// }
};
break;

}
}


return 0;
}
0 0
原创粉丝点击