C語言實現(xiàn)停車場項目
本文實例為大家分享了C語言實現(xiàn)停車場項目的具體代碼,供大家參考,具體內容如下
停車場項目需求
問題描述:停車場是一個能放 n 輛車的狹長通道,只有一個大門,汽車按到達的先后次序停放。若車場滿了,車要停在門外的便道上等候,一旦有車走,則便道上第一輛車進入。當停車場中的車離開時,由于通道窄,在它后面的車要先退出,待它走后在依次進入。汽車離開時按停放時間收費。
基本功能要求:
(1)建立三個數(shù)據(jù)結構分別是:停放棧、讓路棧、等候隊列。
(2)輸入數(shù)據(jù)模擬管理過程,數(shù)據(jù)(入或出,車號)
功能描述:進車登記、出車登記、按車牌號查詢停車車輛信息、查詢出入車記錄、查詢場內車輛信息、查詢等候車輛信息、退出系統(tǒng)。
(1)linux系統(tǒng)編寫(鏈表、棧、隊列);
(2)進車登記:登記車牌號以及入場時間;
(3)出車登記:計算出停車時間,記錄車輛車牌;
(4)按車牌號查詢車輛信息:停車時間,是否來過停車場,是否還在停車場
(5)查詢出入記錄:所有車輛,包括已經離開的
(6)查詢場內車輛信息:列出所有場內車輛信息
(7)查詢等候車輛信息:顯示等候車輛數(shù)量以及所有車牌號
(8)退出系統(tǒng)。
分析,底層需要寫以下內容:
兩個棧,停放棧(車牌號,出場時間,入場時間)[初始化,判斷是否為空,出棧,入棧,查棧] 讓路棧(車牌號)[初始化,出棧,入棧,判斷是否為空]
一個隊列,[初始化,出隊列,入隊列](車牌號)
一個鏈表,(車牌號,出入廠狀態(tài),入場時間,出場時間)[初始化,入鏈表,查找數(shù)據(jù),遍歷打印]
Windows下代碼
工程分為三個文件,分別是main.c fun.c pack.h
main.c:
#include<stdio.h> #include"park.h" #include<stdlib.h> #include<time.h> #include<string.h> /* main函數(shù) 功能:顯示界面,判斷用戶要使用哪個功能。 入參:無 返回值:無 */ void main()? { ? ? int ret=0; ? ? char number[10]; ? ? park_init(); ? ? //所有數(shù)據(jù)結構初始化 ? ? //存放用戶輸入的字符 ? ? while(1)? ? ? { ? ? ? ? /*顯示界面*/ ? ? ? ? printf("################## ? ? ? ? ?停車場 ?v1.0 ? ? ? ? ? ##################\n"); ? ? ? ? printf("# ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #\n"); ? ? ? ? printf("#####################################################################\n"); ? ? ? ? printf("# ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? 1-----------------進車登記------------------- ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? 2-----------------出車登記------------------- ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? 3-----------------車輛查詢------------------- ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? 4-----------------出入記錄------------------- ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? 5-----------------場內車輛------------------- ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? 6-----------------等候車輛------------------- ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? 7-----------------退出系統(tǒng)------------------- ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #\n"); ? ? ? ? printf("#####################################################################\n"); ? ? ? ? printf("# ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?201808 ? ? ? ? ? #\n"); ? ? ? ? printf("#####################################################################\n"); ? ? ? ? gets( number ); ? ? ? ? switch ( *number ) //選擇需要什么功能? ? ? ? ? { ? ? ? ? ? ? case '1': //進車登記? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? system("cls"); ? ? ? ? ? ? ? ? ret=car_in(); ? ? ? ? ? ? ? ? if(ret==FAILURE)? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? printf("輸入錯誤!"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? printf("入車成功!\n"); ? ? ? ? ? ? ? ? getchar(); ? ? ? ? ? ? ? ? system("cls"); ? ? ? ? ? ? } ? ? ? ? ? ? break; ? ? ? ? ? ? case '2': //出車登記? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? system("cls"); ? ? ? ? ? ? ? ? ret=car_out(); ? ? ? ? ? ? ? ? if(ret==FAILURE)? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? printf("輸入錯誤!\n"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? printf("出車成功!\n"); ? ? ? ? ? ? ? ? getchar(); ? ? ? ? ? ? ? ? system("cls"); ? ? ? ? ? ? } ? ? ? ? ? ? break; ? ? ? ? ? ? case '3': //查找車輛? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? system("cls"); ? ? ? ? ? ? ? ? ret=find_car(); ? ? ? ? ? ? ? ? if(ret==FAILURE)? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? printf("輸入錯誤!\n"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? //printf("-------------------------------------------------------------------------------------------------------------"); ? ? ? ? ? ? ? ? getchar(); ? ? ? ? ? ? ? ? system("cls"); ? ? ? ? ? ? } ? ? ? ? ? ? break; ? ? ? ? ? ? case '4': //出入記錄? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? system("cls"); ? ? ? ? ? ? ? ? ret=record_all(); ? ? ? ? ? ? ? ? if(ret==FAILURE)? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? printf("輸入錯誤!\n"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? getchar(); ? ? ? ? ? ? ? ? system("cls"); ? ? ? ? ? ? } ? ? ? ? ? ? break; ? ? ? ? ? ? case '5': //場內車輛? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? system("cls"); ? ? ? ? ? ? ? ? ret=car_park(); ? ? ? ? ? ? ? ? if(ret==FAILURE)? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? printf("輸入錯誤!\n"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? getchar(); ? ? ? ? ? ? ? ? system("cls"); ? ? ? ? ? ? } ? ? ? ? ? ? break; ? ? ? ? ? ? case '6':? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? system("cls"); ? ? ? ? ? ? ? ? //等候車輛 ? ? ? ? ? ? ? ? ret=Car_wait(); ? ? ? ? ? ? ? ? if(ret==FAILURE)? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? printf("輸入錯誤!\n"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? getchar(); ? ? ? ? ? ? ? ? system("cls"); ? ? ? ? ? ? } ? ? ? ? ? ? break; ? ? ? ? ? ? case '7': ? ? ? ? ? ? ? ? ? ? ? ? printf("歡迎下次使用\n"); ? ? ? ? ? ? break; ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? ? ? ? ? system("cls"); ? ? ? ? ? ? printf( "操作錯誤,此項不存在!\n" ); ? ? ? ? ? ? getchar(); ? ? ? ? ? ? system("cls"); ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? if ( strcmp( number, "7" ) == 0 ) ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? } }
park.h
#ifndef _PARK_H #define _PAEK_H #include <time.h> #include <stdio.h> #define SUCCESS 10000 #define FAILURE 10001 #define TRUE 10002 #define FALSE 10003 #define SIZE 6 ? //車場大小 /*汽車信息,節(jié)點結構體 */ struct car_info? { ? ? char name[10]; ? ? // ?車牌 ? ? time_t time1; ? ? // ?入場時間 ? ? time_t time2; ? ? // ?出場時間 } ; typedef struct car_info CAR_I; /*停放,讓路順序棧結構體 需要的參數(shù)為 :車牌號,出場時間,入場時間 配套的子函數(shù) :初始化,判斷是否為空,出棧,入棧,查棧*/ struct sequencestack? { ? ? int top; ? ? CAR_I *date; } ; typedef struct sequencestack ST; /*隊列節(jié)點結構體 需要的參數(shù)為 :車牌號,下一個節(jié)點地址 */ struct car_wait? { ? ? char name[10]; ? ? struct car_wait *next; } ; typedef struct car_wait CAR_W; /*等候鏈式隊列結構 需要的參數(shù)為 :頭指針,尾指針 配套的子函數(shù) :初始化,出隊列,入隊列 */ struct linkqueue? { ? ? CAR_W *front; ? ? CAR_W *rear; } ; typedef struct linkqueue LQ; /*存放所有信息的鏈表 需要的參數(shù)為:車牌號,出入廠狀態(tài),入場時間,出場時間,下一個節(jié)點的地址 需要的函數(shù)為:初始化,入鏈表,查找數(shù)據(jù),遍歷打印 */ struct all_info? { ? ? char name[10]; ? ? char sit[10]; ? ? time_t time1; ? ? // ?入場時間 ? ? time_t time2; ? ? // ?出場時間 ? ? struct all_info *next; } ; typedef struct all_info A_INFO; int car_in(); int car_out(); int find_car(); int record_all(); int car_park(); int Car_wait(); int STinit(ST **q); int STempty(ST *q); int STinsert(ST *q, char na[] ,time_t time); int STout(ST *q,char *a, time_t *time); int LQinit(LQ **q); int LQinsert(LQ *q, char na[]); int LQout(LQ *q, char *a); int LLinit(A_INFO **q); int LLinsert(A_INFO *q, int n, char na[], char s[], time_t timeA, time_t timeB); int LLfind(A_INFO *q, char na[]); int LLget(A_INFO *q, int n, A_INFO **k); int LLtra(A_INFO *q); int parkadd(CAR_I car[], char na[], time_t time); int parkdel(CAR_I car[] ,char na[],time_t time); int print(CAR_I car[]); int park_init(); struct tm * local_time(time_t *tmpcal_ptr); int exchange(A_INFO *q, char na[], char s[]); #endif
fun.c
#include<stdio.h> #include"park.h" #include<stdlib.h>? #include<time.h> #include<string.h> int num = 0; //場內車輛數(shù) ST *stack_park; //停放棧 ST *stack_exchange; //交換棧 LQ *queue_wait; //等候隊列 A_INFO ?*all_car; //存放所有信息的鏈表 /* 順序棧初始化: ? ? 參數(shù):結構體指針 */ int STinit(ST **q)? { ? ? if(q == NULL)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? *q = (ST *)malloc(sizeof(ST)*1); ? ? //為結構體指針分配空間 ? ? (*q)->top = -1; ? ? (*q)->date = (CAR_I *)malloc(sizeof(CAR_I)*SIZE); ? ? //為內容指針分配空間 ? ? return SUCCESS; } /* 順序棧判斷是否為空 ? ? 參數(shù):結構體指針 */ int STempty(ST *q)? { ? ? if(q == NULL)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? return(q->top == -1)? TRUE:FALSE; ? ? //空返回true ? 不空返回false } /* 順序棧入棧 ? ? 參數(shù):結構體指針,車牌,入場時間 */ int STinsert(ST *q, char na[] ,time_t time)? { ? ? if(q == NULL || q->top >= SIZE-1)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? strcpy( q->date[q->top+1].name, na ); ? ? q->date[q->top+1].time1 = time; ? ? q->top++; ? ? return SUCCESS; } /* 結構體出棧 ? ? 參數(shù):結構體指針,保存車牌的形參,保存入場時間的結構體指針 */ int STout(ST *q,char *a, time_t *time)? { ? ? if(q == NULL)? ? ? { ? ? ? ? return FAILURE; ? ? ? ? //錯誤返回failure ? ? } ? ? if(q->top == -1)? ? ? { ? ? ? ? return FALSE; ? ? ? ? //空返回false ? ? } ? ? strcpy(a, q->date[q->top].name); ? ? //a被修改為出場的車牌 ? ? *time = q->date[q->top].time1; ? ? //time被修改為入場時間 ? ? q->top--; ? ? return SUCCESS; } /* 鏈式隊列初始化 ? ? 參數(shù):隊列的結構體指針 */ int LQinit(LQ **q)? { ? ? CAR_W (*p); ? ? (*q) = (LQ *)malloc(sizeof(LQ)); ? ? if( (*q) == NULL )? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? p = (CAR_W *)malloc(sizeof(CAR_W)); ? ? if(p == NULL)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? p->next = NULL; ? ? (*q)->front = (*q)->rear =p; ? ? return SUCCESS; } /* 鏈式隊列入隊 ? ? 參數(shù):隊列的結構體指針,車牌 */ int LQinsert(LQ *q, char na[])? { ? ? CAR_W *p; ? ? if (NULL == q)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? p = (CAR_W *)malloc(sizeof(CAR_W)); ? ? if (NULL == p)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? strcpy(p->name, na); ? ? p->next = NULL; ? ? q->rear->next = p; ? ? q->rear = p; ? ? return SUCCESS; } /* 鏈式隊列出隊 ? ? 參數(shù):隊列結構體指針 */ int LQout(LQ *q, char *a)? { ? ? // ?char na[10]; ? ? CAR_W *p = q->front->next; ? ? if (NULL == q )? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? if(q->rear == q->front)? ? ? { ? ? ? ? return FALSE; ? ? } ? ? strcpy(a, p->name); ? ? q->front->next = p->next; ? ? if (NULL == p->next)? ? ? { ? ? ? ? q->rear = q->front; ? ? ? ? //用參數(shù)a保存出去的車牌 ? ? } ? ? free(p); ? ? return SUCCESS; } /* 鏈表初始化 ? ? 參數(shù):頭指針 */ int LLinit(A_INFO **q)? { ? ? (*q) = (A_INFO *)malloc(sizeof(A_INFO)); ? ? if( (*q) == NULL)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? (*q)->next = NULL; ? ? return SUCCESS; } /* ? ?鏈表插入(頭插) ? ? ? ? 參數(shù):頭指針,插入的位置(1), 車牌, 狀態(tài), 入場時間, 出場時間 */ int LLinsert(A_INFO *q, int n, char na[], char s[], time_t timeA, time_t timeB)? { ? ? A_INFO *l; ? ? A_INFO *p = q; ? ? int k = 1; ? ? if (NULL == q)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? while(k < n && p != NULL)? ? ? { ? ? ? ? p=p->next; ? ? ? ? k++; ? ? } ? ? if(k > n || p == NULL)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? l = (A_INFO *)malloc(sizeof(A_INFO)*1); ? ? if (NULL == l)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? strcpy(l->name, na); ? ? strcpy(l->sit ,s); ? ? l->time1 = timeA; ? ? l->time2 = timeB; ? ? l->next = p->next; ? ? p->next = l; ? ? return SUCCESS; } /* 鏈表定位 ? ? 參數(shù): 頭指針,車牌 */ int LLfind(A_INFO *q, char na[])? { ? ? A_INFO *p; ? ? int len; ? ? if (NULL == q)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? p = q->next; ? ? len = 1; ? ? while(p)? ? ? { ? ? ? ? if(strcmp(p->name, na) == 0)? ? ? ? ? { ? ? ? ? ? ? return len; ? ? ? ? ? ? //找到返回位置 ? ? ? ? } ? ? ? ? p = p->next; ? ? ? ? len++; ? ? } ? ? return FALSE; ? ? //未找到返回false } /* 鏈表查詢 ? ? ? ? 參數(shù):頭指針,位置 */ int LLget(A_INFO *q, int n, A_INFO **k)? { ? ? int i; ? ? A_INFO *p = q; ? ? if (NULL == q)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? for (i = 0; i < n && p != NULL ; i++)? ? ? { ? ? ? ? p = p->next; ? ? } ? ? if(!p)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? (*k) = p; ? ? //用k指針保存找到的結構體地址 ? ? return SUCCESS; } /* 鏈表遍歷 ? ? 參數(shù):頭指針 */ int LLtra(A_INFO *q)? { ? ? double cost; ? ? struct tm *time1; ? ? struct tm *time2; ? ? time_t timenow; ? ? A_INFO *p; ? ? if (NULL == q)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? p = q; ? ? while (p->next)? ? ? { ? ? ? ? p = p->next; ? ? ? ? ////////////////////記得改這里 ? ? ? ? time1 = local_time(&p->time1); ? ? ? ? if(time1 != NULL)? ? ? ? ? { ? ? ? ? ? ? printf("車牌:%s ? 狀態(tài):%s ? 入場時間:%d:%d:%d",p->name,p->sit, time1->tm_hour, time1->tm_min, time1->tm_sec); ? ? ? ? ? ? time2 = local_time(&p->time2); ? ? ? ? ? ? if(time2 != NULL)? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? cost = difftime(p->time2,p->time1); ? ? ? ? ? ? ? ? printf(" ?出場時間:%d:%d:%d", time2->tm_hour, time2->tm_min, time2->tm_sec); ? ? ? ? ? ? ? ? printf(" ?停車時間:%f秒\n",cost); ? ? ? ? ? ? ? ? printf("-------------------------------------------------------------------------------------------------------------\n"); ? ? ? ? ? ? } else? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? time(&timenow); ? ? ? ? ? ? ? ? cost = difftime(timenow,p->time1); ? ? ? ? ? ? ? ? printf(" ?停車時間為:%f秒\n" ,cost); ? ? ? ? ? ? ? ? printf("-------------------------------------------------------------------------------------------------------------\n"); ? ? ? ? ? ? ? ? //return 0; ? ? ? ? ? ? } ? ? ? ? } else? ? ? ? ? { ? ? ? ? ? ? printf("車牌:%s ?排隊中\(zhòng)n",p->name); ? ? ? ? ? ? printf("-------------------------------------------------------------------------------------------------------------\n"); ? ? ? ? ? ? // ?return 0; ? ? ? ? } ? ? } ? ? return SUCCESS; } /* 結構體數(shù)組增加 ? ? 參數(shù):結構體數(shù)組, 車牌,入場時間(下標使用全局變量-車的數(shù)量) */ int parkadd(CAR_I car[], char na[], time_t time)? { ? ? if(num>=6)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? strcpy(car[num].name, na); ? ? car[num].time1 = time; ? ? num++; ? ? return SUCCESS; } /* 結構體數(shù)組減少 ? ? 參數(shù):同上(時間為出廠) */ int parkdel(CAR_I car[] ,char na[],time_t time)? { ? ? int i; ? ? if(num == 0)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? for (i = 0; i < num; i++)? ? ? { ? ? ? ? if(strcmp(car[i].name, na)==0)? ? ? ? ? { ? ? ? ? ? ? for (; i<num; i++)? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? car[i] = car[i+1]; ? ? ? ? ? ? } ? ? ? ? ? ? break; ? ? ? ? } ? ? } ? ? num--; ? ? return SUCCESS; } /* 打印所有 ? ? 參數(shù):結構體數(shù)組 */ int print(CAR_I car[])? { ? ? int i; ? ? for (i = 0; i<num; i++)? ? ? { ? ? ? ? printf("場內車輛信息"); ? ? } ? ? return SUCCESS; } /* 修改鏈表狀態(tài)函數(shù) ? ? 參數(shù) :頭指針,所需修改的車牌,修改為的狀態(tài) */ int exchange(A_INFO *q, char na[], char s[])? { ? ? A_INFO *p; ? ? int len; ? ? if (NULL == q)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? p = q->next; ? ? len = 1; ? ? while(p)? ? ? { ? ? ? ? if(strcmp(p->name, na) == 0)? ? ? ? ? { ? ? ? ? ? ? strcpy( p->sit, s ) ; ? ? ? ? ? ? return SUCCESS; ? ? ? ? } ? ? ? ? p = p->next; ? ? ? ? len++; ? ? } ? ? return FALSE; } /* 修改鏈表time2函數(shù) ? ? 參數(shù) :頭指針,所需修改的車牌,修改為的time */ int exchange1(A_INFO *q, char na[], time_t tmpcal_ptr)? { ? ? A_INFO *p; ? ? int len; ? ? if (NULL == q)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? p = q->next; ? ? len = 1; ? ? while(p)? ? ? { ? ? ? ? if(strcmp(p->name, na) == 0)? ? ? ? ? { ? ? ? ? ? ? p->time2 = tmpcal_ptr; ? ? ? ? ? ? return SUCCESS; ? ? ? ? } ? ? ? ? p = p->next; ? ? ? ? len++; ? ? } ? ? return FALSE; } /* 修改鏈表time1函數(shù) ? ? 參數(shù) :頭指針,所需修改的車牌,修改為的time */ int exchange2(A_INFO *q, char na[], time_t tmpcal_ptr)? { ? ? A_INFO *p; ? ? int len; ? ? if (NULL == q)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? p = q->next; ? ? len = 1; ? ? while(p)? ? ? { ? ? ? ? if(strcmp(p->name, na) == 0)? ? ? ? ? { ? ? ? ? ? ? p->time1 = tmpcal_ptr; ? ? ? ? ? ? return SUCCESS; ? ? ? ? } ? ? ? ? p = p->next; ? ? ? ? len++; ? ? } ? ? return FALSE; } /*//////////////////////////////////// 進車登記函數(shù) 功能:車輛進入登記,修改各存放結構體中的信息。 入參:無 返回值:SUCCESS/FAILURE ////////////////////////////////////*/ int car_in()? { ? ? int i; ? ? char a[12]; ? ? //記錄車牌號 ? ? int ret; ? ? time_t tmpcal_ptr; ? ? time_t tmpcal_ptr1; ? ? time_t tmpcal_ptr2; ? ? //為了給出車時間賦空值 ? ? printf("請輸入車牌號:\n"); ? ? gets(a); ? ? time(&tmpcal_ptr); ? ? ret=STinsert(stack_park, a ,tmpcal_ptr); ? ? if(ret==FAILURE)? ? ? { ? ? ? ? printf("停車場已滿,排隊中\(zhòng)n"); ? ? ? ? ret=LQinsert(queue_wait, a); ? ? ? ? LLinsert(all_car, 1, a, "排隊中",tmpcal_ptr1, tmpcal_ptr2); ? ? ? ? // exchange(all_car, a, "排隊中"); ? ? ? ? if(FAILURE==ret)? ? ? ? ? { ? ? ? ? ? ? return FAILURE; ? ? ? ? } ? ? } else? ? ? { ? ? ? ? LLinsert(all_car, 1, a, "在場中",tmpcal_ptr, tmpcal_ptr2); ? ? } ? ? for (i=0; i<stack_park->top+1; i++)? ? ? { ? ? ? ? printf("車牌 :%s\n",stack_park->date[i].name); ? ? } ? ? return SUCCESS; } /*//////////////////////////////////// 出車登記函數(shù) 功能:車輛出場登記,修改各存放結構體中的信息。 入參:無 返回值:SUCCESS/FAILURE ////////////////////////////////////*/ int car_out()? { ? ? char a[12]; ? ? char b[12]; ? ? char c[12]; ? ? int ret; ? ? time_t tmpcal_ptr3; ? ? //出場時間 ? ? time_t tmpcal_ptr4; ? ? // ? ? printf("\n請輸入出車車牌號:\n\n"); ? ? gets(a); ? ? time(&tmpcal_ptr3); ? ? // ?ret =STout(stack_park, b, &tmpcal_ptr4 ); ? ? // ?while(strcmp(b,a)!=0) ? ? while((ret = STout(stack_park, b, &tmpcal_ptr4 ))!=FALSE)? ? ? { ? ? ? ? if(strcmp(b,a)==0)? ? ? ? ? { ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? STinsert(stack_exchange, b, tmpcal_ptr4); ? ? ? ? // ?ret =STout(stack_park, b, &tmpcal_ptr4 ); ? ? } ? ? if(ret == FALSE)? ? ? { ? ? ? ? printf("未找到該車!\n\n"); ? ? ? ? while( (ret = STout(stack_exchange,b,&tmpcal_ptr4)) != FALSE)? ? ? ? ? { ? ? ? ? ? ? STinsert(stack_park, b,tmpcal_ptr4 ); ? ? ? ? } ? ? ? ? return FAILURE; ? ? } else? ? ? { ? ? ? ? ret = exchange(all_car , b ,"出場了"); ? ? ? ? ret = exchange1(all_car ,b ,tmpcal_ptr3); ? ? ? ? while( (ret = STout(stack_exchange,b,&tmpcal_ptr4)) != FALSE)? ? ? ? ? { ? ? ? ? ? ? STinsert(stack_park, b,tmpcal_ptr4 ); ? ? ? ? } ? ? ? ? if((ret = LQout(queue_wait, c))!=FALSE)? ? ? ? ? { ? ? ? ? ? ? time(&tmpcal_ptr4); ? ? ? ? ? ? STinsert(stack_park, c,tmpcal_ptr4); ? ? ? ? ? ? ret = exchange(all_car , c ,"在場中"); ? ? ? ? ? ? ret = exchange2(all_car ,c ,tmpcal_ptr3); ? ? ? ? } ? ? } ? ? return SUCCESS; } /*//////////////////////////////////// 尋找車輛函數(shù) 功能:在存放信息的鏈表中尋找車輛。 入參:無 返回值:SUCCESS/FAILURE ////////////////////////////////////*/ int find_car()? { ? ? int len; ? ? char a[12]; ? ? double cost; ? ? struct tm *time1; ? ? struct tm *time2; ? ? time_t timenow; ? ? A_INFO *k; ? ? int ret; ? ? printf("請輸入你要查詢的車牌:\n"); ? ? gets(a); ? ? len = LLfind(all_car, a); ? ? if(len == FALSE)? ? ? { ? ? ? ? printf("未來過\n\n"); ? ? ? ? return FAILURE; ? ? } ? ? ret = LLget(all_car, len, &k); ? ? time1 = local_time(&k->time1); ? ? if(time1 != NULL) ? ? { ? ? printf("車牌:%s\n狀態(tài):%s\n入場時間:%d:%d:%d\n",k->name,k->sit, time1->tm_hour, time1->tm_min, time1->tm_sec); ? ? time2 = local_time(&k->time2); ? ? if(time2 != NULL)? ? ? { ? ? ? ? cost = difftime(k->time2,k->time1); ? ? ? ? printf("出場時間:%d:%d:%d\n", time2->tm_hour, time2->tm_min, time2->tm_sec); ? ? ? ? printf("停車時間:%f\n秒",cost); ? ? } else? ? ? { ? ? ? ? time(&timenow); ? ? ? ? cost = difftime(timenow,k->time1); ? ? ? ? printf("出場時間:未出場\n停車時間:%f秒\n",cost); ? ? ? ? return 0; ? ? } ? ? } ? ? else ? ? ? ? printf("等候中"); ? ? return SUCCESS; }//////////////////////// 查看出入登記函數(shù) 功能:遍歷打印存放所有信息的鏈表 入參:無 返回值:SUCCESS/FAILURE ////////////////////////////////////*/ int record_all()? { ? ? LLtra(all_car); ? ? return SUCCESS; } /*//////////////////////////////////// 查看停車場內車輛信息函數(shù) 功能:遍歷打印存放停車場內信息的數(shù)組 入參:無 返回值:SUCCESS/FAILURE ////////////////////////////////////*/ int car_park()? { ? ? int i; ? ? double cost; ? ? struct tm *time1; ? ? time_t timenow; ? ? for (i=0; i<stack_park->top+1; i++)? ? ? { ? ? ? ? time1 = local_time(&stack_park->date[i].time1); ? ? ? ? printf("車牌:%s ? ? 入場時間:%d:%d:%d",stack_park->date[i].name, time1->tm_hour, time1->tm_min, time1->tm_sec); ? ? ? ? time(&timenow); ? ? ? ? cost = difftime(timenow,stack_park->date[i].time1); ? ? ? ? printf(" ?未出場,停車時間為:%f秒\n\n",cost); ? ? ? ? printf("-------------------------------------------------------------------------------------------------------------\n"); ? ? } ? ? return SUCCESS; } /*//////////////////////////////////// 查看等候隊列內車輛信息函數(shù) 功能:遍歷打印等候隊列中車輛 入參:無 返回值:SUCCESS/FAILURE ////////////////////////////////////*/ int Car_wait()? { ? ? CAR_W *p; ? ? p = queue_wait->front; ? ? while(p->next)? ? ? { ? ? ? ? p=p->next; ? ? ? ? printf("車牌:%s\n\n",p->name); ? ? } ? ? return SUCCESS; } /*//////////////////////////////////// 初始化函數(shù) 功能:初始化 入參:無 返回值:SUCCESS/FAILURE ////////////////////////////////////*/ int park_init()? { ? ? STinit(&stack_park); ? ? STinit(&stack_exchange); ? ? LQinit(&queue_wait); ? ? LLinit(&all_car); ? ? return SUCCESS; } /*//////////////////////////////////// 時間函數(shù) 功能:轉換成當?shù)貢r間 入參:time_t *tmpcal_ptr(等待轉換的時間) 返回值:time_local(當?shù)貢r間) ////////////////////////////////////*/ struct tm * local_time(time_t *tmpcal_ptr)? { ? ? struct tm *time_local = NULL; ? ? time_local = localtime(tmpcal_ptr); ? ? //轉換成當?shù)貢r間 ? ? return time_local; }
現(xiàn)象:
linux下代碼
分為三個文件
main_u.c fun_u.c park_u.h
main_u.c
#include<stdio.h> #include"park_u.h" #include<stdlib.h> #include<time.h> #include<string.h> /* main函數(shù) 功能:顯示界面,判斷用戶要使用哪個功能。 入參:無 返回值:無 */ void main()? { ? ? int ret=0; ? ? char number[10]; ? ? park_init(); ? ? //所有數(shù)據(jù)結構初始化 ? ? //存放用戶輸入的字符 ? ? while(1)? ? ? { ? ? ? ? /*顯示界面*/ ? ? ? ? printf("################## ? ? ? ? ?停車場 ?v1.0 ? ? ? ? ? ##################\n"); ? ? ? ? printf("# ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #\n"); ? ? ? ? printf("#####################################################################\n"); ? ? ? ? printf("# ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? 1-----------------進車登記------------------- ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? 2-----------------出車登記------------------- ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? 3-----------------車輛查詢------------------- ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? 4-----------------出入記錄------------------- ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? 5-----------------場內車輛------------------- ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? 6-----------------等候車輛------------------- ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? 7-----------------退出系統(tǒng)------------------- ? ? ? ? ? #\n"); ? ? ? ? printf("# ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? #\n"); ? ? ? ? printf("#####################################################################\n"); ? ? ? ? printf("# ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?201808 ? ? ? ? ? #\n"); ? ? ? ? printf("#####################################################################\n"); ? ? ? ? gets( number ); ? ? ? ? switch ( *number ) //選擇需要什么功能? ? ? ? ? { ? ? ? ? ? ? case '1': //進車登記? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); ? ? ? ? ? ? ? ? ret=car_in(); ? ? ? ? ? ? ? ? if(ret==FAILURE)? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? printf("輸入錯誤!"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? printf("入車成功!\n"); ? ? ? ? ? ? ? ? getchar(); ? ? ? ? ? ? ? ? printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); ? ? ? ? ? ? } ? ? ? ? ? ? break; ? ? ? ? ? ? case '2': //出車登記? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); ? ? ? ? ? ? ? ? ret=car_out(); ? ? ? ? ? ? ? ? if(ret!=FAILURE)? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? printf("出車成功!\n"); ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? getchar(); ? ? ? ? ? ? ? ? printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); ? ? ? ? ? ? } ? ? ? ? ? ? break; ? ? ? ? ? ? case '3': //查找車輛? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); ? ? ? ? ? ? ? ? ret=find_car(); ? ? ? ? ? ? ? ? if(ret==FAILURE)? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? printf("輸入錯誤!\n"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? //printf("-------------------------------------------------------------------------------------------------------------"); ? ? ? ? ? ? ? ? getchar(); ? ? ? ? ? ? ? ? printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); ? ? ? ? ? ? } ? ? ? ? ? ? break; ? ? ? ? ? ? case '4': //出入記錄? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); ? ? ? ? ? ? ? ? ret=record_all(); ? ? ? ? ? ? ? ? if(ret==FAILURE)? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? printf("輸入錯誤!\n"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? getchar(); ? ? ? ? ? ? ? ? printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); ? ? ? ? ? ? } ? ? ? ? ? ? break; ? ? ? ? ? ? case '5': //場內車輛? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); ? ? ? ? ? ? ? ? ret=car_park(); ? ? ? ? ? ? ? ? if(ret==FAILURE)? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? printf("輸入錯誤!\n"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? getchar(); ? ? ? ? ? ? ? ? printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); ? ? ? ? ? ? } ? ? ? ? ? ? break; ? ? ? ? ? ? case '6':? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); ? ? ? ? ? ? ? ? //等候車輛 ? ? ? ? ? ? ? ? ret=Car_wait(); ? ? ? ? ? ? ? ? if(ret==FAILURE)? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? printf("輸入錯誤!\n"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? getchar(); ? ? ? ? ? ? ? ? printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); ? ? ? ? ? ? } ? ? ? ? ? ? break; ? ? ? ? ? ? case '7': ? ? ? ? ? ? ? ? ? ? ? ? printf("歡迎下次使用\n"); ? ? ? ? ? ? break; ? ? ? ? ? ? default: ? ? ? ? ? ? ? ? ? ? ? ? ? ? printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); ? ? ? ? ? ? printf( "操作錯誤,此項不存在!\n" ); ? ? ? ? ? ? getchar(); ? ? ? ? ? ? printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n"); ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? if ( strcmp( number, "7" ) == 0 ) ? ? ? ? ? ? ? ? ? ? ? ? break; ? ? } }
fun_u.c
#include<stdio.h> #include"park_u.h" #include<stdlib.h>? #include<time.h> #include<string.h> int num = 0; //場內車輛數(shù) ST *stack_park; //停放棧 ST *stack_exchange; //交換棧 LQ *queue_wait; //等候隊列 A_INFO ?*all_car; //存放所有信息的鏈表 /* 順序棧初始化: ? ? 參數(shù):結構體指針 */ int STinit(ST **q)? { ? ? if(q == NULL)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? *q = (ST *)malloc(sizeof(ST)*1); ? ? //為結構體指針分配空間 ? ? (*q)->top = -1; ? ? (*q)->date = (CAR_I *)malloc(sizeof(CAR_I)*SIZE); ? ? //為內容指針分配空間 ? ? return SUCCESS; } /* 順序棧判斷是否為空 ? ? 參數(shù):結構體指針 */ int STempty(ST *q)? { ? ? if(q == NULL)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? return(q->top == -1)? TRUE:FALSE; ? ? //空返回true ? 不空返回false } /* 順序棧入棧 ? ? 參數(shù):結構體指針,車牌,入場時間 */ int STinsert(ST *q, char na[] ,time_t time)? { ? ? if(q == NULL || q->top >= SIZE-1)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? strcpy( q->date[q->top+1].name, na ); ? ? q->date[q->top+1].time1 = time; ? ? q->top++; ? ? return SUCCESS; } /* 結構體出棧 ? ? 參數(shù):結構體指針,保存車牌的形參,保存入場時間的結構體指針 */ int STout(ST *q,char *a, time_t *time)? { ? ? if(q == NULL)? ? ? { ? ? ? ? return FAILURE; ? ? ? ? //錯誤返回failure ? ? } ? ? if(q->top == -1)? ? ? { ? ? ? ? return FALSE; ? ? ? ? //空返回false ? ? } ? ? strcpy(a, q->date[q->top].name); ? ? //a被修改為出場的車牌 ? ? *time = q->date[q->top].time1; ? ? //time被修改為入場時間 ? ? q->top--; ? ? return SUCCESS; } /* 鏈式隊列初始化 ? ? 參數(shù):隊列的結構體指針 */ int LQinit(LQ **q)? { ? ? CAR_W (*p); ? ? (*q) = (LQ *)malloc(sizeof(LQ)); ? ? if( (*q) == NULL )? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? p = (CAR_W *)malloc(sizeof(CAR_W)); ? ? if(p == NULL)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? p->next = NULL; ? ? (*q)->front = (*q)->rear =p; ? ? return SUCCESS; } /* 鏈式隊列入隊 ? ? 參數(shù):隊列的結構體指針,車牌 */ int LQinsert(LQ *q, char na[])? { ? ? CAR_W *p; ? ? if (NULL == q)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? p = (CAR_W *)malloc(sizeof(CAR_W)); ? ? if (NULL == p)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? strcpy(p->name, na); ? ? p->next = NULL; ? ? q->rear->next = p; ? ? q->rear = p; ? ? return SUCCESS; } /* 鏈式隊列出隊 ? ? 參數(shù):隊列結構體指針 */ int LQout(LQ *q, char *a)? { ? ? // ?char na[10]; ? ? CAR_W *p = q->front->next; ? ? if (NULL == q )? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? if(q->rear == q->front)? ? ? { ? ? ? ? return FALSE; ? ? } ? ? strcpy(a, p->name); ? ? q->front->next = p->next; ? ? if (NULL == p->next)? ? ? { ? ? ? ? q->rear = q->front; ? ? ? ? //用參數(shù)a保存出去的車牌 ? ? } ? ? free(p); ? ? return SUCCESS; } /* 鏈表初始化 ? ? 參數(shù):頭指針 */ int LLinit(A_INFO **q)? { ? ? (*q) = (A_INFO *)malloc(sizeof(A_INFO)); ? ? if( (*q) == NULL)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? (*q)->next = NULL; ? ? return SUCCESS; } /* ? ?鏈表插入(頭插) ? ? ? ? 參數(shù):頭指針,插入的位置(1), 車牌, 狀態(tài), 入場時間, 出場時間 */ int LLinsert(A_INFO *q, int n, char na[], char s[], time_t timeA, time_t timeB)? { ? ? A_INFO *l; ? ? A_INFO *p = q; ? ? int k = 1; ? ? if (NULL == q)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? while(k < n && p != NULL)? ? ? { ? ? ? ? p=p->next; ? ? ? ? k++; ? ? } ? ? if(k > n || p == NULL)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? l = (A_INFO *)malloc(sizeof(A_INFO)*1); ? ? if (NULL == l)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? strcpy(l->name, na); ? ? strcpy(l->sit ,s); ? ? l->time1 = timeA; ? ? l->time2 = timeB; ? ? l->next = p->next; ? ? p->next = l; ? ? return SUCCESS; } /* 鏈表定位 ? ? 參數(shù): 頭指針,車牌 */ int LLfind(A_INFO *q, char na[])? { ? ? A_INFO *p; ? ? int len; ? ? if (NULL == q)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? p = q->next; ? ? len = 1; ? ? while(p)? ? ? { ? ? ? ? if(strcmp(p->name, na) == 0)? ? ? ? ? { ? ? ? ? ? ? return len; ? ? ? ? ? ? //找到返回位置 ? ? ? ? } ? ? ? ? p = p->next; ? ? ? ? len++; ? ? } ? ? return FALSE; ? ? //未找到返回false } /* 鏈表查詢 ? ? ? ? 參數(shù):頭指針,位置 */ int LLget(A_INFO *q, int n, A_INFO **k)? { ? ? int i; ? ? A_INFO *p = q; ? ? if (NULL == q)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? for (i = 0; i < n && p != NULL ; i++)? ? ? { ? ? ? ? p = p->next; ? ? } ? ? if(!p)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? (*k) = p; ? ? //用k指針保存找到的結構體地址 ? ? return SUCCESS; } /* 鏈表遍歷 ? ? 參數(shù):頭指針 */ int LLtra(A_INFO *q)? { ? ? double cost; ? ? struct tm *time1; ? ? struct tm *time2; ? ? time_t timenow=0; ? ? A_INFO *p; ? ? if (NULL == q)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? p = q; ? ? while (p->next)? ? ? { ? ? ? ? p = p->next; ? ? ? ? time1 = local_time(&p->time1); ? ? ? ? if(time1 != NULL)? ? ? ? ? { ? ? ? ? ? ? printf("車牌:%s ? 狀態(tài):%s ? 入場時間:%d:%d:%d",p->name,p->sit, time1->tm_hour, time1->tm_min, time1->tm_sec); ? ? ? ? ? ? time2 = local_time(&p->time2); ? ? ? ? ? ? if(!(time2->tm_hour==8&&time2->tm_min==0&&time2->tm_sec==0))? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? cost = difftime(p->time2,p->time1); ? ? ? ? ? ? ? ? printf(" ?出場時間:%d:%d:%d", time2->tm_hour, time2->tm_min, time2->tm_sec); ? ? ? ? ? ? ? ? printf(" ?停車時間:%f秒\n",cost); ? ? ? ? ? ? ? ? printf("-------------------------------------------------------------------------------------------------------------\n"); ? ? ? ? ? ? } else? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? time(&timenow); ? ? ? ? ? ? ? ? cost = difftime(timenow,p->time1); ? ? ? ? ? ? ? ? printf(" ?停車時間為:%f秒\n" ,cost); ? ? ? ? ? ? ? ? printf("-------------------------------------------------------------------------------------------------------------\n"); ? ? ? ? ? ? ? ? //return 0; ? ? ? ? ? ? } ? ? ? ? } else? ? ? ? ? { ? ? ? ? ? ? printf("車牌:%s ?排隊中\(zhòng)n",p->name); ? ? ? ? ? ? printf("-------------------------------------------------------------------------------------------------------------\n"); ? ? ? ? ? ? // ?return 0; ? ? ? ? } ? ? } ? ? return SUCCESS; } /* 結構體數(shù)組增加 ? ? 參數(shù):結構體數(shù)組, 車牌,入場時間(下標使用全局變量-車的數(shù)量) */ int parkadd(CAR_I car[], char na[], time_t time)? { ? ? if(num>=6)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? strcpy(car[num].name, na); ? ? car[num].time1 = time; ? ? num++; ? ? return SUCCESS; } /* 結構體數(shù)組減少 ? ? 參數(shù):同上(時間為出廠) */ int parkdel(CAR_I car[] ,char na[],time_t time)? { ? ? int i; ? ? if(num == 0)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? for (i = 0; i < num; i++)? ? ? { ? ? ? ? if(strcmp(car[i].name, na)==0)? ? ? ? ? { ? ? ? ? ? ? for (; i<num; i++)? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? car[i] = car[i+1]; ? ? ? ? ? ? } ? ? ? ? ? ? break; ? ? ? ? } ? ? } ? ? num--; ? ? return SUCCESS; } /* 打印所有 ? ? 參數(shù):結構體數(shù)組 */ int print(CAR_I car[])? { ? ? int i; ? ? for (i = 0; i<num; i++)? ? ? { ? ? ? ? printf("場內車輛信息"); ? ? } ? ? return SUCCESS; } /* 修改鏈表狀態(tài)函數(shù) ? ? 參數(shù) :頭指針,所需修改的車牌,修改為的狀態(tài) */ int exchange(A_INFO *q, char na[], char s[])? { ? ? A_INFO *p; ? ? int len; ? ? if (NULL == q)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? p = q->next; ? ? len = 1; ? ? while(p)? ? ? { ? ? ? ? if(strcmp(p->name, na) == 0)? ? ? ? ? { ? ? ? ? ? ? strcpy( p->sit, s ) ; ? ? ? ? ? ? return SUCCESS; ? ? ? ? } ? ? ? ? p = p->next; ? ? ? ? len++; ? ? } ? ? return FALSE; } /* 修改鏈表time2函數(shù) ? ? 參數(shù) :頭指針,所需修改的車牌,修改為的time */ int exchange1(A_INFO *q, char na[], time_t tmpcal_ptr)? { ? ? A_INFO *p; ? ? int len; ? ? if (NULL == q)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? p = q->next; ? ? len = 1; ? ? while(p)? ? ? { ? ? ? ? if(strcmp(p->name, na) == 0)? ? ? ? ? { ? ? ? ? ? ? p->time2 = tmpcal_ptr; ? ? ? ? ? ? return SUCCESS; ? ? ? ? } ? ? ? ? p = p->next; ? ? ? ? len++; ? ? } ? ? return FALSE; } /* 修改鏈表time1函數(shù) ? ? 參數(shù) :頭指針,所需修改的車牌,修改為的time */ int exchange2(A_INFO *q, char na[], time_t tmpcal_ptr)? { ? ? A_INFO *p; ? ? int len; ? ? if (NULL == q)? ? ? { ? ? ? ? return FAILURE; ? ? } ? ? p = q->next; ? ? len = 1; ? ? while(p)? ? ? { ? ? ? ? if(strcmp(p->name, na) == 0)? ? ? ? ? { ? ? ? ? ? ? p->time1 = tmpcal_ptr; ? ? ? ? ? ? return SUCCESS; ? ? ? ? } ? ? ? ? p = p->next; ? ? ? ? len++; ? ? } ? ? return FALSE; } /*//////////////////////////////////// 進車登記函數(shù) 功能:車輛進入登記,修改各存放結構體中的信息。 入參:無 返回值:SUCCESS/FAILURE ////////////////////////////////////*/ int car_in()? { ? ? int i; ? ? char a[12]; ? ? //記錄車牌號 ? ? int ret; ? ? time_t tmpcal_ptr; ? ? time_t tmpcal_ptr1; ? ? time_t tmpcal_ptr2; ? ? //為了給出車時間賦空值 ? ? printf("請輸入車牌號:\n"); ? ? gets(a); ? ? time(&tmpcal_ptr); ? ? ret=STinsert(stack_park, a ,tmpcal_ptr); ? ? if(ret==FAILURE)? ? ? { ? ? ? ? printf("停車場已滿,排隊中\(zhòng)n"); ? ? ? ? ret=LQinsert(queue_wait, a); ? ? ? ? LLinsert(all_car, 1, a, "排隊中",tmpcal_ptr1, tmpcal_ptr2); ? ? ? ? // exchange(all_car, a, "排隊中"); ? ? ? ? if(FAILURE==ret)? ? ? ? ? { ? ? ? ? ? ? return FAILURE; ? ? ? ? } ? ? } else? ? ? { ? ? ? ? LLinsert(all_car, 1, a, "在場中",tmpcal_ptr, tmpcal_ptr2); ? ? } ? ? for (i=0; i<stack_park->top+1; i++)? ? ? { ? ? ? ? printf("車牌 :%s\n",stack_park->date[i].name); ? ? } ? ? return SUCCESS; } /*//////////////////////////////////// 出車登記函數(shù) 功能:車輛出場登記,修改各存放結構體中的信息。 入參:無 返回值:SUCCESS/FAILURE ////////////////////////////////////*/ int car_out()? { ? ? char a[12]; ? ? char b[12]; ? ? char c[12]; ? ? int ret; ? ? time_t tmpcal_ptr3; ? ? //出場時間 ? ? time_t tmpcal_ptr4; ? ? // ? ? printf("\n請輸入出車車牌號:\n\n"); ? ? gets(a); ? ? time(&tmpcal_ptr3); ? ? // ?ret =STout(stack_park, b, &tmpcal_ptr4 ); ? ? // ?while(strcmp(b,a)!=0) ? ? while((ret = STout(stack_park, b, &tmpcal_ptr4 ))!=FALSE)? ? ? { ? ? ? ? if(strcmp(b,a)==0)? ? ? ? ? { ? ? ? ? ? ? break; ? ? ? ? } ? ? ? ? STinsert(stack_exchange, b, tmpcal_ptr4); ? ? ? ? // ?ret =STout(stack_park, b, &tmpcal_ptr4 ); ? ? } ? ? if(ret == FALSE)? ? ? { ? ? ? ? printf("未找到該車!\n\n"); ? ? ? ? while( (ret = STout(stack_exchange,b,&tmpcal_ptr4)) != FALSE)? ? ? ? ? { ? ? ? ? ? ? STinsert(stack_park, b,tmpcal_ptr4 ); ? ? ? ? } ? ? ? ? return FAILURE; ? ? } else? ? ? { ? ? ? ? ret = exchange(all_car , b ,"出場了"); ? ? ? ? ret = exchange1(all_car ,b ,tmpcal_ptr3); ? ? ? ? while( (ret = STout(stack_exchange,b,&tmpcal_ptr4)) != FALSE)? ? ? ? ? { ? ? ? ? ? ? STinsert(stack_park, b,tmpcal_ptr4 ); ? ? ? ? } ? ? ? ? if((ret = LQout(queue_wait, c))!=FALSE)? ? ? ? ? { ? ? ? ? ? ? time(&tmpcal_ptr4); ? ? ? ? ? ? STinsert(stack_park, c,tmpcal_ptr4); ? ? ? ? ? ? ret = exchange(all_car , c ,"在場中"); ? ? ? ? ? ? ret = exchange2(all_car ,c ,tmpcal_ptr3); ? ? ? ? } ? ? } ? ? return SUCCESS; } /*//////////////////////////////////// 尋找車輛函數(shù) 功能:在存放信息的鏈表中尋找車輛。 入參:無 返回值:SUCCESS/FAILURE ////////////////////////////////////*/ int find_car()? { ? ? int len; ? ? char a[12]; ? ? double cost; ? ? struct tm *time1; ? ? struct tm *time2; ? ? time_t timenow; ? ? A_INFO *k; ? ? int ret; ? ? printf("請輸入你要查詢的車牌:\n"); ? ? gets(a); ? ? len = LLfind(all_car, a); ? ? if(len == FALSE)? ? ? { ? ? ? ? printf("未來過\n\n"); ? ? ? ? return FAILURE; ? ? } ? ? ret = LLget(all_car, len, &k); ? ? time1 = local_time(&k->time1); ? ? if(time1 != NULL) ? ? { ? ? printf("車牌:%s\n狀態(tài):%s\n入場時間:%d:%d:%d\n",k->name,k->sit, time1->tm_hour, time1->tm_min, time1->tm_sec); ? ? time2 = local_time(&k->time2); ? ? if(time2 != NULL)? ? ? { ? ? ? ? cost = difftime(k->time2,k->time1); ? ? ? ? printf("出場時間:%d:%d:%d\n", time2->tm_hour, time2->tm_min, time2->tm_sec); ? ? ? ? printf("停車時間:%f\n秒",cost); ? ? } else? ? ? { ? ? ? ? time(&timenow); ? ? ? ? cost = difftime(timenow,k->time1); ? ? ? ? printf("出場時間:未出場\n停車時間:%f秒\n",cost); ? ? ? ? return 0; ? ? } ? ? } ? ? else ? ? ? ? printf("等候中"); ? ? return SUCCESS; } /*//////////////////////////////////// 查看出入登記函數(shù) 功能:遍歷打印存放所有信息的鏈表 入參:無 返回值:SUCCESS/FAILURE ////////////////////////////////////*/ int record_all()? { ? ? LLtra(all_car); ? ? return SUCCESS; } /*//////////////////////////////////// 查看停車場內車輛信息函數(shù) 功能:遍歷打印存放停車場內信息的數(shù)組 入參:無 返回值:SUCCESS/FAILURE ////////////////////////////////////*/ int car_park()? { ? ? int i; ? ? double cost; ? ? struct tm *time1; ? ? time_t timenow; ? ? for (i=0; i<stack_park->top+1; i++)? ? ? { ? ? ? ? time1 = local_time(&stack_park->date[i].time1); ? ? ? ? printf("車牌:%s ? ? 入場時間:%d:%d:%d",stack_park->date[i].name, time1->tm_hour, time1->tm_min, time1->tm_sec); ? ? ? ? time(&timenow); ? ? ? ? cost = difftime(timenow,stack_park->date[i].time1); ? ? ? ? printf(" ?未出場,停車時間為:%f秒\n\n",cost); ? ? ? ? printf("-------------------------------------------------------------------------------------------------------------\n"); ? ? } ? ? return SUCCESS; } /*//////////////////////////////////// 查看等候隊列內車輛信息函數(shù) 功能:遍歷打印等候隊列中車輛 入參:無 返回值:SUCCESS/FAILURE ////////////////////////////////////*/ int Car_wait()? { ? ? CAR_W *p; ? ? p = queue_wait->front; ? ? while(p->next)? ? ? { ? ? ? ? p=p->next; ? ? ? ? printf("車牌:%s\n\n",p->name); ? ? } ? ? return SUCCESS; } /*//////////////////////////////////// 初始化函數(shù) 功能:初始化 入參:無 返回值:SUCCESS/FAILURE ////////////////////////////////////*/ int park_init()? { ? ? STinit(&stack_park); ? ? STinit(&stack_exchange); ? ? LQinit(&queue_wait); ? ? LLinit(&all_car); ? ? return SUCCESS; } /*//////////////////////////////////// 時間函數(shù) 功能:轉換成當?shù)貢r間 入參:time_t *tmpcal_ptr(等待轉換的時間) 返回值:time_local(當?shù)貢r間) ////////////////////////////////////*/ struct tm * local_time(time_t *tmpcal_ptr)? { ? ? struct tm *time_local = NULL; ? ? time_local = localtime(tmpcal_ptr); ? ? //轉換成當?shù)貢r間 ? ? return time_local; }
park_u.h
#ifndef _PARK_H #define _PAEK_H #include <time.h> #include <stdio.h> #define SUCCESS 10000 #define FAILURE 10001 #define TRUE 10002 #define FALSE 10003 #define SIZE 6 ? //車場大小 /*汽車信息,節(jié)點結構體 */ struct car_info? { ? ? char name[10]; ? ? // ?車牌 ? ? time_t time1; ? ? // ?入場時間 ? ? time_t time2; ? ? // ?出場時間 } ; typedef struct car_info CAR_I; /*停放,讓路順序棧結構體 需要的參數(shù)為 :車牌號,出場時間,入場時間 配套的子函數(shù) :初始化,判斷是否為空,出棧,入棧,查棧*/ struct sequencestack? { ? ? int top; ? ? CAR_I *date; } ; typedef struct sequencestack ST; /*隊列節(jié)點結構體 需要的參數(shù)為 :車牌號,下一個節(jié)點地址 */ struct car_wait? { ? ? char name[10]; ? ? struct car_wait *next; } ; typedef struct car_wait CAR_W; /*等候鏈式隊列結構 需要的參數(shù)為 :頭指針,尾指針 配套的子函數(shù) :初始化,出隊列,入隊列 */ struct linkqueue? { ? ? CAR_W *front; ? ? CAR_W *rear; } ; typedef struct linkqueue LQ; /*存放所有信息的鏈表 需要的參數(shù)為:車牌號,出入廠狀態(tài),入場時間,出場時間,下一個節(jié)點的地址 需要的函數(shù)為:初始化,入鏈表,查找數(shù)據(jù),遍歷打印 */ struct all_info? { ? ? char name[10]; ? ? char sit[10]; ? ? time_t time1; ? ? // ?入場時間 ? ? time_t time2; ? ? // ?出場時間 ? ? struct all_info *next; } ; typedef struct all_info A_INFO; int car_in(); int car_out(); int find_car(); int record_all(); int car_park(); int Car_wait(); int STinit(ST **q); int STempty(ST *q); int STinsert(ST *q, char na[] ,time_t time); int STout(ST *q,char *a, time_t *time); int LQinit(LQ **q); int LQinsert(LQ *q, char na[]); int LQout(LQ *q, char *a); int LLinit(A_INFO **q); int LLinsert(A_INFO *q, int n, char na[], char s[], time_t timeA, time_t timeB); int LLfind(A_INFO *q, char na[]); int LLget(A_INFO *q, int n, A_INFO **k); int LLtra(A_INFO *q); int parkadd(CAR_I car[], char na[], time_t time); int parkdel(CAR_I car[] ,char na[],time_t time); int print(CAR_I car[]); int park_init(); struct tm * local_time(time_t *tmpcal_ptr); int exchange(A_INFO *q, char na[], char s[]); #endif
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C語言二叉樹常見操作詳解【前序,中序,后序,層次遍歷及非遞歸查找,統(tǒng)計個數(shù),比較,求深度】
這篇文章主要介紹了C語言二叉樹常見操作,結合實例形式詳細分析了基于C語言的二叉樹前序,中序,后序,層次遍歷及非遞歸查找,統(tǒng)計個數(shù),比較,求深度等相關操作技巧與注意事項,需要的朋友可以參考下2018-04-04C++實現(xiàn)LeetCode(149.共線點個數(shù))
這篇文章主要介紹了C++實現(xiàn)LeetCode(149.共線點個數(shù)),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內容,需要的朋友可以參考下2021-07-07C++類模板實戰(zhàn)之vector容器的實現(xiàn)
本文我們將做一個類模板實戰(zhàn)-手寫精簡版vector容器。讓我們自己封裝一個數(shù)組類,可以適應基本數(shù)據(jù)類型和自定義數(shù)據(jù)類型,感興趣的可以了解一下2022-07-07