版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、. 數(shù)據(jù)結(jié)構(gòu)項目課程報告 一、 問題描述及分析題目要求: 設(shè)停車場內(nèi)只有一個可停放n輛汽車的狹長通道,且只有一個大門可供汽車進出。汽車在停車場內(nèi)按車輛到達(dá)時間的先后順序,依次由北向南排列(大門在最南端,最先到達(dá)的第一輛車停放在車場的最北端),若車場內(nèi)已停滿n輛汽車,則后來的汽車只能在門外的便道上等候,一旦有車開走,則排在便道上的第一輛車即可開入;當(dāng)停車場內(nèi)某輛車要離開時,在它之后開入的車輛必須先退出車場為它讓路,待該車輛開出大門外,其他車輛再按原次序進入車場,每輛車停放在車場的車在它離開停車場時必須按它停留的時間長短交納費用。試為停車場編制按上述要求進行管理的模擬程序。分析:由題目只有一個大門
2、進出并且“先到后處理”可知,應(yīng)使構(gòu)造棧模擬停車場,進入離開停車場即入棧出棧的過程;而便道等候的汽車符合“先到先處理”原則 ,應(yīng)構(gòu)造線性隊列模擬;構(gòu)造存儲結(jié)構(gòu),存放車輛信息,車牌號,到達(dá)離開時間;通過time.h頭文件中的函數(shù)獲取汽車到來以及離開時間,利用時間差計算價錢。主要模塊如下圖:停車場管理系統(tǒng)車輛到達(dá)停車場車輛進入停車場車輛離開停車場車輛停留在便道車輛進入停車場輸出車輛的進入信息記錄進入的時間位置輸出車輛的離開信息記錄車輛離開的時間位置輸出應(yīng)繳納的費用便道的車輛進入停車場二、 功能模塊及數(shù)據(jù)結(jié)構(gòu)描述頭文件、宏定義以及存儲結(jié)構(gòu)結(jié)構(gòu)體定義相關(guān)類型的鏈表空間有順序棧的定義、鏈隊、車輛信息結(jié)構(gòu)體
3、、時間計算結(jié)構(gòu)體、車輛位置字符型結(jié)構(gòu)體數(shù)組和循環(huán)單鏈表存儲下一個地址空間。#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h>#include <time.h>#define OK 1#define ERROR 0typedef struct time1char times100;long time2;time1;typedef struct nodechar num10;time1 reach;time1 leave;Car;type
4、def struct StackCar *stack100;int top;SqStack;typedef struct carCar *data;struct car *next;QNode;typedef struct NodeQNode *front;QNode *rear;LinkQueue;主函數(shù)void main()SqStack In, Out;LinkQueue Wait;int ch;int MAX;float price;printf("請輸入停車場的容量:");scanf("%d", &MAX);printf("請
5、輸入停車場的收費標(biāo)準(zhǔn)(元 / 小時) :");scanf("%f", &price);InitStack(&In, MAX);InitStack(&Out, MAX);InitQueue(&Wait);while (1)system("color 0A");printf(" = = = = 歡迎使用停車場管理系統(tǒng) = = = = = = = = = = n");printf("輸入的停車場容量為%d位,費用為%2.1f元 / 小時, 請按任意鍵進入系統(tǒng)", MAX, pri
6、ce);getchar();system("CLS");printf(" = = = = 歡迎使用停車場管理系統(tǒng) = = = = = = = = = n");printf("您輸入的停車場容量為%d位,費用為%2.1f元 / 小時。n", MAX, price);printf("n = = = = = = = = = = = = = n");printf("(1)車輛到達(dá)n(2)車輛離開n(3)停車場信息n(4)退出系統(tǒng)n請選擇n");printf("n = = = = = = = =
7、 = = = = = = n");while (1)printf("請輸入操作的序號");scanf("%d", &ch);switch (ch)case 1:arrive(&In, &Wait, MAX, price);break;case 2:leave(&In, &Out, &Wait, MAX, price);break;case 3:info(In, Wait);break;case 4: system("CLS"); printf("n = = = = =
8、 = = = = = = = = = n"); printf(" 謝謝使用!n"); printf("n = = = = = = = = = = = = = = = = = = = = = n"); exit(0);default:printf("n按鍵無效,請重新按鍵選擇");getchar();system("CLS");printf(" = = = = = 歡迎使用停車場管理系統(tǒng) = = = = = n");printf("您輸入的停車場容量為%d位,費用為%2.1f元
9、 / 小時。n", MAX, price);printf("n = = = = = = = = = = = = = = = = = = = = n");printf("(1)車輛到達(dá)n(2)車輛離開n(3)停車場信息n(4)退出系統(tǒng)n請選擇n");printf("n = = = = = = = = = = = = = = = = = = = = = n");費用計算函數(shù),通過時間差計算費用void print(Car *p, int room, int MAX, float price)time_t rawtime; str
10、uct tm * timeinfo; long A1, A2, B1, B2;time ( &rawtime );timeinfo = localtime ( &rawtime );p->leave.time2=rawtime;printf("n車離開的時間為:%s ",asctime (timeinfo);strcpy(p->leave.times,asctime (timeinfo);printf("n車牌號碼:");puts(p->num);printf("n車到達(dá)的時間是: %s ", p-&
11、gt;reach.times);printf("n車離開的時間是: %s ", p->leave.times);A1=p->leave.time2;A2=p->reach.time2;B1=p->leave.time2-p->reach.time2;if(B1<=3600&&B1>=0)printf("n費用為: %2.1f元", price);elseprintf("n費用為: %2.1f元", B1/3600*price);free(p);/消費計算函數(shù)車輛到達(dá)函數(shù),獲取車
12、輛信息,判斷是否有車位,進行入?;蛉腙牪僮鱥nt arrive(SqStack *In, LinkQueue *W, int MAX, float price)Car *p;QNode *t;time_t rawtime; struct tm * timeinfo; /time ( &rawtime ); / 獲取時間,以秒計,從1970年1月一日起算,存于rawtime/timeinfo = localtime ( &rawtime ); /轉(zhuǎn)為當(dāng)?shù)貢r間,tm 時間結(jié)構(gòu) /printf ( "當(dāng)前系統(tǒng)時間: %s", asctime (timeinfo)
13、);/asctime (timeinfo); / 轉(zhuǎn)為標(biāo)準(zhǔn)ASCII時間格式:p = (Car *)malloc(sizeof(Car);printf(" = = = = = = = = = = = 歡迎使用停車場管理系統(tǒng) = = = = = = = = = = = n");printf("您輸入的停車場容量為%d位,費用為%2.1f元 / 小時。n", MAX, price);printf("停車場還有%d停車位, 車可先進入便道等待", MAX - In->top);printf("n請輸入車牌號碼(例:警A103
14、5) :");getchar();scanf("%s", p->num);getchar();if (In->top < MAX)time ( &rawtime );p->reach.time2=rawtime;timeinfo = localtime ( &rawtime );In->top+;printf("停車的位置:%d號停車位。", In->top);printf("n車到達(dá)的時間為%s: ",asctime (timeinfo);strcpy(p->rea
15、ch.times,asctime (timeinfo);In->stackIn->top = p;printf("請按任意鍵返回n");printf("n = = = = = = = = = = = = = = = = = = = = = = = n");getchar();return OK;elseprintf(" = = = = = 歡迎使用停車場管理系統(tǒng) = = = = = = = = = n");printf("您輸入的停車場容量為%d位,費用為%2.1f元 / 小時。n", MAX, pri
16、ce);printf("n = = = = = = = = = = = = = = = = = = = = = n");printf("停車位已滿,該車須在便道等待!");t = (QNode *)malloc(sizeof(QNode);t->data = p;t->next = NULL;W->rear->next = t;W->rear = t;printf("n請按任意鍵返回");getchar();return OK;getchar();車輛離開函數(shù),如果是最前面的車離開,后面的車必須都進行出棧
17、操作進入到另一個棧中讓需要離開的車離開后再重新進入;如果便道有車那么便道的前面的車即先入隊的車進行出隊入棧void leave(SqStack *In, SqStack *Out, LinkQueue *W, int MAX, float price)int place;Car *p, *t;QNode *q;time_t rawtime;struct tm * timeinfo;system("CLS");if (In->top>0)while (1)printf(" = = = = 歡迎使用停車場管理系統(tǒng) = = = = = = = = = n&q
18、uot;);printf("您輸入的停車場容量為%d位,費用為%2.1f元 / 小時。n", MAX, price);printf("n = = = = = = = = = = = = = = = = = = = = = = = = = = n");printf("請輸入車在停車場的位置(1 - %d):", In->top);scanf("%d", &place);if (place >= 1 && place <= In->top) break;while (In
19、->top>place)Out->top+;Out->stackOut->top = In->stackIn->top;In->stackIn->top = NULL;In->top-;p = In->stackIn->top;In->stackIn->top = NULL;In->top-;while (Out->top >= 1)In->top+;In->stackIn->top = Out->stackOut->top;Out->stackOut-&g
20、t;top = NULL;Out->top-;print(p, place, MAX, price);if (W->front != W->rear) && In->top<MAX)time ( &rawtime );timeinfo = localtime ( &rawtime );q = W->front->next;t = q->data;In->top+;printf("n便道的%s號車進入車場第%d號停車位。", t->num, In->top);printf(&qu
21、ot;n車到達(dá)的時間: %s ",asctime (timeinfo);t->reach.time2=rawtime;strcpy(t->reach.times,asctime (timeinfo);W->front->next = q->next;if (q = W->rear) W->rear = W->front;In->stackIn->top = t;free(q);elseprintf("n = = = = = = = = = = = = = = = = = = = = = = = = = n"
22、;);printf("停車場里沒有車n");printf("n請按任意鍵返回");printf("nn = = = = = = = = = = = = = = = = = = = = = = = = = = n");getchar();getchar();停車場信息函數(shù),遍歷隊列和棧能知道便道中和停車場中車輛信息void info1(SqStack *S)int i;if (S->top>0)printf("n 停車場n");for (i = 1; i <= S->top; i+)print
23、f("n 位置: n %dn", i);printf(" 到達(dá)時間:n%s 車牌號: n %sn", S->stacki->reach.times,S->stacki->num);elseprintf("n = = = = = = = = = = = = = = = = = = = = = n");printf("停車場里沒有車");void info2(LinkQueue *W)QNode *p;p = W->front->next;if (W->front != W-&
24、gt;rear)printf("n便道中車輛的號碼為:n");while (p != NULL)printf("%sn", p->data->num);p = p->next;else printf("n便道里沒有車n");printf("n請按任意鍵返回n");printf("nn = = = = = = = = = = = = = = = = = = = = = n");getchar();void info(SqStack S, LinkQueue W)system(&q
25、uot;CLS");info1(&S);info2(&W);getchar();隊列和棧的初始化void InitStack(SqStack *s, int MAX)int i;s->top = 0;for (i = 0; i <= MAX; i+)s->stacks->top = NULL;int InitQueue(LinkQueue *Q)Q->front = (QNode *)malloc(sizeof(QNode);if (Q->front != NULL)Q->front->next = NULL;Q->
26、;rear = Q->front;return OK;else return ERROR;/隊列和棧初始函數(shù)三、 主要算法流程描述及部分核心算法流程描述1.車輛到達(dá)停車場其中車輛到達(dá)時需要用戶輸入車輛信息,接著要判斷棧是否已滿,如當(dāng)前棧未滿,則進行入棧操作,即車輛進入停車場;如果棧已滿,則車輛必須進入便道等待。2. 車輛進入停車場車輛進入,則進行入棧操作,即將車輛信息結(jié)點進行入棧操作。3. 車輛離開停車場車輛的離開,則需要另設(shè)一個棧,給離去的汽車讓路而從停車場退出來的汽車臨時停放,也用順序棧實現(xiàn),車輛離開后需檢查便道內(nèi)是否有車輛等待,若有等待車輛則進行便道內(nèi)的車輛進入停車場的操作,即將車
27、輛信息結(jié)點進行入棧操作,輸入當(dāng)前時間后開始計費,最后進行出隊操作,表示車輛已離開便道。核心算法車輛到達(dá)停車場獲取車輛到達(dá)時間是車輛進入停車場開 始記錄車輛信息和到達(dá)時間判斷是否重復(fù)有車輛否是儲存車輛信息和時間信息結(jié) 束車輛離開停車場開 始獲取車輛離開信息和時間計算時間差運算停車費用判斷便道中是否有車否輸出離開停車場信息和收費金額車輛離開停車場更新棧表結(jié) 束是輸出車輛現(xiàn)在停止在便道位置等待車輛離開四、 系統(tǒng)使用說明運行環(huán)境為VC+6.0,如果需要在VS上運行需要改動scanf函數(shù)為scanf_s運行截圖附:源碼#include <stdio.h> #include <stdli
28、b.h> #include <string.h> #include <math.h>#include <time.h>#define OK 1#define ERROR 0typedef struct time1char times100;long time2;time1;typedef struct nodechar num10;time1 reach;time1 leave;Car;typedef struct StackCar *stack100;int top;SqStack;typedef struct carCar *data;struct
29、 car *next;QNode;typedef struct NodeQNode *front;QNode *rear;LinkQueue;void print(Car *p, int room, int MAX, float price);int arrive(SqStack *In, LinkQueue *W, int MAX, float price);void leave(SqStack *In, SqStack *Out, LinkQueue *W, int MAX, float price);void info1(SqStack *S);void info2(LinkQueue
30、*W);void info(SqStack S, LinkQueue W);void InitStack(SqStack *s, int MAX);int InitQueue(LinkQueue *Q);void main()SqStack In, Out;LinkQueue Wait;int ch;int MAX;float price;printf("請輸入停車場的容量:");scanf("%d", &MAX);printf("請輸入停車場的收費標(biāo)準(zhǔn)(元 / 小時) :");scanf("%f", &a
31、mp;price);InitStack(&In, MAX);InitStack(&Out, MAX);InitQueue(&Wait);while (1)system("color 0A");printf(" = = = = 歡迎使用停車場管理系統(tǒng) = = = = = = = = = = n");printf("輸入的停車場容量為%d位,費用為%2.1f元 / 小時, 請按任意鍵進入系統(tǒng)", MAX, price);getchar();system("CLS");printf("
32、= = = = 歡迎使用停車場管理系統(tǒng) = = = = = = = = = n");printf("您輸入的停車場容量為%d位,費用為%2.1f元 / 小時。n", MAX, price);printf("n = = = = = = = = = = = = = n");printf("(1)車輛到達(dá)n(2)車輛離開n(3)停車場信息n(4)退出系統(tǒng)n請選擇n");printf("n = = = = = = = = = = = = = = n");while (1)printf("請輸入操作的序號
33、");scanf("%d", &ch);switch (ch)case 1:arrive(&In, &Wait, MAX, price);break;case 2:leave(&In, &Out, &Wait, MAX, price);break;case 3:info(In, Wait);break;case 4: system("CLS"); printf("n = = = = = = = = = = = = = = n"); printf(" 謝謝使用!n&qu
34、ot;); printf("n = = = = = = = = = = = = = = = = = = = = = n"); exit(0);default:printf("n按鍵無效,請重新按鍵選擇");getchar();system("CLS");printf(" = = = = = 歡迎使用停車場管理系統(tǒng) = = = = = n");printf("您輸入的停車場容量為%d位,費用為%2.1f元 / 小時。n", MAX, price);printf("n = = = = =
35、= = = = = = = = = = = = = = = n");printf("(1)車輛到達(dá)n(2)車輛離開n(3)停車場信息n(4)退出系統(tǒng)n請選擇n");printf("n = = = = = = = = = = = = = = = = = = = = = n");/主函數(shù)菜單void print(Car *p, int room, int MAX, float price)time_t rawtime; struct tm * timeinfo; long A1, A2, B1, B2;time ( &rawtime );t
36、imeinfo = localtime ( &rawtime );p->leave.time2=rawtime;printf("n車離開的時間為:%s ",asctime (timeinfo);strcpy(p->leave.times,asctime (timeinfo);printf("n車牌號碼:");puts(p->num);printf("n車到達(dá)的時間是: %s ", p->reach.times);printf("n車離開的時間是: %s ", p->leave.
37、times);A1=p->leave.time2;A2=p->reach.time2;B1=p->leave.time2-p->reach.time2;if(B1<=3600&&B1>=0)printf("n費用為: %2.1f元", price);elseprintf("n費用為: %2.1f元", B1/3600*price);free(p);/消費計算函數(shù)int arrive(SqStack *In, LinkQueue *W, int MAX, float price)Car *p;QNode
38、*t;time_t rawtime; struct tm * timeinfo; /time ( &rawtime ); / 獲取時間,以秒計,從1970年1月一日起算,存于rawtime/timeinfo = localtime ( &rawtime ); /轉(zhuǎn)為當(dāng)?shù)貢r間,tm 時間結(jié)構(gòu)/printf ( "當(dāng)前系統(tǒng)時間: %s", asctime (timeinfo) );/asctime (timeinfo); / 轉(zhuǎn)為標(biāo)準(zhǔn)ASCII時間格式:p = (Car *)malloc(sizeof(Car);printf(" = = = = = =
39、 = = = = = 歡迎使用停車場管理系統(tǒng) = = = = = = = = = = = n");printf("您輸入的停車場容量為%d位,費用為%2.1f元 / 小時。n", MAX, price);printf("停車場還有%d停車位, 車可先進入便道等待", MAX - In->top);printf("n請輸入車牌號碼(例:警A1035) :");getchar();scanf("%s", p->num);getchar();if (In->top < MAX)time
40、( &rawtime );p->reach.time2=rawtime;timeinfo = localtime ( &rawtime );In->top+;printf("停車的位置:%d號停車位。", In->top);printf("n車到達(dá)的時間為%s ",asctime (timeinfo);strcpy(p->reach.times,asctime (timeinfo);In->stackIn->top = p;printf("請按任意鍵返回n");printf(&quo
41、t;n = = = = = = = = = = = = = = = = = = = = = = = n");getchar();return OK;elseprintf(" = = = = = 歡迎使用停車場管理系統(tǒng) = = = = = = = = = n");printf("您輸入的停車場容量為%d位,費用為%2.1f元 / 小時。n", MAX, price);printf("n = = = = = = = = = = = = = = = = = = = = = n");printf("停車位已滿,該車須在便道
42、等待!");t = (QNode *)malloc(sizeof(QNode);t->data = p;t->next = NULL;W->rear->next = t;W->rear = t;printf("n請按任意鍵返回");getchar();return OK;getchar();void leave(SqStack *In, SqStack *Out, LinkQueue *W, int MAX, float price)int place;Car *p, *t;QNode *q;time_t rawtime;struct
43、 tm * timeinfo;system("CLS");if (In->top>0)while (1)printf(" = = = = 歡迎使用停車場管理系統(tǒng) = = = = = = = = = n");printf("您輸入的停車場容量為%d位,費用為%2.1f元 / 小時。n", MAX, price);printf("n = = = = = = = = = = = = = = = = = = = = = = = = = = n");printf("請輸入車在停車場的位置(1 - %d)
44、:", In->top);scanf("%d", &place);if (place >= 1 && place <= In->top) break;while (In->top>place)Out->top+;Out->stackOut->top = In->stackIn->top;In->stackIn->top = NULL;In->top-;p = In->stackIn->top;In->stackIn->top = NU
45、LL;In->top-;while (Out->top >= 1)In->top+;In->stackIn->top = Out->stackOut->top;Out->stackOut->top = NULL;Out->top-;print(p, place, MAX, price);if (W->front != W->rear) && In->top<MAX)time ( &rawtime );timeinfo = localtime ( &rawtime );q = W->front->next;t = q->data;In->top+;printf("n便道的%s號車進入車場第%d號停車位。", t->num, In->top);printf("n車到達(dá)的
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 射頻濾波器課程設(shè)計
- 課程設(shè)計管理系統(tǒng)選題
- 2024至2030年中國高級潤膚洗手乳液數(shù)據(jù)監(jiān)測研究報告
- 2024至2030年中國汽車剎車?yán)瓧U行業(yè)投資前景及策略咨詢研究報告
- 2024年轉(zhuǎn)換器設(shè)備項目可行性研究報告
- 2024年船用軸流通風(fēng)機項目可行性研究報告
- 2024年電焊機滑塊項目可行性研究報告
- 2024年漲縮機液壓站項目可行性研究報告
- 2024年油箱控制裝置項目可行性研究報告
- 中國高純過氧化氫行業(yè)應(yīng)用潛力及未來趨勢預(yù)測研究報告(2024-2030版)
- 皮炎濕疹診斷治療課件
- Python程序設(shè)計課件第7章面向?qū)ο蟪绦蛟O(shè)計
- 空運提單格式
- 課件零件手冊vespa gts250ie2011-2013cina
- 咽喉解剖生理醫(yī)學(xué)課件
- 幼兒園課件《撓撓小怪物》
- 骨質(zhì)疏松癥-PPT課件
- 調(diào)查問卷-“職工之家”建設(shè)調(diào)查問卷
- 2019年11月系統(tǒng)集成項目管理工程師真題
- 小小建筑師公開課-PPT課件
- 完整版老舊住宅小區(qū)綜合整治工程施工組織設(shè)計方案
評論
0/150
提交評論