c语言程序课程设计2.1【问题描述】 设停车场是一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。

2.1【问题描述】
设停车场是一个可停放n辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆到达时间的先后顺序,依次由北向南排列(大门在最南端,最先到达的第一辆车停放在停车场的最北端),若停车场内已停满n辆汽车,则后来的汽车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,在它之后进入的车辆必须先退出车场为它让路,待该辆车开出大门外,其他车辆再按原次序进入车场,每辆停放在车场的车在它离开停车场时必须按它停留的时间长短交纳费用。试为停车场编制按上述要求进行管理的模拟程序。
2.2【设计需求及分析】
以栈模拟停车场,以队列模拟车场外的便道,按照从终端读入的输入数据序列进行模拟管理。每一组输入数据包括三个数据项:汽车“到达”或“离去”信息、汽车牌照号码以及到达或离去的时刻。对每一组输入数据进行操作后的输出信息为:若是车辆到达,则输出汽车在停车场内或便道上的停车位置;若是车辆离去,则输出汽车在停车场内停留的时间和应交纳的费用(在便道上停留的时间不收费)。栈以顺序结构实现,队列以链表结构实现。

2.3【设计功能的实现】(用C或C++语言描述)
//说明:此内容由学生自己设计完成。

2.4【实例测试及运行结果】
设n=2,输入数据为:(‘A’,1,5),(‘A’,2,10),(‘D’,1,15),(‘A’,3,20),(‘A’,4,25),(‘A’5,30),(‘D’,2,35),(‘D’,4,40),(‘E’,0,0)。其中:‘A’表示到达(arrival);‘D’表示离去(departure);‘E’表示输入结束(end)。

2.5【实现提示】
需另设一个栈,临时停放为给要离去的汽车让路而从停车场退出来的汽车,也用顺序存储结构实现。输入数据按到达或离去的时刻有序。栈中每个元素表示一辆汽车,包含两个数据项:汽车的牌照号码和进入停车场的时刻

第1个回答  2018-07-07
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#define ERROR 0
#define OK 1
#define TRUE 1
#define FALSE 0
typedef int SElemType;
typedef int Status;
typedef int CarStack;
typedef int LinkQueue;
typedef int InputData;

// definition of array based stack
#define STACK_INIT_SIZE 100 //Initial size for memory allocation
#define STACKINCREMENT 10 //incremental size
typedef struct{
SElemType *base; //base pointer
SElemType *top; //top pointer
CarStack *garage;
CarStack *Temp;
LinkQueue *road;
InputData *inputdata
int stacksize; //current size
}SqStack;
status PopStack(CarStack *garage, CarStack *Temp, LinkQueue *road, InputData *inputdata)//车出库
{
CarNode *Car = (CarNode *)malloc(sizeof(CarNode));
if (!Car)
{
cout << "内存分配失败!" << endl;
exit(0);
}
Car->num = inputdata->num;
Car->leave = inputdata->t;
int i = garage->top;
while (i)
{
if (garage->stack[i -1 ]->num != Car->num)
{
garage->top--;//进临时车站
Temp->stack[Temp->top] = garage->stack[garage->top];
Temp->top++;

i--;//没有找到,继续循环查找
if (i == 0)
{
cout << "没有该车牌的车!" << endl;
}
}
else//获得车辆信息,并回归临时车栈的车辆
{
Car->reach = garage->stack[i -1 ]->reach;
Print(Car);//打印车辆收费单
garage->top--;
while (Temp->top>0)//回归临时车站的车辆
{
Temp->top--;
garage->stack[garage->top] = Temp->stack[Temp->top];
garage->top++;
}
if (road->head != road->rear)//从便道出来一辆车到停车场
{
garage->stack[garage->top] = road->head->next;
cout << endl;
cout << "车场有空位,便道第一辆车可以进入!" << endl;
garage->top++;
road->head = road->head->next;
}
i = 0;//已经找到,退出循环
}
}
}

void InfoStack(CarStack *garage)//车库车辆信息
{
if (garage->top ==0 )
cout << "车库里没有车!" << endl;
else
{
cout << " _________________________ " << endl;
cout << "| |" << endl;
cout << "| 车 库 |" << endl;
cout << "|位置 车辆号码 到达时间|" << endl;
for (int i = 0; i<garage->top; i++)
{
cout << "| " << i +1 << " " << garage->stack[i]->num << " " << garage->stack[i]->reach << " |" << endl;
}
cout << "|_________________________|" << endl;
}
}

