C语言实现简单停车场管理系统

时间:2021-05-20

本文实例为大家分享了C语言停车场管理系统的具体代码,供大家参考,具体内容如下

/***************************************************************************项目要求停车场管理问题描述:停车场是一个能放n辆车的狭长通道,只有一个大门,汽车按到达的先后次序停放。若车场满了,车要停在门外的便道上等候,一旦有车走,则便道上第一辆车进入。当停车场中的车离开时,由于通道窄,在它后面的车要先退出,待它走后再依次进入。汽车离开时按停放时间收费。基本功能要求:(1) 建立三个数据结构分别是:停放栈、让路栈、等候队列。(2) 输入数据模拟管理过程,数据(入或出,车号)。***************************************************************************/#include<stdio.h>#include<stdlib.h>#include<time.h> #define D (24*60*60) #define H (60*60) #define M (60)#define OK 1#define ERROR 0#define MAX_STACK_SIZE 10 typedef int StackData;typedef int QueueData;typedef int ElemType;typedef struct Node{ int No; int Timeinit; }Node;typedef struct QueueNode { struct Node data; struct QueueNode* next; } QueueNode;typedef struct LinkQueue { struct QueueNode *rear, *front;} LinkQueue; typedef struct SqStackNode { int top; int bottom; struct Node stack_array[MAX_STACK_SIZE+1] ;}SqStackNode ; //***************************************************************SqStackNode* InitStack() { SqStackNode *S=(SqStackNode *)malloc(sizeof(SqStackNode)); S->bottom=S->top=0; return (S);}int FullStack(SqStackNode *S) { return S->top==MAX_STACK_SIZE;}int pushStack(SqStackNode *S,Node data) { if(FullStack(S)) { return ERROR; } S->top++ ; (S->stack_array[S->top]).No=data.No ; (S->stack_array[S->top]).Timeinit=data.Timeinit; return OK; }int popStack(SqStackNode *S,Node *data) { if(S->top==0) { return ERROR; } (*data).No=(S->stack_array[S->top]).No; (*data).Timeinit=(S->stack_array[S->top]).Timeinit; S->top--; return OK; }int FinfStack(SqStackNode *S,Node data) { int i; if(S->top==0) { return ERROR; } for(i=1;i<=S->top;i++) { if(S->stack_array[i].No == data.No) { return OK; } } return ERROR; } //**************************************************** LinkQueue* InitQueue (void) { LinkQueue *Q=( LinkQueue * ) malloc( sizeof ( LinkQueue ) ); Q->rear=Q->front=NULL; return Q;} int QueueEmpty ( LinkQueue *Q ) { return Q->front == NULL;} int GetFrontQueue ( LinkQueue *Q, Node *data ) { if ( QueueEmpty (Q) ) return 0; (*data).No = (Q->front->data).Timeinit; return 1; }int EnQueue ( LinkQueue **Q, Node data) { QueueNode *p = ( QueueNode * ) malloc( sizeof ( QueueNode ) ); (p->data).No = data.No; (p->data).Timeinit = data.Timeinit; p->next = NULL; if ( (*Q)->front == NULL ) { (*Q)->front = (*Q)->rear = p; } else { (*Q)->rear = (*Q)->rear->next = p; } return 1;}int DeQueue ( LinkQueue **Q, Node *data) { if ( QueueEmpty (*Q) ) { return 0; } QueueNode *p = (*Q)->front; (*data).No = p->data.No; (*data).Timeinit = p->data.Timeinit; (*Q)->front = (*Q)->front->next; if ((*Q)->front == NULL) (*Q)->rear = NULL; free (p); return 1; }/*********************************************************/int now_time(void) { time_t t1; time(&t1); int time=t1%D; return time; } Parking(LinkQueue **Q,SqStackNode *S) { int i,time_now; Node data; printf("Input the Car No:\n"); scanf(" %d",&data.No); for(i=1;i<=S->top;i++) { if(S->stack_array[i].No == data.No) { printf("The Car is existed\n"); return ; } } EnQueue(Q,data); while(!QueueEmpty(*Q)) { if(FullStack(S)) { printf("Please Wait...\n"); break; } else { DeQueue(Q,&data); data.Timeinit=now_time(); pushStack(S,data); printf("Park Success\n"); } } return ;}leaving(SqStackNode *S,SqStackNode *B,LinkQueue **Q){ if(S->bottom == S->top) { printf("Parking is Empty:\n"); } else { Node data; int i,h,m,s; float charge; int time_now,parking_time; printf("Leaving No:\n"); scanf(" %d",&i); data.No=i; if(!FinfStack(S,data)) { printf("Do not find the car\n"); return ; } else { while(S->stack_array[S->top].No != i) { popStack(S,&data); pushStack(B,data); } popStack(S,&data); time_now=now_time(); parking_time=time_now-data.Timeinit; h = parking_time/H; parking_time = parking_time%H; m = parking_time/M; s = parking_time%M; charge = 6*h+0.1*(m+1); printf("The leaving car:%d Parking time:%d:%d:%d Charge($6/h):$%g\n",data.No,h,m,s,charge); while(B->bottom != B->top) { popStack(B,&data); pushStack(S,data); } while(!FullStack(S)&&(!QueueEmpty(*Q))) { DeQueue(Q,&data); data.Timeinit=now_time(); pushStack(S,data); } } }}situation(SqStackNode *S,LinkQueue **Q){ Node data; int i; int time_now,parking_time; int h,m,s; struct QueueNode *p; int wait_count=0; p=(*Q)->front; if(p == NULL) { printf("Waiting car :0\n"); } else { do { wait_count++; p=p->next; }while(p!=NULL); printf("Waiting car :%d\n",wait_count); } printf("Car No: "); for(i=1;i<=S->top;i++) { printf("%-10d",S->stack_array[i].No); if(S->stack_array[i].No == data.No) { return OK; } } printf("\nPark time:"); for(i=1;i<=S->top;i++) { time_now = now_time(); parking_time = time_now - S->stack_array[i].Timeinit; h = parking_time/H; parking_time = parking_time%H; m = parking_time/M; s = parking_time%M; printf("%02d:%02d:%02d ",h,m,s); } printf("\n"); } int main(){ int i; Node data; SqStackNode *park; SqStackNode *back; LinkQueue *wait; park=InitStack(); back=InitStack(); wait=InitQueue(); while(1) { system("clear\n"); printf("----------Welcome to our Car Parking----------\n"); printf(" 1.Parking \n"); printf(" 2.leaving \n"); printf(" 3.situation \n"); printf(" 4.exit \n"); scanf(" %d",&i); switch(i) { case 1: { system("clear\n"); Parking(&wait,park); setbuf(stdin,NULL); getchar(); break; } case 2: { leaving(park,back,&wait); setbuf(stdin,NULL); getchar(); break; } case 3: { system("clear\n"); situation(park,&wait); setbuf(stdin,NULL); getchar(); break; } case 4: { return 0; } default: { break; } } } return 0; }

更多学习资料请关注专题《管理系统开发》。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章