本文最后更新于571 天前,其中的信息可能已经过时,如有错误请发送邮件到1986413837@qq.com
我的评价是AI替代不了人类,只要要求多了,AI就变得很诡异
要求很复杂:
设停车场是一个可停放n 辆汽车的狭长通道,且只有一个大门可供汽车进出。汽车在停车场内按车辆达到时间的先后顺序依次由北向南排列(大门在最南端,最先达到的第一辆车停放在车场的最北端),若车场内已停满n辆汽车,则后来的汽车只能在门外的便道上等候,一旦有车开走,则排在便道上的第一辆车即可开入;当停车场内某辆车要离开时,在它之后进入的车辆必须先退车车场为它让路,待赶辆车开出大门外,其它车辆在按原次序进入车场,每辆停放在车场的车在它离开停车场时必须按它停留的时间长短叫纳费用。试为停车场编制按上述要求进行管理的模拟程序
实现思路:
基本顺序栈作为停车场 限制停车数为3
链队列作为便道 放置暂时无法进入的车辆
建立一个临时栈 放置因前面车辆退出而需临时退出的车辆
下面是实现代码 我只能说chat废物一个
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PARKING 3
#define MAX_TEMP 5
#define FEE_PER_HOUR 10
typedef struct Car
{
char license[20];
int arrivalTime;
} Car;
typedef struct Stack
{
Car cars[MAX_PARKING];
int top;
} Stack;
typedef struct TempStack
{
Car cars[MAX_TEMP];
int top;
} TempStack;
typedef struct Node
{
Car car;
Node* next;
} Node;
typedef struct Queue
{
Node* front;
Node* rear;
} Queue;
void initStack(Stack* s);
void initTempStack(TempStack* ts);
void initQueue(Queue* q);
int isFull(Stack* s);
int isEmpty(Stack* s);
void push(Stack* s, Car car);
Car pop(Stack* s);
int isTempFull(TempStack* ts);
int isTempEmpty(TempStack* ts);
void pushTemp(TempStack* ts, Car car);
Car popTemp(TempStack* ts);
int isQueueEmpty(Queue* q);
void enqueue(Queue* q, Car car);
Car dequeue(Queue* q);
int calculateFee(int parkedTime);
int main()
{
Stack parkingLot;
TempStack tempStack;
Queue waitingQueue;
initStack(&parkingLot);
initTempStack(&tempStack);
initQueue(&waitingQueue);
char command[10];
Car car;
while (1)
{
printf("请输入操作 (到达A/离去D) 或 '退出E': ");
scanf("%s", command);
if (strcmp(command, "E") == 0)
{
break;
}
printf("请输入车牌号: ");
scanf("%s", car.license);
printf("请输入时间 (分钟): ");
scanf("%d", &car.arrivalTime);
if (strcmp(command, "A") == 0)
{
if (isFull(&parkingLot))
{
enqueue(&waitingQueue, car);
printf("车辆 %s 进入便道,等待入场\n", car.license);
}
else
{
push(&parkingLot, car);
}
}
else if (strcmp(command, "D") == 0)
{
Car leavingCar = { "", 0 };
int found = 0;
for (int i = parkingLot.top; i >= 0; --i)
{
if (strcmp(parkingLot.cars[i].license, car.license) == 0)
{
leavingCar = parkingLot.cars[i];
found = 1;
for (int j = parkingLot.top; j > i; --j)
{
Car tempCar = pop(&parkingLot);
pushTemp(&tempStack, tempCar);
}
parkingLot.top--;
break;
}
}
if (found)
{
int parkedDuration = car.arrivalTime - leavingCar.arrivalTime;
int fee = calculateFee(parkedDuration);
printf("车辆 %s 离开,停留时间: %d 分钟,应交纳费用: %d 元。\n", leavingCar.license, parkedDuration, fee);
while (!isTempEmpty(&tempStack))
{
Car tempCar = popTemp(&tempStack);
push(&parkingLot, tempCar);
}
if (!isQueueEmpty(&waitingQueue))
{
Car nextCar = dequeue(&waitingQueue);
push(&parkingLot, nextCar);
}
}
else
{
printf("未找到车辆 %s。\n", car.license);
}
}
else
{
printf("无效操作,请重新输入。\n");
}
}
return 0;
}
void initStack(Stack* s)
{
s->top = -1;
}
void initTempStack(TempStack* ts)
{
ts->top = -1;
}
void initQueue(Queue* q)
{
q->front = q->rear = NULL;
}
int isFull(Stack* s)
{
return s->top == MAX_PARKING - 1;
}
int isEmpty(Stack* s)
{
return s->top == -1;
}
void push(Stack* s, Car car)
{
if (!isFull(s))
{
s->cars[++s->top] = car;
printf("车辆 %s 进入停车场,位置:%d\n", car.license, s->top + 1);
}
else
{
printf("停车场已满,车辆 %s 无法进入。\n", car.license);
}
}
Car pop(Stack* s)
{
if (!isEmpty(s))
{
return s->cars[s->top--];
}
Car emptyCar = { "", 0 };
return emptyCar;
}
int isTempFull(TempStack* ts)
{
return ts->top == MAX_TEMP - 1;
}
int isTempEmpty(TempStack* ts)
{
return ts->top == -1;
}
void pushTemp(TempStack* ts, Car car)
{
if (!isTempFull(ts))
{
ts->cars[++ts->top] = car;
printf("车辆 %s 暂时退到临时栈。\n", car.license);
}
else
{
printf("临时栈已满,车辆 %s 无法退到临时栈。\n", car.license);
}
}
Car popTemp(TempStack* ts)
{
if (!isTempEmpty(ts))
{
return ts->cars[ts->top--];
}
Car emptyCar = { "", 0 };
return emptyCar;
}
int isQueueEmpty(Queue* q)
{
return q->front == NULL;
}
void enqueue(Queue* q, Car car)
{
Node* newNode = (Node*)malloc(sizeof(Node));
newNode->car = car;
newNode->next = NULL;
if (isQueueEmpty(q))
{
q->front = q->rear = newNode;
}
else
{
q->rear->next = newNode;
q->rear = newNode;
}
}
Car dequeue(Queue* q)
{
if (!isQueueEmpty(q))
{
Node* temp = q->front;
Car car = temp->car;
q->front = q->front->next;
if (q->front == NULL)
{
q->rear = NULL;
}
free(temp);
return car;
}
Car emptyCar = { "", 0 };
return emptyCar;
}
int calculateFee(int parkedTime)
{
return (parkedTime / 60 + (parkedTime % 60 != 0 ? 1 : 0)) * FEE_PER_HOUR;
}