這個程序是利用棧和循環隊列實現的,自己得先處理好邏輯關系就好了。由于題目沒有要求,這個程序就沒加重復判斷,比如一輛車已經停在車位上或者便道上,再來一輛就判斷不了了。關于棧,就是先進后出的思想,隊列就是先進先出的思想。這個程序自己沒用鏈棧和鏈隊列做,因為感覺比較耗時。不過棧和隊列的運用大多數都是用數組,先掌握好數組的表示再用鏈表寫上手也很快。
**項目要求:**停車場是一個能放 n 輛車的狹長通道,只有一個大門,汽車按到達的先后次序停放。若車場滿了,車要在門外的便道上等候,一旦有車走,則便道上第一輛車進入。當停車場中的車離開時,由于通道窄,在它后面的車要先退出,待它走后依次進入。汽車離開時按停放時間收費。
基本功能要求:
1)建立三個數據結構分別是:停放隊列,讓路棧,等候隊列
2)輸入數據模擬管理過程,數據(入或出,車號)。
頭文件: PLot.h
//
// Created by PC-Saw on 2018/12/17.
//#ifndef __PLOT_H__
#define __PLOT_H__#define Price 1 // 單價可以自己定義n
#define MAX_STOP 10
#define MAX_PAVE 10#include <stdlib.h>
#include <stdio.h>
#include <windows.h>
#include <time.h> // 包含時間函數的頭文件
#include <string.h>// 汽車信息
typedef struct
{int timeIn; // 進入停車場時間int timeOut; // 離開停車場時間char plate[10];// 汽車牌照號碼,定義一個字符指針類型
}Car;// 停放棧(用于停放車輛)
typedef struct
{Car Stop[MAX_STOP]; // 用于停放車輛的棧int top; // 標記棧頂位置
}Stopping;// 等候隊列
typedef struct
{int count; // 用來指示隊中的數據個數Car Pave[MAX_PAVE]; // 等候停車的隊列int front, rear; // 標記隊頭和隊尾位置
}Pavement;// 讓路棧
typedef struct
{Car Help[MAX_STOP]; // 用于讓路的隊列int top; // 標記站定位置
}Buffer;Stopping s;
Pavement p;
Buffer b;
Car c;
char C[10];void stop_to_pave(); // 車停入便道
void car_come (); // 車停入停車位
void stop_to_buff(); // 車進入讓路棧
void car_leave (); // 車離開
void welcome (); // 主界面函數
void Display (); // 顯示車輛信息#endif //__PLOT_H__
源文件 PLot.c
//
// Created by PC-Saw on 2018/12/17.
//#include "PLot.h"void stop_to_pave() // 車停入便道
{// 判斷隊滿if (p.count > 0 && (p.front == (p.rear + 1) % MAX_PAVE)){printf ("便道已滿,請下次再來\n");}else{strcpy(p.Pave[p.rear].plate, C); // 車進入便道p.rear = (p.rear + 1) % MAX_PAVE; // 隊尾指示器加1p.count++; // 計數器加1printf ("牌照為%s的汽車停入便道上的%d的位置\n", C, p.rear);}
}void car_come() // 車停入停車位
{printf ("請輸入即將停車的車牌號:"); // 輸入車牌號scanf ("%s", &C);if (s.top >= MAX_STOP - 1) // 如果停車位已滿,停入便道{stop_to_pave(); // 停入便道}else{s.top++; // 停車位棧頂指針加1time_t t1;long int t = time(&t1); // 記錄進入停車場的時間char* t2;t2 = ctime (&t1); // 將當前時間轉換為字符串s.Stop[s.top].timeIn = t;strcpy(s.Stop[s.top].plate, C); // 將車牌號登記printf ("牌照為%s的汽車停入停車位的%d車位, 當前時間:%s\n", C, s.top + 1, t2);}return ;
}void stop_to_buff() // 車進入讓路棧
{// 停車位棧壓入臨時棧,為需要出棧的車輛讓出道while (s.top >= 0){if (0 == strcmp(s.Stop[s.top].plate, C)){break;}// 讓出的車進入讓路棧strcpy(b.Help[b.top++].plate, s.Stop[s.top].plate);printf ("牌照為%s的汽車暫時退出停車場\n", s.Stop[s.top--].plate);}// 如果停車位中的車都讓了道,說明停車位中無車輛需要出行if (s.top < 0){printf ("停車位上無此車消息\n");}else{printf ("牌照為%s的汽車從停車場開走\n", s.Stop[s.top].plate);time_t t1;long int t = time (&t1);c.timeOut = t; // 標記離開停車場的時間char* t2;t2 = ctime (&t1); // 獲取當前時間printf ("離開時間%s\n需付%ld元\n", t2, Price * (c.timeOut - s.Stop[s.top].timeIn));s.top--;}// 將讓路棧中的車輛信息壓入停車位棧while (b.top > 0){strcpy(s.Stop[++s.top].plate, b.Help[--b.top].plate);printf ("牌照為%s的汽車停回停車位%d車位\n", b.Help[b.top].plate, s.top);}// 從便道中 -> 停車位while (s.top < MAX_STOP-1){if (0 == p.count) // 判斷隊列是否為空{break;} // 不為空,將便道中優先級高的車停入停車位else{strcpy(s.Stop[++s.top].plate, p.Pave[p.front].plate);printf ("牌照為%s的汽車從便道中進入停車位的%d車位\n", p.Pave[p.front].plate, s.top);p.front = (p.front + 1) % MAX_PAVE;p.count--;}}
}void car_leave() // 車離開
{printf ("請輸入即將離開的車牌號:\n");scanf ("%s", &C);if (s.top < 0) // 判斷停車位是否有車輛信息{printf ("車位已空,無車輛信息!\n");}else{stop_to_buff();}
}void Display()
{int i = s.top;if (-1 == i){printf ("停車場為空\n");}time_t t1;long int t = time(&t1); // 標記顯示時的時間printf ("\t車牌號\t\t\t停放時間\t\t當前所需支付金額\n");while (i != -1){printf ("\t%s\t\t%d秒\t\t\t%d元\n", s.Stop[i].plate, t - s.Stop[i].timeIn, Price * (t - s.Stop[i].timeIn) / 10);i--;}
}void welcome()
{printf ("\t*******************目前停車場狀況***********************\n");printf ("\t停車場共有%d個車位,當前停車場共有%d輛車,等候區共有%d輛車\n", MAX_STOP, s.top+1, (p.rear + MAX_PAVE - p.front)% MAX_PAVE);printf ("\t********************************************************\n");printf ("\t---------------Welcome to our Car Parking---------------\n");printf ("\t* 1.Parking *\n");printf ("\t* 2.leaving *\n");printf ("\t* 3.situation *\n");printf ("\t* 4.exit *\n");printf ("\t--------------------------------------------------------\n");
}
主函數 main.c
/**********************************************************
問題描述:停車場是一個能放 n 輛車的狹長通道,只有一個大門,
汽車按到達的先后次序停放。若車場滿了,車要在門外的便道上等候
,一旦有車走,則便道上第一輛車進入。當停車場中的車離開時,由
于通道窄,在它后面的車要先退出,待它走后依次進入。汽車離開
時按停放時間收費。
基本功能要求:1)建立三個數據結構分別是:停放隊列,讓路棧,等候隊列2)輸入數據模擬管理過程,數據(入或出,車號)。
***********************************************************/
#include "PLot.h"int main()
{// 初始化s.top = -1;b.top = 0;p.rear = 0;p.count = 0;p.front = 0;while(1){//system("clear");welcome();int i, cho;scanf ("%d", &i);if (1 == i) car_come();if (2 == i) car_leave();if (3 == i) Display();if (4 == i) break;printf ("返回請輸入1\n");scanf ("%d", &cho);if (1 == cho){continue;}else{printf ("您的輸入有誤,請重新輸入\n");scanf ("%d", &cho);continue;}}return 0;
}