C語(yǔ)言實(shí)現(xiàn)停車場(chǎng)管理系統(tǒng)
問題描述:停車場(chǎng)是一個(gè)能放n輛車的狹長(zhǎng)通道,只有一個(gè)大門,汽車按到達(dá)的先后次序停放。若車場(chǎng)滿了,車要停在門外的便道上等候,一旦有車走,則便道上第一輛車進(jìn)入。當(dāng)停車場(chǎng)中的車離開時(shí),由于通道窄,在它后面呢的車要先退出,待它走后再依次進(jìn)入。汽車離開時(shí)按停放時(shí)間收費(fèi)。
基本功能要求:
(1)建立三個(gè)數(shù)據(jù)結(jié)構(gòu)分別是:停放隊(duì)列、讓路棧、等候隊(duì)列。
(2)輸入數(shù)據(jù)模擬管理過程,數(shù)據(jù)(入或出,車號(hào))。
停車管理系統(tǒng)是C語(yǔ)言中隊(duì)列和棧比較簡(jiǎn)單的應(yīng)用,需要注意的是停車隊(duì)列、等候隊(duì)列、讓路棧結(jié)構(gòu)體的構(gòu)建。在寫代碼時(shí),出隊(duì)列入棧和出棧入隊(duì)列時(shí),指針容易出錯(cuò)而造成段錯(cuò)誤,應(yīng)當(dāng)注意。我所寫的代碼如下:
//定義結(jié)構(gòu)體
#include <stdio.h>
#include <stdlib.h>
#define F 0
#define T 1
#define MAX 3
typedef struct Node //數(shù)據(jù)
{
int number;
int time;
}Node;
typedef struct QueueNode //隊(duì)列結(jié)點(diǎn)
{
struct Node infom;
struct QueueNode * next;
}*QueueNode;
typedef struct LinkQueue //鏈隊(duì)列
{
struct QueueNode * front;
struct QueueNode * rear;
}LinkQueue;
typedef struct stack //棧結(jié)點(diǎn)
{
struct Node data;
struct stack *next;
}*StackNode;
typedef struct LinkStack //鏈棧
{
StackNode top;
int count;
}LinkStack;
//函數(shù)實(shí)現(xiàn)
void menu(LinkQueue *wait,LinkQueue *park,LinkStack *giveway,int num,int t);//菜單
int init(LinkQueue *wait,LinkQueue *park,LinkStack *giveway);//初始化
int linklength(LinkQueue q);//查看長(zhǎng)度
int enqueue(LinkQueue *q,int num,int t);//入隊(duì)列
int dequeue(LinkQueue *q,int *num,int *t);//出隊(duì)列
void park1(LinkQueue *wait,LinkQueue *park);//停車函數(shù)
int push(LinkStack *s,int num,int t);//入棧
int pop(LinkStack *s,int *num,int *t);//出棧
void leave2(LinkQueue *wait,LinkQueue *park,LinkStack *giveway,int num,int t);//離開函數(shù)
void view3(LinkQueue wait,LinkQueue park);//查看停車場(chǎng)狀態(tài)
int main()
{
LinkQueue wait;
LinkQueue park;
LinkStack giveway;
int num = 0;
int t = 0;
init(&wait,&park,&giveway);
menu(&wait,&park,&giveway,num,t);
return 0;
}
int init(LinkQueue *wait,LinkQueue *park,LinkStack *giveway)
{
QueueNode newnode1 = (QueueNode)malloc(sizeof(struct QueueNode));
if(NULL == newnode1)
{
return F;
}
newnode1->next = NULL;
wait->front = newnode1;
wait->rear = newnode1;
QueueNode newnode2 = (QueueNode)malloc(sizeof(struct QueueNode));
if(NULL == newnode2)
{
return F;
}
newnode2->next = NULL;
park->front = newnode2;
park->rear = newnode2;
giveway->top = NULL;
giveway->count = 0;
}
void menu(LinkQueue *wait,LinkQueue *park,LinkStack *giveway,int num,int t)
{
printf("**********Welcome to our Car Parking !**********\n");
printf("********** Please choose function **********\n");
printf("********** 1 : park. **********\n");
printf("********** 2 : leave. **********\n");
printf("********** 3 : view. **********\n");
printf("********** 4 : exit. **********\n");
int option;
scanf("%d",&option);
switch(option)
{
case 1:{
park1(wait,park);
printf("停車完成!\n");
menu(wait,park,giveway,num,t);
break;
}
case 2:{
leave2(wait,park,giveway,num,t);
menu(wait,park,giveway,num,t);
break;
}
case 3:{
view3(*wait,*park);
menu(wait,park,giveway,num,t);
break;
}
case 4:{
printf("********** 歡迎再次使用,謝謝! **********\n");
break;
}
default:{
printf("********** 請(qǐng)輸入正確的指令! **********\n");
menu(wait,park,giveway,num,t);
break;
}
}
}
int linklength(LinkQueue q)
{
int i = 0;
while(q.front != q.rear)
{
i++;
q.front = q.front->next;
}
return i;
}
int enqueue(LinkQueue *q,int num,int t)
{
QueueNode newnode = (QueueNode)malloc(sizeof(struct QueueNode));
if(NULL == newnode)
{
return F;
}
newnode->infom.number = num;
newnode->infom.time = t;
newnode->next = NULL;
q->rear->next = newnode;
q->rear = newnode;
return T;
}
int dequeue(LinkQueue *q,int *num,int *t)
{
if(q->front == q->rear)
{
printf("the queue is empty!\n");
return F;
}
*num = q->front->next->infom.number;
*t = q->front->next->infom.time;
QueueNode temp = q->front->next;
q->front->next = temp->next;
if(temp->next == NULL)
{
q->rear = q->front;
}
free(temp);
return T;
}
void park1(LinkQueue *wait,LinkQueue *park)
{
printf("請(qǐng)輸入車號(hào)和停車時(shí)間\n");
int num,t;
scanf("%d,%d",&num,&t);
if(linklength(*park) >= MAX)
{
printf("停車場(chǎng)已滿,進(jìn)入等待區(qū)!\n");
enqueue(wait,num,t);
}
else
{
enqueue(park,num,t);
}
}
int push(LinkStack *s,int num,int t)
{
StackNode newnode = (StackNode)malloc(sizeof(struct stack));
if(NULL == newnode)
{
return F;
}
newnode->data.number = num;
newnode->data.time = t;
newnode->next = s->top;
s->top = newnode;
s->count++;
return T;
}
int pop(LinkStack *s,int *num,int *t)
{
if(0 == s->count)
{
printf("the stack is empty !\n");
return F;
}
*num = s->top->data.number;
*t = s->top->data.time;
StackNode temp = s->top;
s->top = s->top->next;
free(temp);
s->count--;
return T;
}
void leave2(LinkQueue *wait,LinkQueue *park,LinkStack *giveway,int num,int t)
{
printf("請(qǐng)輸入要離開車的車號(hào)\n");
int leavenumber;
scanf("%d",&leavenumber);
int i = 0;
QueueNode head = park->front;
while(head != park->rear)
{
if(head->next->infom.number != leavenumber)
{
head = head->next;
i++;
}
else
break;
}
int j = 0;
if(i <= MAX-1)
{
while(j != i)
{
dequeue(park,&num,&t);
push(giveway,num,t);
j++;
}
dequeue(park,&num,&t);
}
else
{
printf("查無(wú)此車!\n");
}
while(giveway->top != NULL)
{
pop(giveway,&num,&t);
enqueue(park,num,t);
}
if(linklength(*wait) != 0)
{
dequeue(wait,&num,&t);
enqueue(park,num,t);
}
}
void view3(LinkQueue wait,LinkQueue park)
{
printf("******************** 目前停車場(chǎng)狀況 ********************\n");
printf("停車場(chǎng)共%d個(gè)車位,當(dāng)前停車場(chǎng)共有%d量車,等待區(qū)共有%d量車\n",
MAX,linklength(park),linklength(wait));
printf("**************************************************************\n");
printf("車 號(hào):");
QueueNode head1 = park.front;
QueueNode head2 = park.front;
while(head1 != park.rear)
{
printf("%d ",head1->next->infom.number);
head1 = head1->next;
}
printf("\n");
printf("停車時(shí)間:");
while(head2 != park.rear)
{
printf("%d ",head2->next->infom.time);
head2 = head2->next;
}
printf("\n");
}
更多學(xué)習(xí)資料請(qǐng)關(guān)注專題《管理系統(tǒng)開發(fā)》。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C語(yǔ)言實(shí)現(xiàn)停車場(chǎng)項(xiàng)目
- C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)易停車場(chǎng)管理系統(tǒng)
- C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單的停車場(chǎng)管理系統(tǒng)
- C語(yǔ)言實(shí)現(xiàn)游戲VIP停車場(chǎng)管理系統(tǒng)
- C語(yǔ)言源碼實(shí)現(xiàn)停車場(chǎng)管理系統(tǒng)
- C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單停車場(chǎng)管理系統(tǒng)
- C語(yǔ)言實(shí)現(xiàn)停車場(chǎng)管理
- C語(yǔ)言設(shè)計(jì)圖書登記系統(tǒng)與停車場(chǎng)管理系統(tǒng)的實(shí)例分享
- C語(yǔ)言課程設(shè)計(jì)之停車場(chǎng)管理問題
相關(guān)文章
用C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單掃雷小游戲
這篇文章主要為大家詳細(xì)介紹了用C語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07
C語(yǔ)言代碼實(shí)現(xiàn)簡(jiǎn)單2048游戲
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)2048游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
C語(yǔ)言fgetc和fputc函數(shù)用法詳解(以字符形式讀寫文件)
這篇文章主要介紹了C語(yǔ)言fgetc和fputc函數(shù)用法詳解(以字符形式讀寫文件),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01