void InfoQueue(LinkQueue *road)//便道车辆信息
{
CarNode *p;
p = road->head->next;
int i = 0;
if (road->head == road->rear)
cout << "便道里没有车!" << endl;
else
{
cout << " __________________ " << endl;
cout << "| |" << endl;
cout << "| 便道 |" << endl;
cout << "| 位置 车辆号码 |" << endl;
while (p != NULL)
{
cout << "| " << ++i << " " << p->num << " |" << endl;
p = p->next;
}
cout << "|__________________|" << endl;
}
free(p);
}

void Print(CarNode *Car)//打印出站车辆信息
{
int rh, lh;
float t;
rh = Car->reach;
lh = Car->leave;
t = (lh - rh) * 60 *0.01 ;
cout << " ---------------------------------------------------- " << endl;
cout << "| 车辆的号码 进库时间 离开时间 车费(元) |" << endl;
cout << "| " << Car->num << " " << rh << " " << lh << " " << t << " |" << endl;
cout << "|____________________________________________________|" << endl;
}

void Input(InputData *inputdata)
{

int maxsize;
cout << "请输入停车场容量 Maxsize (最多为5) : ";
do
{
cin >> maxsize;
if (!(maxsize<5 && maxsize>0))
{
cout << "输入有误,请重新输入停车场容量:";
}

} while (!(maxsize<5 && maxsize>0));

int i = 0, j =0 , k =1 ;
char c;
int n;
CarStack garage, Temp;
LinkQueue road;
CarNode car;
InitStack(&garage, maxsize);
InitStack(&Temp, maxsize);
InitQueue(&road);

while (k)
{
i = 0;
while (!i)
{
cout << " ____________________________________________________________" << endl;
cout << "| |" << endl;
cout << "| 欢迎光临停车场,本停车场每分钟收费元 |" << endl;
cout << "| |" << endl;
cout << "| 请输入车库命令,格式如下(命令,车牌号,入库时间) |" << endl;
cout << "| |" << endl;
cout << "| A(a)-入库 D(d)-离开 P(p)-查看停车场车辆 P(p)-查看过道车辆 |" << endl;
cout << "|____________________________________________________________|" << endl;

cin >> (inputdata->command) >> (inputdata->num) >> (inputdata->t);

c = inputdata->command;
n = inputdata->num;

if (c != 'A'&&c != 'a'&&c != 'D'&&c != 'd'&&c != 'W'&&c != 'w'&&c != 'P'&&c != 'p'&&c != 'E'&&c != 'e')
{
cout << "命令不正确,请重新输入!" << endl;
i =0 ;
}
else
i = 1;
// if(SearchInGarage(garage,n,maxsize)&&SearchInRoad(road,n))
// {
// i =0 ;
// cout<<"命令不正确,请重新输入!"<<endl;
// }

}
switch (c)
{
case 'A':
case 'a':
GetCarNode(&garage, &road, inputdata, maxsize);//获取车辆信息
break;
case 'D':
case 'd':
PopStack(&garage, &Temp, &road, inputdata);//车出库
break;
case 'P':
case 'p'://查询停车场的信息
InfoStack(&garage);//车库车辆信息
break;
case 'W':
case 'w'://查询侯车场的信息
InfoQueue(&road);//便道车辆信息
break;
case 'E':
case 'e':
k =0 ;
break;
default:
break;
}
}
cout << endl;
cin.clear();
}

bool SearchInGarage(CarStack garage, int n, int maxsize)//a表示要查找的车牌号,如果在停车场里面,就返回true
{
bool tag = false;
for (int i =0 ; i<maxsize || (i != garage.top); i++)
{
if (n == garage.stack[i]->num)
{
tag = true;
break;
}
}
return tag;
}

bool SearchInRoad(LinkQueue road, int n)//a表示要查找的车牌号,如果在通道里面,就返回true
{
bool tag = false;
StackNode *p;
p = (StackNode *)malloc(sizeof(StackNode));
if (road.head != road.rear)//如果队列非空
{
p = road.head;
while (p != road.rear)
{
++p;
if (p->num == n)
tag = true;
}//退出此while循环时p指向最后一个元素
}
free(p);
return tag;
}本回答被网友采纳
相似回答