轉載請註明出處:http://blog.csdn.net/u012860063?viewmode=contents
問題描述:
由於我國經濟發展迅速,車輛的擁有量也跟著大幅上升,城市擁堵的情況越來越嚴重,停車場越來越成為一種稀缺資源,因此就有了要求高效利用停車場的需求。
控制稀缺資源的有效辦法就是收費。停車場的收費規則是,1小時以內免費。超過1小時,每小時6元錢。人工計費費時費力,而且容易出錯,希望你們開發一個軟體來協助辛勤的保安們來管理停車場。
1)對車位進行管理:
能查詢一共有多少車位
能查詢有多少空的車位
能查詢佔用車位對應的車牌號和停靠時間
2)收費管理:
能查詢現在停靠了多少車
能夠記錄車輛的停靠時間
根據根據停靠時間計算出費用
代碼如下:
#include<cstdio>#include<cstring>#include<malloc.h>#include<ctime>#define NULL 0# define LEN sizeof(struct node)struct node{int num; //序號char numble[47]; //車牌char intime[47]; //進入時間char outtime[47]; //出去時間struct node *next;};struct node *creat()//建立一個有十個車位的停車場鏈表{int n;struct node *head,*p,*tail;head = NULL;for(n = 1 ; n <= 10 ; n++ ){p = (struct node*)malloc(LEN);p->num = n ; strcpy(p->numble,"0");strcpy(p->intime,"0");strcpy(p->outtime,"0");if(n == 1){head = p;}elsetail->next = p;tail = p;}tail->next = NULL;return(head);}void print(struct node *head){struct node *p;printf("當前停車場資訊如下:\n\n");p=head;if(head!=NULL){do{printf("車場序號: %-6d車牌號碼: %5s\n",p->num,p->numble);printf("進入時間:%32s\n",p->intime);printf("駛出時間:%32s\n",p->outtime);printf("*******************************************\n");p=p->next;}while(p!=NULL);}}void Money(struct node *head)//計費{int n,m;struct node *p;time_t rawtime;struct tm*timeinfo;time(&rawtime);timeinfo = localtime(&rawtime);printf("親、請輸入你要計費的車輛序號\n");scanf("%d",&n);p = head;while(p->num != n){p = p->next; //尋找對應序號}//int tt = asctime(timeinfo) - p->intime;char time1[47],time2[47];strcpy(time1,asctime(timeinfo));strcpy(time2,p->intime);int len1 = strlen(time1);int len2 = strlen(time2);int t1= 0,t2 = 0;for(int i = 0 ; i < len1 ; i++){if(time1[i] == ':'){t1 = t1*10+time1[i-2]-'0';}}for( i = 0 ; i < len2 ; i++){if(time2[i] == ':'){t2 = t2*10+time2[i-2]-'0';}}int tt = t2 - t1;if(tt > 1)m = (tt-1)*6;elsem = 0;printf("此次停車共計費用為: %d\n",m);} void in(struct node *head)//車輛進入停車場{char s[47];time_t rawtime;struct tm*timeinfo;time(&rawtime);timeinfo=localtime(&rawtime);struct node *p;p = head;while((p!=NULL)&&(strcmp(p->numble,"0")!=0))//尋找空車位{p=p->next;} if(p!=NULL){printf("請輸入當前的車牌號!\n");scanf("%s",s);printf("您的停車位是:[%d]號!\n",p->num);strcpy(p->numble,s);strcpy(p->intime,asctime(timeinfo));}else{printf("停車場已滿,親、請退出操作!\n");}}void out(struct node* head){struct node *p;int n;time_t rawtime;struct tm*timeinfo;time(&rawtime);timeinfo = localtime(&rawtime);printf("請輸入車輛的序號!\n");scanf("%d",&n);p = head;while(p->num != n){p = p->next; //尋找對應序號}strcpy(p->outtime,asctime(timeinfo));printf("車牌 號碼為:[%s]\n",p->numble);printf("車輛進入時間為:%s\n",p->intime);printf("車輛駛出時間為:%s\n",p->outtime);strcpy(p->numble,"0");strcpy(p->intime,"0");}void main(){int n;struct node *head;head=NULL;printf("請輸入對應的數字,進行操作\n");printf("0:退出程式\n");printf("1:建立停車場資訊\n");printf("2:輸出停車場資訊\n");printf("3:車輛進入停車場\n"); printf("4:車輛駛出停車場\n");printf("5: 車輛停車所需費用\n");printf("請輸入對應的數字,進行操作\n");scanf("%d",&n);while(n!=0){switch(n){case 1:head=creat();break;case 2: print(head);break;case 3:in(head); break;case 4:out(head);break;case 5:Money(head);break;default: 0;}printf("請輸入相應的數字,進行操作\n");scanf("%d",&n);}}