先上图片
源码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <windows.h>
#define D_Full 3
typedef struct Node
{
char Number[8];//车牌号
struct Node* pNext;
struct Node* pPre;
double Time;//进场时刻
int iposition;//车位号
}Car_Node;
int icount = 0;
int pcount = 0;
void Main_Menu(Car_Node* pstack, Car_Node* pQueue);//主页面
Car_Node* Stack();//申请空头
bool IsEmpty(Car_Node* pstack);//判断是否为空
bool IsFull();//判断是否为满
void Push(Car_Node* pstack, char Number[8], double Time);//入栈
void Pop(Car_Node* pstack, Car_Node* plook);//弹出头
Car_Node* LookByName(Car_Node* pstack, char Number[8]);//查找指定的位置
void LookToAllCar(Car_Node* pstack);//输出所有车位
//下面开始写等候区的函数
Car_Node* Queue();//申请队列空头
void PushToQueue(Car_Node* pQueue, char Number[8]);//队列入队,用尾插就行
bool IsEmptyToQueue(Car_Node* pQueue);//判断是否为空
Car_Node* PopToQueue(Car_Node* pQueue);//弹出队列头
void LookToAllWaitCar(Car_Node* pQueue);//输出所有等候区车位
void SaveToFile(Car_Node* pstack, Car_Node* pQueue);//保存信息进文件
void ReadStuFromFile(Car_Node* pstack, Car_Node* pQueue);//读取信息出文件
void SaveToFileByWait(Car_Node* pQueue);//保存等候区进文件
void ReadStuFromFileByWait(Car_Node* pQueue);//读取等候区信息出文件
void FindByName(Car_Node* pstack, Car_Node* pQueue);//根据车牌号查询
void FreeToStack(Car_Node* pstack);//释放栈
void FreeToQueue(Car_Node* pQueue);//释放队列
int main(void)
{
Car_Node* pQueue= Queue();
Car_Node* pstack= Stack();
ReadStuFromFile(pstack, pQueue);
Main_Menu(pstack, pQueue);
SaveToFile(pstack,pQueue);
system("pause");
return 0;
}
void Main_Menu(Car_Node* pstack, Car_Node* pQueue)
{
int flag = 1;
system("cls");
while (flag)
{
if (IsFull() == false && IsEmptyToQueue(pQueue) == false)
{
Car_Node* plookqueue = PopToQueue(pQueue);
printf("请输入等待区车牌号为%s车辆入场时间!\n",plookqueue->Number);
double ntime;
scanf("%lf", &ntime);
Push(pstack, plookqueue->Number, ntime);//入栈
printf("车牌号为%s已进入停车场\n", plookqueue->Number);
}
printf("*********************XMUT车位管理程序*********************\n");
printf("*********************系统操作指令如下*********************\n");
printf("*** 1.车辆入场 ***\n");
printf("*** 2.车辆出场 ***\n");
printf("*** 3.车场信息 ***\n");
printf("*** 4.等待信息 ***\n");
printf("*** 5.根据车牌号查询 ***\n");
printf("*** 6.清空停车场 ***\n");
printf("*** 7.清空等候区 ***\n");
printf("*** 8.退出并保存程序 ***\n");
printf("===================== 剩余车位:%d车位 =================\n", D_Full - icount);
printf("**********************************************************\n");
printf("\n");
printf("***********请输入操作指令!***********\n");
char ichoose[5] = {0};
char Name[8] = { 0 };
double iTime = 0;
scanf("%s", ichoose);
switch (atoi(ichoose))
{
case 1:
if ((IsFull()==false))
{
printf("请输入车牌:\n");
scanf("%s", Name);
Car_Node* plook = pstack->pNext;
int qlag = 1;
while (plook!=pstack)
{
if (strcmp(plook->Number, Name) == 0)
{
qlag = 0;
break;
}plook = plook->pNext;
}
if (qlag == 1)
{
printf("请输入进场时间:\n");
scanf("%lf", &iTime);
Push(pstack, Name, iTime);
}
else
{
printf("该车已经在%d车位内了!\n",plook->iposition);
}
}
else
{
//这里写入队
printf("请输入车牌:\n");
scanf("%s", Name);
Car_Node* plook = pstack->pNext;
int qlag = 1;
while (plook != pstack)
{
if (strcmp(plook->Number, Name) == 0)
{
qlag = 0;
break;
}plook = plook->pNext;
}
if (qlag == 1)
{
Car_Node* plook = pQueue->pNext;
int flag = 1;
while (plook != pQueue)
{
if (strcmp(plook->Number, Name) == 0)
{
flag = 0;
break;
}plook = plook->pNext;
}
if (flag == 1)
{
PushToQueue(pQueue, Name);
}
else
printf("车牌号%s已经在%d号等候区等待入场了!\n", Name, plook->iposition);
}//qlag==1
else
printf("该车已停在停车场!\n");
}
break;
case 2:
printf("请输入车牌号:\n");
char nnum[8] = {0};
scanf("%s", nnum);
Car_Node* plook=LookByName(pstack, nnum);
if (plook == NULL)
{int i =3;
while (1)
{
printf("没有这辆车!%d\n", i--);
if (i == 0)
break; Sleep(1000);
}
}
else
{
Pop(pstack,plook);
}
break;
case 3:
LookToAllCar(pstack);
break;
case 4:
//查询所有等待区的车辆
LookToAllWaitCar(pQueue);
break;
case 5:
FindByName(pstack,pQueue);
break;
case 6:
//清空停车场
FreeToStack( pstack);//释放栈
break;
case 7:
FreeToQueue( pQueue);//释放队列
break;
case 8:
flag = 0;
break;
default:
break;
}
}
}
Car_Node* Stack()
{
//申请节点,空头
Car_Node* p = (Car_Node*)malloc(sizeof(Car_Node));
strcpy(p->Number, "空头");
p->pNext = p;
p->pPre = p;
p->Time = -1;
p->iposition = -1;
return p;
}
bool IsEmpty(Car_Node* pstack)
{
if (pstack == NULL || pstack->pNext == pstack)
return true;
return false;
}
bool IsFull()
{
if (icount == D_Full)
return true;
return false;
}
void Push(Car_Node* pstack, char Number[8], double Time)
{
if (pstack == NULL)
return;
//申请节点
Car_Node* p = (Car_Node*)malloc(sizeof(Car_Node));
strcpy(p->Number, Number);
p->Time = Time; p->iposition = icount + 1;
p->pNext = NULL; p->pPre = NULL;
//节点链接
p->pPre = pstack;
p->pNext = pstack->pNext;
pstack->pNext->pPre = p;
pstack->pNext = p;
icount++;
printf("进场成功!\n");
}
void Pop(Car_Node* pstack, Car_Node* plook)
{
printf("请输入出场时间\n");
double inum = 0;
scanf("%lf", &inum);
double nsum = ((int)(inum)-(int)(plook->Time)) * 60;
double isum = ((int)(plook->Time * 100))%100;
double qsum = ((int)(inum * 100)) % 100;
double cha = qsum - isum;
double sum = (nsum+cha )* (5.0/60.0);
printf("\n");
printf("------------- 出场汽车信息表 ------------\n");
printf("--------------- 车牌号:%s --------------\n", plook->Number);
printf("--------------- 车位号:%d --------------\n", plook->iposition);
printf("-- 进场时刻 -- 出场时刻 -- 代缴费 --\n");
printf("-- %.2lf -- %.2lf -- %.2lf --\n", plook->Time,inum,sum);
printf("\n");
Car_Node* pend = pstack->pNext;
while (pend!=plook)
{
pend->iposition = pend->pNext->iposition;
pend = pend->pNext;
}
//记录节点
struct Node* p = plook;
//链接节点
p->pPre->pNext = p->pNext;
p->pNext->pPre = p->pPre;
//释放节点
free(p);
icount--;
}
Car_Node* LookByName(Car_Node* pstack, char Number[8])
{
Car_Node* p = pstack->pPre;
while (p != pstack)
{
if (strcmp(p->Number, Number) == 0)
{
return p;
break;
}
p = p->pPre;
}
return NULL;
}
void LookToAllCar(Car_Node* pstack)
{
if (IsEmpty(pstack) == true)//判断是否为空
{
printf("停车场为空!\n");
return;
}
Car_Node* p = pstack->pPre;
printf("场内汽车信息如下:\n");
printf("\n");
while (p!=pstack)
{
printf("停在%d号车位的 %6s 进场时刻:%.2lf点\n",p->iposition, p->Number, p->Time);
p = p->pPre;
}
printf("\n");
}
//等待区的队列
Car_Node* Queue()
{
Car_Node* p = (Car_Node*)malloc(sizeof(Car_Node));
if (p == NULL)
return NULL;
strcpy(p->Number, "队列头");
p->iposition = -1;
p->pNext = p;
p->pPre = p;
p->Time = -1;
return p;
}
void PushToQueue(Car_Node* pQueue, char Number[8])
{
if (pQueue == NULL)
return;
Car_Node* p = (Car_Node*)malloc(sizeof(Car_Node));
strcpy(p->Number, Number);
p->iposition = pcount + 1;
printf("车牌号%s已经进入%d号等待区\n", Number, p->iposition);
p->pPre = pQueue->pPre;
p->pNext = pQueue;
pQueue->pPre->pNext = p;
pQueue->pPre = p;
pcount++;
}
bool IsEmptyToQueue(Car_Node* pQueue)
{
if (pQueue==NULL||pQueue->pNext == pQueue)
return true;
return false;
}
Car_Node* PopToQueue(Car_Node* pQueue)
{
if (IsEmptyToQueue(pQueue) == true)
return NULL;
Car_Node* p = pQueue->pNext;
Car_Node* q = pQueue->pPre;
while (q!=p)
{
q->iposition--;
q = q->pPre;
}
pQueue->pNext = p->pNext;
p->pNext->pPre = p->pPre;
pcount--;
return p;
}
void LookToAllWaitCar(Car_Node* pQueue)
{
if (IsEmptyToQueue(pQueue) == true)
{
printf("等候区车辆为空!\n");
return;
}
Car_Node* p = pQueue->pNext;
printf("========================等候区车辆信息========================\n");
while (p!=pQueue)
{
printf("== 车牌号 ========================================== 排队号 ==\n");
printf("==%8s %d ==\n", p->Number, p->iposition);
p = p->pNext;
}
}
void SaveToFile(Car_Node* pstack, Car_Node* pQueue)//写入信息进文件
{
if (pstack == NULL)
return;
SaveToFileByWait(pQueue);
FILE* pFile = NULL;
//打开文件
pFile = fopen("pstack.dat", "wb+");
if (NULL == pFile)
{
printf("文件打开失败");
return;
}
//操做文件指针
Car_Node* p = pstack->pPre;
while (p!=pstack)
{
fprintf(pFile, "%s %lf\n", p->Number, p->Time);
p = p->pPre;
}
printf("保存成功!\n");
//关闭文件
fclose(pFile);//清空文件指针,写入文件
}
void ReadStuFromFile(Car_Node* pstack, Car_Node* pQueue)//读取文件中信息
{
ReadStuFromFileByWait(pQueue);
FILE* pFile = fopen("pstack.dat", "rb+");
if (NULL == pFile)
{
printf("文件打开失败\n");
return;
}
char arrName[8] = { 0 };
double Time = -1;
fgetc(pFile);
if (feof(pFile)) {
}
else {
rewind(pFile);
}
while (!feof(pFile))
{
fscanf(pFile, "%s %lf\n", arrName, &Time);
Push(pstack, arrName, Time);//入栈
}
fclose(pFile);
}
void SaveToFileByWait(Car_Node* pQueue)//保存等候区信息进文件
{
if (pQueue == NULL)
return;
char strbuff[20] = { 0 };
FILE* pFile = NULL;
//打开文件
pFile = fopen("pQueue.dat", "wb+");
if (NULL == pFile)
{
printf("文件打开失败");
return;
}
//操做文件指针
Car_Node* p = pQueue->pNext;
while (p != pQueue)
{
//复制
strcpy(strbuff, p->Number);
fwrite(strbuff, 1, strlen(strbuff), pFile);//
fwrite("\r\n", 1, strlen("\r\n"), pFile);//
p = p->pNext;
}
printf("保存成功!\n");
//关闭文件
fclose(pFile);//清空文件指针,写入文件
}
void ReadStuFromFileByWait(Car_Node* pQueue)//读取等候区文件
{
FILE* pFile = fopen("pQueue.dat", "rb+");
if (NULL == pFile)
{
printf("文件打开失败\n");
return;
}
char strbuff[20] = { 0 };
char arrName[8] = { 0 };
while (fgets(strbuff, 20, pFile) != NULL)
{
int i = 0;
for (i = 0; strbuff[i] != '\r'; i++)
{
arrName[i] = strbuff[i];
}
PushToQueue(pQueue, arrName);
}
fclose(pFile);
}
void FindByName(Car_Node* pstack, Car_Node* pQueue)
{
printf("请输入车牌号:\n");
char name[10];
scanf("%s", name);
Car_Node* plook = pstack->pNext;
while (plook!=pstack)
{
if (strcmp(plook->Number, name) == 0)
{
printf("车牌号%2s,停在停车场的%2d号位置,进场时间%.2lf\n", plook->Number, plook->iposition, plook->Time);
return;
}
plook = plook->pNext;
}
Car_Node* nlook = pQueue->pNext;
while (nlook != pQueue)
{
if (strcmp(nlook->Number, name) == 0)
{
printf("车牌号%2s,停在停车场的%2d号位置\n", nlook->Number, nlook->iposition);
return;
}
nlook = nlook->pNext;
}
printf("没找到这辆车!\n");
}
void FreeToStack(Car_Node* pstack)//释放栈
{
if (pstack->pNext ==pstack)
return;
Car_Node* plook = pstack->pNext;
while (plook != pstack)
{
Car_Node* p = plook;
plook = plook->pNext;
free(p);
icount--;
}
pstack->pNext = pstack;
pstack->pPre = pstack;
printf("清空成功!\n");
}
void FreeToQueue(Car_Node* pQueue)//释放队列
{
if (pQueue->pNext ==pQueue)
return;
Car_Node* plook = pQueue->pNext;
while (plook != pQueue)
{
Car_Node* p = plook;
plook = plook->pNext;
free(p);
pcount--;
}
pQueue->pNext = pQueue;
pQueue->pPre = pQueue;
printf("清空成功!\n");
}