#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 2
#define price 0.05
typedef struct time{
int hour;
int min;
}Time;
typedef struct node{
char num[10];
Time reach;
Time leave;
}CarNode;
typedef struct NODE{
CarNode *stack[MAX+1];
int top;
}SeqStackCar;
typedef struct car{
CarNode *data;
struct car *next;
}QueueNode;
typedef struct Node{
QueueNode *head;
QueueNode *rear;
}LinkQueueCar;
/*------------------------------------------------------------------------------*/
void InitStack(SeqStackCar *);
int InitQueue(LinkQueueCar *);
int Arrival(SeqStackCar *,LinkQueueCar *);
void Leave(SeqStackCar *,SeqStackCar *,LinkQueueCar *);
void List(SeqStackCar,LinkQueueCar);
/*------------------------------------------------------------------------------*/
int main()
{
SeqStackCar Enter,Temp;
LinkQueueCar Wait;
int ch;
InitStack(&Enter);
InitStack(&Temp);
InitQueue(&Wait);
while(1)
{
printf("\n1. the car arrive");
printf(" 2. the car leave");
printf(" 3. the schedule ");
printf(" 4. out\n");
while(1)
{
scanf("%d",&ch);
if(ch>=1&&ch<=4)break;
else printf("\nplease choose: 1|2|3|4.");
}
switch(ch)
{
case 1:Arrival(&Enter,&Wait);break;
case 2:Leave(&Enter,&Temp,&Wait);break;
case 3:List(Enter,Wait);break;
case 4:exit(0);
default: break;
}
}
}
/*------------------------------------------------------------------------------*/
void InitStack(SeqStackCar *s)
{
int i;
s->top=0;
for(i=0;i<=MAX;i++)
s->stack[s->top]=NULL;
}
int InitQueue(LinkQueueCar *Q)
{
Q->head=(QueueNode *)malloc(sizeof(QueueNode));
if(Q->head!=NULL)
{
Q->head->next=NULL;
Q->rear=Q->head;
return(1);
}
else return(-1);
}
void PRINT(CarNode *p,int room)
{
int A1,A2,B1,B2;
printf("\nplease input thedepart time:/**:**/");
scanf("%d:%d",&(p->leave.hour),&(p->leave.min));
printf("\nthe number of the car:");
puts(p->num);
printf("\nthe time the car arrive: %d:%d",p->reach.hour,p->reach.min);
printf("the depart time: %d:%d",p->leave.hour,p->leave.min);
A1=p->reach.hour;
A2=p->reach.min;
B1=p->leave.hour;
B2=p->leave.min;
printf("\nthe fee: %2.1fY",((B1-A1)*60+(B2-A2))*price);
free(p);
}
int Arrival(SeqStackCar *Enter,LinkQueueCar *W)
{
CarNode *p;
QueueNode *t;
p=(CarNode *)malloc(sizeof(CarNode));
flushall();
printf("\ninput the number of the car(A1234):");
gets(p->num);
if(Enter->top<MAX)
{
Enter->top++;
printf("\nthe place of the car.",Enter->top);
printf("\nthe time thecar arrive:/**:**/");
scanf("%d:%d",&(p->reach.hour),&(p->reach.min));
Enter->stack[Enter->top]=p;
return(1);
}
else
{
printf("\nwait in LinkQueueCar!");
t=(QueueNode *)malloc(sizeof(QueueNode));
t->data=p;
t->next=NULL;
W->rear->next=t;
W->rear=t;
return(1);
}
}
void Leave(SeqStackCar *Enter,SeqStackCar *Temp,LinkQueueCar *W)
{
int i, room;
CarNode *p,*t;
QueueNode *q;
if(Enter->top>0)
{
while(1)
{
printf("\npleasw in put the place of the car 1 -- %d :",Enter->top);
scanf("%d",&room);
if(room>=1&&room<=Enter->top) break;
}
while(Enter->top>room)
{
Temp->top++;
Temp->stack[Temp->top]=Enter->stack[Enter->top];
Enter->stack[Enter->top]=NULL;
Enter->top--;
}
p=Enter->stack[Enter->top];
Enter->stack[Enter->top]=NULL;
Enter->top--;
while(Temp->top>=1)
{
Enter->top++;
Enter->stack[Enter->top]=Temp->stack[Temp->top];
Temp->stack[Temp->top]=NULL;
Temp->top--;
}
PRINT(p,room);
if((W->head!=W->rear)&&Enter->top<MAX)
{
q=W->head->next;
t=q->data;
Enter->top++;
printf("\n%s%d.",t->num,Enter->top);
printf("\nnow time/**:**/:");
scanf("%d:%d",&(t->reach.hour),&(t->reach.min));
W->head->next=q->next;
if(q==W->rear) W->rear=W->head;
Enter->stack[Enter->top]=t;
free(q);
}
else printf("\nLinkQueueCar no car.\n");
}
else printf("\nSeqStackCar no car.");
}
void List1(SeqStackCar *S)
{
int i;
if(S->top>0)
{
printf("\nSeqStackCar:");
printf("\n place time number\n");
for(i=1;i<=S->top;i++)
{
printf(" %d ",i);
printf("%d:%d ",S->stack[i]->reach.hour,S->stack[i]->reach.min);
puts(S->stack[i]->num);
}
}
else printf("\nSeqStackCar no car");
}
void List2(LinkQueueCar *W)
{
QueueNode *p;
p=W->head->next;
if(W->head!=W->rear)
{
printf("\n the number of the car:");
while(p!=NULL)
{
puts(p->data->num);
p=p->next;
}
}
else printf("\nLinkQueueCar no car.");
}
void List(SeqStackCar S,LinkQueueCar W)
{
int flag,tag;
flag=1;
while(flag)
{
printf("\nplease choose 1|2|3:");
printf("\n1.SeqStackCar\n2.LinkQueueCar\n3.exit\n");
while(1)
{
scanf("%d",&tag);
if(tag>=1||tag<=3) break;
else printf("\nplease choose 1|2|3:");
}
switch(tag)
{
case 1:List1(&S);break;
case 2:List2(&W);break;
case 3:flag=0;break;
default: break;
}
}
}
评论3
最新资源