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

📄 stop4.cpp

📁 停车场程序源代码
💻 CPP
字号:
#include <stdio.h>
#include<iostream>
#include<string.h>
#include<math.h>
#include<malloc.h>
using namespace std;

int n=2;//定义车场的容量为n,即场内最多能停n辆车

//模拟停车场的堆栈的性质
typedef struct car{     
    int number;    //汽车车号
    int arv_time;    //汽车到达时间
}Scar;

typedef struct{
   Scar   *base;    //停车场的堆栈底
   Scar    *top;    //停车场的堆栈顶
   int stacksize;
}Sstack;

//堆栈的基本操作;
int  initstack(Sstack &S)    //构造一个空栈
{
    S.base=(Scar*)malloc(sizeof(car));
    if(!S.base)     return -1;
    S.top=S.base;
    S.stacksize=0;
   return 0;
}
int push(Sstack  &S, Scar e) //把元素e压入s栈
{
    *S.top++=e;
    S.stacksize++;
	return 0;
}

int  pop(Sstack &S,  Scar &e)    //把元素e弹出s栈
{
    if(S.top==S.base) 
    {
        printf("停车场为空 !!");
        return  -1;
    }
    e=*--S.top;
    S.stacksize--;
	return 0;
}

//模拟便道的队列的性质;
typedef struct duilie{     
    int number;      //汽车车号
    int arv_time;      //汽车到达时间
   struct duilie *next;
}*queueptr;

typedef struct{
    queueptr front;    //便道的队列的对头
    queueptr rear;    //便道的队列的队尾
    int length;
}linkqueue;

//队列的基本操作;
int initqueue(linkqueue &Q) //构造一个空队列 
{
    Q.front=Q.rear=(queueptr)malloc(sizeof(duilie));
    if(!Q.front||!Q.rear)
        return -1;
    Q.front->next=NULL;
    Q.length=0;
	return 0;
}
int  enqueue(linkqueue &Q , int number , int arvtime) //在队尾插入一个新的元素(属性为number,ar_time
{
    queueptr p;
    p=(queueptr)malloc(sizeof(duilie));
    if(!p) return 0;
    p->number=number;
    p->arv_time=arvtime;
    p->next=NULL;
    Q.rear->next=p;
    Q.rear=p;
    Q.length++;
   return 0;

}
void Dequeue(linkqueue &Q, queueptr &w) //若队列不空,则删除队头元素,用w返回其值,(属性为number,ar_time)
{
    queueptr p;
    if(Q.front==Q.rear) 
    {
		cout<<"停车场的通道为空 ! !"<<endl;
		return;
    }
    p=Q.front->next;
    w=p;    
    Q.front->next=p->next;
    Q.length--;
    if(Q.rear==p)  Q.rear=Q.front;
	//free(p);
   
}

void Arv_Car(Sstack &S,  linkqueue &Q)    //对进入停车场的汽车的处理;
{
    int number,time_a;
  cout<<"车牌为:";
   cin>>number;
   cout<<"到达时刻:";
   cin>>time_a;
    if(S.stacksize<n)
    {
       Scar e;
       e.number=number;
       e.arv_time=time_a;
     push(S,e);
	cout<< " 该车已进入停车场,停在第 " <<S.stacksize<<"   号车道  "<<endl;
	}
    else
    {
     enqueue(Q,number,time_a);
	 cout<<"停车场已满,该车先停在便道的第  " << Q.length<<  "个位置上"<<endl<<endl;
     }
}

void depart_car(Sstack &St, Sstack &Sd, linkqueue &Q) //对离开的汽车的处理;
{                                                                                                        //St堆栈为停车场,Sd堆栈为倒车场
    int number=0, time_d=0, flag=1,money,arvtime;              //Q为便道队列
    Scar e,q_to_s;
    queueptr w;

	cout<<"车牌为:";
    cin>>number;
   cout<<endl<<"出场的时刻:";
   cin>>time_d;

    while(flag)    //找到要开出的车,并弹出停车场栈
    {
		  pop(St,e);
		  push(Sd,e);
		  if(e.number==number)
		  {
			 flag=0;
			 money=(time_d-e.arv_time)*2;
             arvtime=e.arv_time;
     }
    }

    pop(Sd,e);    //把临时堆栈的第一辆车(要离开的)去掉;

    while(Sd.stacksize) //把倒车场的车倒回停车场
    {
        pop(Sd,e);
        push(St,e);
    }

    if(St.stacksize<n && Q.length!=0) //停车场有空位,便道上的车进入停车场
    {
		 Dequeue(Q,w);
		 q_to_s.arv_time=time_d;
		 q_to_s.number=w->number;
		 push(St,q_to_s);
		cout<<"车牌为"<< q_to_s.number<<" 的车已从便道进入停车场,停在第 "<<St.stacksize<<"  号车道上"<<endl<<endl;
    }


  cout<<"----------------------------------------收据----------------------------------"<<endl; 
 cout<<"====================== 车牌号:   "<<number<<endl; 
  cout<<"=========================================================="<<endl;
  cout<<"|进车场时刻 | 出车场时刻 | 停留时间 | 应付(元)|"<<endl;
 cout<<"=========================================================="<<endl;
  cout<<"    |    "<<arvtime<< "     |     "<<time_d<< "     |     "<<time_d-arvtime<< "     |     "<<money<< "     |     "<<endl;
 cout<<"---------------------------------------------------------------------------------"<<endl;
}

int main()
{
 int m=100;
 char flag;              //进入或离开的标识;
 Sstack St , Sd;    //停车场和临时倒车场堆栈的定义;
 linkqueue   line;        //队列的定义;
 initstack(St);        //构造停车场堆栈sting
 initstack(Sd);        //构造倒车场堆栈slinshi
 initqueue(line);          //构造便道队列line
   
 //printf(" 请输入停车场的容量 n:   ");
// scanf_s("%d",n);
   cout<<" ***********************************停车场管理程序************************************"<<endl<<endl;
   cout<<"================================================"<<endl;
   cout<<" **********A --- 汽车进车场;    D --- 汽车出车场 ;   E --- 退出程序 *****"<<endl;
  cout<<"================================================"<<endl<<endl;
 while(m)
 {
 
 cout<<" 请选择 :(A,D,E): ";
   cin>>flag;
   switch(flag)
     {
     case 'A':Arv_Car(St,line);break;        //汽车进车场
     case 'D':depart_car(St,Sd,line);break; //汽车出车场
     case 'E': return 0;
     }
     m--;
 }


} 


⌨️ 快捷键说明

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