這篇文章主要介紹了C語言如何實現簡單停車場管理系統,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
本文實例為大家分享了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) /* 搜索棧內元素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;
}感謝你能夠認真閱讀完這篇文章,希望小編分享的“C語言如何實現簡單停車場管理系統”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關注億速云行業資訊頻道,更多相關知識等著你來學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。