(代码很乱,也没什么注释,调试信息也没删,真心不是给别人看的东西,等标题上的草稿两字没了再看也不迟。)(注:标题带有“草稿”两字的,质量普遍很低,亲们,就不要浪费时间在上面吧)
题目:
停车场管理
问题描述:停车场是一个能放n辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要停在门外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面的车要先退出,待它走后在依次进入。汽车离开时按停放时间收费。
基本功能要求:
(1) 建立三个数据结构分别是:停放队列、让路栈、等候队列。
(2) 输入数据模拟管理过程,数据(入或出,车号)。
因为觉得不是很难,做起来挺无聊的,就搞了个界面(字符显示,把屏分为了三个功能区,分别显示即时停车场的占用信息、车辆信息、以及命令输入窗口),能用类似Linux Shell的命令行方式来操作。
结果有点弄巧成拙了,有个很明显的BUG(应该是“find_car_out”函数的哪里错了,改了半天也没改出来),大家就不要细看了罢。
注意:本段程序只能在windows下运行。
parking_lot.h
#ifndef PARKING_LOT_H_INCLUDED
#define PARKING_LOT_H_INCLUDED
#include <stdio.h>
#include <conio.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
#include <time.h>
#define park_lot_size 35
int RANDOM_SIZE=20;
inline void gotoxy(int y, int x)
{
COORD p;
p.X = x;
p.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), p);
}
void whereXY(int *x,int *y)
{
HANDLE h1;
CONSOLE_SCREEN_BUFFER_INFO scrInfo;
h1=GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(h1,&scrInfo);
*x=scrInfo.dwCursorPosition.X;
*y=scrInfo.dwCursorPosition.Y;
}
#endif // PARKING_LOT_H_INCLUDED
car.h
#ifndef CAR_H_INCLUDED
#define CAR_H_INCLUDED
typedef struct _car
{
char *license_plate;
time_t arrive_time;
time_t leave_time;
}st_car, *car;
typedef struct _car StackData;
typedef struct _car QueueData;
void car_print(car c);
car car_creat(int i);
#endif // CAR_H_INCLUDED
car.c
#include "car.h"
extern int RANDOM_SIZE;
char* car_license_plate_random(time_t t,int i)
{
char place[31][2] = {"京","津","沪","渝","冀"
,"豫","云","辽","黑","湘"
,"皖","鲁","苏","赣","浙"
,"粤","鄂","桂","甘","晋"
,"蒙","陕","吉","闽","贵"
,"青","藏","川","宁","新","琼"};
char *plate = (char*)malloc(sizeof(char) * 9);
while (plate == NULL)
{
plate = (char*)malloc(sizeof(char) * 9);
}
srand(t + i);
int random = rand();
plate[0] = place[random%31][0];
plate[1] = place[random%31][1];
plate[2] = random % 26 + 'A';
plate[3] = random % 10 + '0';
random /= 10;
plate[4] = random % 10 + '0';
random /= 10;
plate[5] = random % 10 + '0';
random /= 10;
plate[6] = random % 10 + '0';
random /= 10;
plate[7] = random % 10 + '0';
plate[8] = '\0';
return plate;
}
void car_print(car c)
{
struct tm *arrive;
struct tm *leave;
arrive = localtime(&c->arrive_time);
int hour = arrive->tm_hour;
int min = arrive->tm_min;
int sec = arrive->tm_sec;
leave = localtime(&c->leave_time);
// gotoxy(line,0);
printf("%s %02d:%02d:%02d %02d:%02d:%02d %2.2f"
, c->license_plate
// , arrive->tm_hour, arrive->tm_min, arrive->tm_sec
,hour,min,sec
, leave->tm_hour, leave->tm_min, leave->tm_sec
, (c->leave_time-c->arrive_time)*0.05);
return;
}
car car_creat(int i)
{
int tmp = i;
time_t t;
// struct tm *tm_t;
car pcar;
pcar = (car)malloc(sizeof(st_car));
while (pcar == NULL)
{
pcar = (car)malloc(sizeof(st_car));
}
time(&t);
// tm_t = localtime(&t);
srand((unsigned int)i);
while(tmp--)rand();
pcar->arrive_time = t;
pcar->leave_time = t + rand()%10 + 1;
pcar->license_plate = car_license_plate_random(t,i);
return pcar;
}
void printstar(StackData *x)
{
printf("*");
}
link_stack.h
#ifndef LINK_STACK_H_INCLUDED
#define LINK_STACK_H_INCLUDED
typedef struct st_StackNode StackNode;
typedef StackData *stackdata;
struct st_StackNode
{
stackdata data;
StackNode *next;
};
typedef struct _stack
{
StackNode *bottom;
StackNode *top;
}stack;
void stack_init(stack *s);
#define isempty(s) ((s)->bottom == (s)->top)
int push(stack *s, stackdata data);
int pop(stack *s, stackdata *data);
void stack_print(stack *s, void (*print)(StackData *),int line,int startnum);
void stack_star_print(stack *s);
void stack_star_print2(int length);
#endif // LINK_STACK_H_INCLUDED
link_s