我想使用堆栈的数组实现模拟一个停车场。 使用3个基本功能:
推-返回指向struct的指针。
弹出-将进入时间返回到停车场并卸下汽车。
PopByNum-根据汽车ID移除汽车并返回金额 钱的汽车必须根据在停车场的时间支付。 (这样做时,我只能使用弹出和推入)。
和main将包含一个while循环,该循环将模拟从08:00到12:00的一天的工作,而该循环的每个条目都是10分钟。 每10分钟就有一辆汽车进来。 每20分钟就有一辆汽车熄灭。
在循环中的某个点,停车场应该已经满了。我想算一下我拒绝的汽车数量,并节省我拒绝的第一辆汽车的时间。
当前是我的主要问题,我的停车场从来没有满员。我认为问题出在“ PopByNum”功能上,我找不到它。
也许这是一个重载,但是我认为只显示一个函数而不显示其他函数是愚蠢的,我也知道我的代码可能还有很多其他问题,但目前我主要关注的是只是想弄清楚为什么我的停车场永远都没空。
基本上在“ PopByNum”中,我尝试执行以下操作:
退还要支付的款项。
typedef struct TIME
{
int hours;
int minutes;
} Time;
typedef struct CAR
{
int id;
Time* entery_time;
} Car;
typedef struct PARKING_LOT
{
float rate_hour;
int size;
Car *stack;
int top;
} Parking_Lot;
int PLisFull(Parking_Lot* PL)
{
return PL->top == PL->size - 1;
}
int PLisEmpty(Parking_Lot* PL)
{
return PL->top == - 1;
}
Parking_Lot* PushCar(Parking_Lot* PL, int id, Time *time)
{
if (PLisFull(PL))
return PL;
Car* tmp = (Car*)malloc(sizeof(Car));
tmp->id = id;
tmp->entery_time = time;
PL->top += 1;
PL->stack[PL->top] = *tmp;
return PL;
}
Time* PopCar(Parking_Lot* PL)
{
if (PLisEmpty(PL))
return 0;
Car* tmp = (Car*)malloc(sizeof(Car));
Time *time = (Time*)malloc(sizeof(Time));
tmp[0] = PL->stack[PL->top];
time = tmp->entery_time;
PL->top -= 1;
return time;
}
float popByCarNum(Parking_Lot* PL, Time* CurrentTime, int CarID)
{
if (PLisEmpty(PL))
return 0;
int counter = 0;
for (int i = PL->top; i >= 0; i--)
{
if (PL->stack[i].id == CarID)
counter = i;
}
Parking_Lot *tmp = 0;
tmp = CreateParkingLot(tmp, PL->rate_hour,(PL->top - counter));
int id;
Time *time;
int i = PL->top;
for ( ; i > counter ; i--)
{
id = PL->stack[PL->top].id;
time = PopCar(PL);
tmp = PushCar(tmp, id, time);
}
Time* entery = 0;
entery = PopCar(PL);
float sum = CalculateRate(entery , CurrentTime, PL->rate_hour);
for (int i = (PL->top - counter); i>= 0 ; i--)
{
id = tmp->stack[tmp->top].id;
time = PopCar(tmp);
PL = PushCar(tmp, id, time);
}
return sum;
}
void main()
{
int SIZE = 3;
float rate = 30;
int timer = 800;
Time* timeOfday = timeToStruct(timer);
Time* pressure_time = timeToStruct(0);
Parking_Lot* PL = 0;
PL = CreateParkingLot(PL, rate, SIZE);
int counter = 0;
int rejected_cars = 0;
int num = 0;
int num1 = 0;
float sum = 0;
while (timeOfday->hours <= 12)
{
Time* currentTime = timeToStruct(0);
timeOfday->minutes += 10;
if (timeOfday->minutes >= 60)
{
timeOfday->hours += 1;
timeOfday->minutes -= 60;
}
if (1)
{
if (PL->top == (PL->size -1))
{
rejected_cars++;
if ((pressure_time->hours == 0) && (pressure_time->minutes == 0))
{
pressure_time->hours = timeOfday->hours;
pressure_time->minutes = timeOfday->minutes;
}
}
currentTime->hours = timeOfday->hours;
currentTime->minutes = timeOfday->minutes;
srand(time(NULL));
num1 = rand() % 9000 + 1000;
PL = PushCar(PL, num1 , currentTime);
}
if ((counter > 0) && (counter % 2 == 0))
{
srand(time(NULL));
num = rand() % (PL->top + 1);
sum += popByCarNum(PL, timeOfday, PL->stack[num].id);
}
counter++;
}
printf("The profit of the day is :%f\nThe total of rejected cars is :%d\nThe pressure start at :%0.4\n", sum, rejected_cars, TimeRepresent(pressure_time));
system("pause");
}