⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bus3.cpp

📁 一个停车场管理的模拟程序!VC++6.0下开发~!
💻 CPP
字号:
#include<stdlib.h>
#include<stdio.h>
#include<iostream.h>
enum status{ERROR=0, OVERFLOW=0,OK=1};

typedef struct{
	char flag;
	int num;
	int time;
}car_infor;

typedef struct{
	car_infor *base;
	car_infor *top;
}sqstack;

typedef struct qnode{
	car_infor data;
	struct qnode *next;
}qnode,*queueptr;

typedef struct{
	queueptr front;
	queueptr rear;
}linkqueue;

status InitStack(sqstack &S)
{
	S.base=(car_infor*)malloc(2*sizeof(car_infor));
    if (!S.base)
		exit(OVERFLOW);												//存储分配失败
    S.top = S.base;
    return OK;
}																	// InitStack

status Push(sqstack &S, car_infor e) 
{

																	  //插入元素e为新的栈顶元素

	*S.top = e;
	S.top++;
	return OK;
}																			// Push

status Pop(sqstack &S, car_infor &e)
{							               // 若栈不空,则删除S的栈顶元素,用e返回
									   // 其值,并返回OK;否则返回ERROR
	if (S.top==S.base)						 // 栈空,栈溢出(下溢)
    return ERROR;
    e = *--S.top;
    return OK;
}												//Pop

status InitQueue (linkqueue &Q) 
{
                                            // 构造一个空队列Q
	Q.front = Q.rear =(queueptr)malloc(sizeof(qnode));
                      //产生一个头结点,头、尾指针均指向头结点
	if (!Q.front) exit (OVERFLOW);                   
                                           // 存储分配失败
    Q.front->next = NULL;
    return OK;
}      

status EnQueue (linkqueue &Q, car_infor e) 
{
	queueptr p;														// 插入元素e为Q的新的队尾元素
	p=(queueptr)malloc(sizeof(qnode));
    if (!p)  exit (OVERFLOW);								 // 存储分配失败
    p->data = e;   p->next = NULL;
    Q.rear->next = p;    Q.rear = p;
    return OK;
}

status DeQueue (linkqueue &Q, car_infor &e) {
													// 若队列不空,则删除Q的队头元素,
													// 用 e 返回其值,并返回OK;否则返回ERROR
	queueptr p;
   if (Q.front == Q.rear)    
	   return ERROR;									//队空
   p = Q.front->next; e = p->data;							//p指向队头元素   
   Q.front->next = p->next; 
																//修改头结点指针域,指向新的队头元素
   if (Q.rear==p) 
	   Q.rear=Q.front;
   free (p);														//释放被删掉的原队头元素
   return OK;
}

int main()
{
	sqstack    port;//停车场
	sqstack    temp;//出车时临时使用
	linkqueue  road;//便道
	car_infor  m,n;//输入时暂存数据
	int cartime,carcost,pos,i=0,j=0;//循环所用变量
	queueptr tr,tt;

	InitStack(port);
	InitStack(temp);
	InitQueue(road);

	printf("停车场容量:2 \n");

	printf("Please input the datas (A/D,number,time) : \n");//输入
	cin>>m.flag;
	cin>>m.num;
	cin>>m.time;
	while(m.flag!='E')//为E,退出
	{
		if(m.flag=='A')//为A,进车站
		{
			if(port.top-port.base<2)//停车场不满
			{
				Push(port , m);
				printf("The 位置 of car is: %d \n",port.top-port.base);
			}
			else//满了就进队列
			{
				i++;
				EnQueue(road , m);
				printf("The 位置 of car(in road) is: %d \n",i);
			}
		}
		else
			if(m.flag=='D')//有车要出来
			{
				Pop(port , n);
				while(n.num!=m.num&&port.top>port.base)//找到堆栈中的车
				{
					Push(temp , n);
					Pop(port , n);
				}
				if((port.base==port.top)&&n.num!=m.num)//如果不在堆栈中,就到队列中去找。
				{
					tr=road.front;
					tt=road.front;
					if(tr->data.num!=m.num)
					{
						tr=tr->next;
						j++;
					}
					while(tr->data.num!=m.num)//找到要出去的车
					{
						tr=tr->next;
						road.front=road.front->next;
						j++;
					}
						n=tr->data;
						road.front->next=road.front->next->next;
						road.front=tt;

						cartime=m.time-n.time;
						printf("The 车牌号 of car is : %d\n",m.num);//输出信息
						printf("The 位置 of car in the 便道 is : %d\n",j);
						printf("The 停车时间 of car is : %d\n",cartime);
						j=0;
				}
				else
				{
					if(n.num==m.num)//在堆栈中找到要出去的车
					{
						cartime=m.time-n.time;
						carcost=3*cartime;
						pos=port.top-port.base+1;

						printf("The 车牌号 of car is : %d\n",m.num);//输出信息
						printf("The 位置 of car in the 停车场 is : %d\n",pos);
						printf("The 停车时间 of car is : %d       and 费用 of car is :%d\n",cartime,carcost);
					//	printf("The cost of car is : $ %d\n",carcost);
						while(temp.top!=temp.base)
						{
							Pop(temp , n);
							Push(port , n);
						}
	
						if(road.front != road.rear)
						{
							DeQueue(road,n);
							Push(port,n);
						}
					}
				}
			}
		
				printf("Please input the datas (A/D,number,time) : \n");
				cin>>m.flag;
				cin>>m.num;
				cin>>m.time;
	}
	if(m.flag=='E')
		return OK;
	return OK;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -