首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

帮~忙啊解决办法

2012-02-15 
帮~~~~忙啊停车站管理1.有一个两层的停车场,每层6个车位,当第一层车停满后才允许使用第二层(停车场可用一

帮~~~~忙啊
停车站管理
1.               有一个两层的停车场,每层6个车位,当第一层车停满后才允许使用第二层(停车场可用一个2维数组实现,每个数组元素存放一个车牌号)每辆车的信息包括车牌号,层号,车位号,停车时间共四项,其中停车时间按分钟计算
2.               假设停车场初始状态为第一层已经停有4辆车,其车号依次为1——4,停车时间依次为20.15.10.5,即先将这四辆车的信息存入文件“car.date”中(数组的对应元素也要赋值)
3.               停车操作:当一辆车进入停车场时,先输入其车牌号,再为它分配一个层号和一个车位号(分配前先查询车位的使用情况,如果第一层有空则必须停在第一层),停车时间设为5,最后将新停入的汽车的信息添加文件“car.date”并将在此前的所有的停车时间加5。
4.               收费管理(取车):当有车离开时,输入其车牌号,先按其停车时间计算费用,每5分钟0.2元,(停车费用可设置一个变量进行保存),同时从文件“car.date”中删除该车的信息,并将该车对应的车位设置为可使用的状态(即二维数组对应元素清零)按用户的选择来判断是否要输出停车收费的总计。
5.               输出停车场中全部车辆的信息
6.               退出系统
哪为大虾帮编一下~~~


[解决办法]
供参考:

用TC编的。主要的要求是:设有一个车站,用栈的结构表示;车站的外面有边道,用队列的结构表示。当车辆进栈的时候,如果栈中有空位,则进栈;如果没有空位,则在边道上等候,直到有栈中的车辆出栈才可以进栈。且出栈的车辆要按在栈中停留的时间交纳费用。

在其中设了一个辅助栈,用来临时容纳为要给离去的车辆让路而退出来的车辆。输入的数据有三个数据项:车辆的进栈或出栈,车牌号,车子 "进出栈 "的时刻;栈中则为车牌号, "进栈 "的时刻;队列中则为车牌号,因为车辆在边道上的等待时间并不计入费用中。


#include <stdio.h>
#include <stdlib.h>
struct {
char status;
int num;
int time;
}a; /*命令的结构*/
typedef struct{
int num;
int time;
}Element;
struct {
Element *base;
Element *top;
int stacksize;
}S,VS; /*S为栈,VS为辅助栈*/
void main(){
typedef struct{
int num;
struct QNode *next;
}QNode,*QueuePtr;
QueuePtr l;
struct {
QueuePtr front;
QueuePtr rear;
}Q; /*队列*/
int n,x,m=0,order,money,b=0;
printf( "\nInput the size of the garage and the cost per hour: ");
scanf( "%d%d ",&n,&x);
S.base=(Element*)malloc(n*sizeof(Element));
S.top=S.base;
S.stacksize=n;
VS.base=(Element *)malloc((n-1)*sizeof(Element));
VS.top=VS.base;
VS.stacksize=n-1;
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
Q.front-> next=NULL; /*各结构的初始化*/
while (b!=1){
printf( "\nInput the order!: ");
scanf( "%c,%d,%d ",&(a.status),&(a.num),&(a.time));
switch(a.status)
{
case 'E ':b=1;break;
case 'A ':
if (S.top-S.base <S.stacksize){
(*(S.top)).num=a.num;
(*(S.top)).time=a.time;
S.top++;
order=S.top-S.base;
printf( "The %d car is in the %d of garage!\n ",a.num,order);
}
else {
Q.rear=(QueuePtr)malloc(sizeof(QNode));
Q.rear-> next=NULL;
Q.front-> next=Q.rear;
Q.rear-> num=a.num;
m++;
printf( "The %d car is in the %d of Queue!\n ",a.num,m);
}
break;
case 'D ':
while ((*(--S.top)).num!=a.num){
(*(VS.top)).num=(*(S.top)).num;
(*(VS.top)).time=(*(S.top)).time;
VS.top++;
}
money=(a.time-(*(S.top)).time)*x;
printf( "The %d car is out of %d of garage and the cost is %d!\n ",a.num,S.top-S.base+1,money);
while (VS.top!=VS.base){
(*(S.top)).num=(*(--VS.top)).num;
(*(S.top)).time=(*(VS.top)).time;
S.top++;


}
if (m!=0){
l=Q.front-> next;
(*(S.top)).num=l-> num;
(*(S.top)).time=a.time;
S.top++;
printf( "The %d car is in the %d of garage!\n ",l-> num,S.stacksize);
l=Q.front-> next;
Q.front-> next=Q.front-> next-> next;
free(l);
m--;
}
break;
default: printf( "The order is wrong!\n ");break;
}
}
printf( "\nThe program has finished!\n ");
}

热点排行