In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to use C language to realize a simple parking lot management system". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how to use C language to achieve a simple parking management system"!
Problem description:
There is only one narrow passageway for n cars in the parking lot, and there is only one gate for cars to enter and exit. Cars in the parking lot are arranged from north to south according to the order of arrival time (the gate is at the southernmost end, and the first car to arrive is parked at the northernmost end of the parking lot). If the parking lot is full of n cars, then the later cars can only wait on the sidewalk outside the door, and once there is a car leaving, the first car on the sidewalk can enter. When a car in the parking lot is about to leave, the vehicle entering after it must first leave the yard to make way for it. When the car leaves the gate, other vehicles will enter the parking lot in the original order. Each car parked in the parking lot must pay a fee according to the length of time it stays when it leaves the parking lot.
According to the title, the functions of this system are as follows:
(1) to design the structure of the car park. (the problem is analyzed as a stack, because there is only one door so that it is like the stack learned in the data structure, but because I want to get out of the stack and enter the stack, I will simplify the stack to a linked list.)
(2) the structure waiting outside the garage when the garage is full. Just like queuing, first come in first and then go in later, only enter from one end and exit from the other.
(3) the structure of the vehicle, what information should a car have? First of all, it is necessary to have the license plate number of the car, which can be stored in a character array (because the license plate is not necessarily all numbers, there may also be Chinese characters, English letters, etc.). You need to know the entry time and exit time (how to get it will be discussed below).
Algorithm description:
1. Just begin to define the type of structure, such as the type of car, the type in the garage, the type of waiting outside the garage.
2. Declare the function to be used:
Void menu (Stack* cheku,SequenQueue* paidui); / / start menu / / is to display the menu function / / queue related operations SequenQueue* InitQueue (); / / apply for an empty team int EmptyQueue (SequenQueue* Q); / / judge the team empty int FullQueue (SequenQueue* Q); / / judge the team full int EnQueue (SequenQueue* Q, ElemType * e); / / join the team int DeQueue (SequenQueue* Q, ElemType * e); / / exit Stack* build () / / build linked list int fullstack (Stack * cheku); / / check linked list full void tingche (Stack * cheku,SequenQueue* paidui); / / parking function void likai (Stack * cheku,SequenQueue* paidui); / / leave function void chakan (Stack * cheku,SequenQueue* paidui); / / check garage parking function
3. Some instructions that may not be understood.
(1)。 The function to get time, write the header file # include, define a variable starttime,starttime=time (NULL) of type time_t; that is, get the number of seconds from January 1, 1970 to the current, define a character array tmp2, strftime (tmp2,sizeof (tmp2), "% Y-%m-%d% H:%M:%S", localtime (& Q-> a.starttime)) With this sentence, you can save the current time of the year, month, day, minute and second into a character array, puts (tmp2), and get the current time.
(2)。 The Sleep function is used in the code, the function of this function is delay, and a header file, # include, is used in conjunction with (3).
(3). (2) mentioned in the system ("cls"), its function is to clear the screen, (2) the function is to show the results to the user. Therefore, if the two work together, we can get a wonderful effect.
Program code: # include # define TRUE 1#define FALSE 0#define MAXSIZE 1024static int maxsize;typedef char ElemType;typedef struct {char num [10]; / license plate time_t starttime,endtime;// enters launch time} car; / / car typedef struct Stack {int top; car a; struct Stack * next;} Stack;typedef struct {char a [10];} dat Typedef struct {/ / queue structure definition dat data [MAXSIZE]; int front; int rear;} SequenQueue;void menu (Stack * cheku,SequenQueue* paidui); / / start menu SequenQueue* InitQueue (); / / apply for an empty team int EmptyQueue (SequenQueue* Q); / / judge a team empty int FullQueue (SequenQueue* Q); / / judge a full team int EnQueue (SequenQueue* Q, ElemType * e); / / join the queue int DeQueue (SequenQueue* Q, ElemType * e) / / Stack* build (); / / build linked list void tingche (Stack* cheku,SequenQueue* paidui); int fullstack (Stack* cheku); void likai (Stack* cheku,SequenQueue* paidui); void chakan (Stack* cheku,SequenQueue* paidui); int main () {Stack* cheku=build (); SequenQueue* paidui=InitQueue (); printf ("Please enter maximum garage capacity:"); scanf ("% d", & maxsize); system ("cls"); menu (cheku,paidui) Return 0;} void menu (Stack * cheku,SequenQueue* paidui) {printf ("* Welcome to park! *\ n"); printf ("* Please choose a *\ n"); printf ("* 1: park. Printf ("* 2: leave. *\ n ") printf (" * 3: view. *\ n ") printf (" * 4: exit. *\ n "); int option; scanf ("% d ", & option); system (" cls "); switch (option) {case 1: {tingche (cheku,paidui); menu (cheku,paidui); break } case 2: {likai (cheku,paidui); menu (cheku,paidui); break;} case 3: {chakan (cheku,paidui); menu (cheku,paidui); break } case 4: {printf ("* Welcome to use it again, thank you! *\ n "); break;} default: {printf (" * Please enter the correct instruction! *\ n "); Sleep (1000); menu (cheku,paidui); system (" cls "); break;} int fullstack (Stack * cheku) {if (cheku- > topfront = Q-> rear = 0; return Q;} int DeQueue (SequenQueue* Q, ElemType * e) {if (EmptyQueue (Q)) return FALSE Else {strcpy (eJing Q-> data [Q-> front] .a); Q-> front= (Q-> front+1)% MAXSIZE; return TRUE;}} int EnQueue (SequenQueue* Q, ElemType * e) {if (FullQueue (Q)) {printf ("there are too many vehicles waiting, please come again next time"); return FALSE } strcpy (Q-> front [Q-> front] .a, e); Q-> rear = (Q-> rear+1)% MAXSIZE; return TRUE;} int FullQueue (SequenQueue* Q) {if ((Q-> rear+1)% MAXSIZE==Q- > front) {return TRUE;} else {return FALSE;} int EmptyQueue (SequenQueue* Q) {if (Q-> front = = Q-> rear) return TRUE; else return FALSE } Stack* build (Stack* cheku,SequenQueue* paidui) {Stack* a; a = (Stack*) malloc (sizeof (Stack)); a-> top=-1; return a;} void tingche (Stack* cheku,SequenQueue* paidui) {Stack* p; p = (Stack*) malloc (sizeof (Stack)); printf ("Please enter license plate number\ n"); fflush (stdin); gets (p-> a.num) If (fullstack (cheku)) {p-> next=cheku- > next; cheku- > next=p; p-> a.starttime=time (NULL); cheku- > top++; printf ("parking succeeded\ n"); Sleep (1000); system ("cls");} else {printf ("Please wait at the door when the garage is full") EnQueue (paidui,p- > a.num); Sleep (1000); system ("cls");}} void likai (Stack * cheku,SequenQueue* paidui) {char m [10]; Stack * pdhoreq; char e [10]; int naughty; paircheku; if (cheku- > top==-1) {printf ("Garage is empty\ n"); Sleep (1000) System ("cls");} else {printf ("Please enter the license plate for departure:\ n"); fflush (stdin); gets (m); while (p-> nextlicense plate'\ 0') {double money; if (strcmp (p-> next- > a. Numm) = = 0) {qroomp-> next; p-> next=q- > next Q-> a.endtime=time (NULL); money= (Q-> a.endtime H:%M:%S Q-> a.starttime) * 0.00139; char tmp1, tmp2 [100]; strftime (tmp1,sizeof (tmp1), "% Y-%m-%d% H:%M:%S", localtime (& Q-> a.endtime)) Strftime (tmp2,sizeof (tmp2), "% Y-%m-%d% H:%M:%S", localtime (& Q-> a.starttime)); printf ("stopping time:% s\ n", tmp2); printf ("departure time:% s\ n", tmp1); printf ("Total ds\ n", Q-> a.endtime Q-> a.starttime) Printf ("charge% .5lf (five yuan per hour)\ n", money); Sleep (3000); free (Q); system ("cls"); cheku- > top--; payment; if (EmptyQueue (paidui) = = 0) {DeQueue (paidui,e) Stack * d = (Stack *) malloc (sizeof (Stack)); strcpy (d-> a.numrecoe); d-> a.starttime=time (NULL); d-> next=cheku- > next; cheku- > next=d; cheku- > top++; printf ("the first car waiting has entered the parking lot") Sleep (1000); system ("cls");} break;} pairp-> next;} if (nasty information 0) {printf ("Please try again if the vehicle information is not found"); Sleep (1000); system ("cls") } void chakan (Stack * cheku,SequenQueue* paidui) {if (cheku- > top==maxsize-1) {printf ("the garage is full, there are% d vehicles waiting", (paidui- > rear-paidui- > front+MAXSIZE)% MAXSIZE); Sleep (1000); system ("cls");} else {printf ("% d spaces left in the garage", maxsize-cheku- > top-1) Sleep (1000); system ("cls");}} at this point, I believe you have a deeper understanding of "how to use C language to achieve a simple parking management system". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.