main.c//Parking Management system////Created by ding Small not on 13-7-14.//Copyright (c) 2013 Dingxiaowei. All rights reserved.////title: Parking Management System//(1) Management personnel according to the password into the system//(2) Management of parking information (parking number, status) and the rate per minute; (3) Enter the parking information (car number, model, berth location, Parking start time); If the parking space is full to give a hint;//(4) when taking the car, according to the license plate, if not give a hint, need to according to the vehicle parking time automatically calculate the cost and display on the screen. #include <stdio.h> #include <string.h> #include <time.h> #define MAX 20struct stopedcar{Char carnum[20 ]; Car brand char carshap[20];//model int areanum; Berth location number char stopedtime[30]; Start berthing time int h; Record hour int m; Record minutes};int total; Total record number char password[20]; Password struct Stopedcar stopedcar[max];int price=1.0;//default parking unit price for 1¥1m/*************** function declaration ****************************** /void Init ()//initialization, the file does not exist, is established, while recording the number of records in the file int get_menu_choice (); Accept menu select void Menu ()//menu corresponding void Show_menu ();//Display menu file *file_operate (char *mode); File operation type void SET_PSW ()//Set password int psw_check ();//password Authentication int inputonecar (int i);//Add a record to the parking number void inputfile (int i,file *fp) ;//write a record of subscript I to file void Input (*FP);//Add a record int setpri to the management systemCE ()//Set the parking unit price (for example: ¥1for1s) void Getcarbyareanum (FILE *fp);//Take the car according to the parking number/***** the following function is to simplify the function of part work from the above function ************** /void printtitle ();//print head information void Showallcarinfo (); Show all parking information void showonecarinfo (int i); Show parking information float Countmoney (float Danjia)//calculate parking costs based on the incoming price void Readfile (int i,file *FP);//read a record from the file int checknumber ( int areanum_temp); Check the parking number is empty, there is a return number, if the parking space is returned -1char* GetTime (); Get the current time int cost (char *time1,int Danjia);// Calculate the price of parking consumption based on unit price and time difference int Cost2 (int h,int m,int Danjia); void GetCarByAreaNum2 (FILE *fp); int Timeh (); int timem ();/********* The implementation of the function ****************************************///get the current system time char* GetTime () {{time_t now; struct TM *timenow; Time (&now); TimeNow = LocalTime (&now); Char *p = asctime (TimeNow); return p; }}//returns the current hour int Timeh () {int h; time_t now; struct TM *timenow; Time (&now); TimeNow = LocalTime (&now); Char *p = asctime (TimeNow); h=timenow->tm_hour; return h;} Returns the current minute int TImem () {int m; time_t now; struct TM *timenow; Time (&now); TimeNow = LocalTime (&now); Char *p = asctime (TimeNow); m=timenow->tm_min; return m;} Accept menu Select int Get_menu_choice () {int menu_ch; Do {printf ("Enter menu options:"); scanf ("%d", &menu_ch); if ((menu_ch<0) | | (menu_ch>9)) printf ("Input error!"); } while ((menu_ch<0) | | (menu_ch>9)); return menu_ch;} Show menu void Show_menu () {printf (">>>>>>>>>>>>>>> Welcome to use parking Management system << <<<<<<<<<<<<<<\n "); printf ("************************************************\n"); printf ("* 1. New Parking Info | 2. Show all parking Information *\n "); printf ("* 3.) Pickup car by license plate number | 4. According to the parking number to take the car *\n "); printf ("* 5.) Search vehicle information by license plate number | 6. According to the Parking Number inquiry vehicle Information *\n "); printf ("* 7. Set Parking Price | 8. Password set *\n "); printf ("* 9. Active Clear Screen | 0. Exit *\n "); printf ("****************************\ n ");} Clear the screen, there is a problem//void clean ()//{//system ("pause");/System ("CLS");//}//menu response void menu () {while (1) {Show_menu ( ); Switch (Get_menu_choice ()) {Case 1://new parking Information Input (file_operate ("AB")); Binary append write break; Case 2://Displays all parking information showallcarinfo (); Break Case 3://according to the license plate to take the car break; Case 4://According to the parking number to take the car GetCarByAreaNum2 (File_operate ("WB")); Break Case 5://according to the license plate number Inquiry parking information break; Case 6://In accordance with the parking number to query parking information break; Case 7://Set Parking price (for example: ¥1for1s) Setprice (); Break Case 8://Password setting SET_PSW (); Break Case 9://Manual Clear Screen//clear (); printf ("Manual screen is incorrect.) "); Break Case 0://End Program//system ("CLS"); printf ("****************\ n "); printf ("Welcome to use this system \ n"); printf ("**********************\n"); Break Exit (0); Default:break; }}//Print header information void PrintTitle () {printf ("-------------------------------------------------------------\ n"); printf ("Parking number of car license plate number parked at the start time \ \ \ \ n"); printf ("-------------------------------------------------------------\ n");} Initialize, the file does not exist, then establish, and record the number of records in the file void Init () {int II; FILE *FP; total=0; if ((Fp=fopen ("Stopedcar.txt", "RB")) ==null) {Fp=fopen ("Stopedcar.txt", "AB"); Clear (); menu (); else {for (ii=0;feof (FP) ==0;ii++)//feof Check that the file is closed {Readfile (II,FP); } total=ii-1; }//total=ii-1; Fclose (FP); File action type *file_operate (char *mode) {char choice; FILE *FP; do {fflush (stdin);//empty input cache so as not to affect subsequent input if ((Fp=fopen ("Stopedcar.txt", mode) ==null) {puts ("Fail to open the file!"); Puts ("Try again!" ( y/n)? "); scanf ("%c", &choice); else Fp=fopen ("Stopedcar.txt", mode); while (choice== ' y ') | | choice== ' Y '); if (choice== ' n ' | | choice== ' N ') {}//exit (1); Abnormal exit exit (0); system normal exit return (FP);} Reads a record from a file void Readfile (int i,file *fp) {fscanf (FP, "%20s", &stopedcar[i].carnum); FSCANF (FP, "%20s", &stopedcar[i].carshap); FSCANF (FP, "%5d", &stopedcar[i].areanum); FSCANF (FP, "%30s", &stopedcar[i].stopedtime);} Display print a parking information void showonecarinfo (int i) {printf ("%10s%10s%1d%30s \ n", stopedcar[i].carnum,stopedcar[i].carshap,st Opedcar[i].areanum,stopedcar[i].stopedtime);} Show print all parking information void Showallcarinfo ()/* Display print all information */{int i; printf ("%d records: \ n", total); PrintTitle (); for (i=0;i<total;i++) showonecarinfo (i);} Check the parking number is empty, there is a return number, if the parking space is not parked then return -1int checknumber (int areanum_temp) {int i,result=0; for (i=0;i<total;i++) { if (stopedcar[i].areanum==areanum_temp) {result=1; Break } if (result==1) return i; else return-1;} Add a record to the parking number int inputonecar (int i) {int areanum; Do {printf ("Enter the parking number: (e.g. 1)"); scanf ("%d", &areanum); if (areanum<0) printf ("error!\n"); if (Areanum>max) {printf ("not more than%d parking spaces \ n", MAX); } while (areanum<0); The parking space has parking if (CheckNumber (areanum) >0) {printf ("The parking space has been parked!\n"); return 0; //If the parking space is not parked else {stopedcar[i].areanum=areanum; Record the parking number as the current parking number, printf ("Input license plate number (less than chars):"); scanf ("%s", stopedcar[i].carnum); printf ("Vehicle model (for example: baomaX6):"); scanf ("%s", &stopedcar[i].carshap); Char *time=gettime ()///Get current system time Stopedcar[i].h=timeh ()//Log Hour Stopedcar[i].m=timem ()//minutes char *q=s Topedcar[i].stopedtime; strcpy (q, time);//assign current times to car. Stopedtime PrintTitle (); Showonecarinfo (i); Inputfile (I,FP); return 1; }}//writes the record of subscript I to the file void inputfile (int i,file *fp) {fscanf (FP, "%20s", &stopedcar[i].carnum); FSCANF (FP, "%20s", &stopedcar[i].carshap); FSCANF (FP, "%5d", &stopedcar[i].areanum); FSCANF (FP, "%30s", &stopedcar[i].stopedtime);} Add the Record void Input (FILE *fp) {int I to the management system; I=total; if (Inputonecar (i)) {total++; Inputfile (I,FP); Fclose (FP);} Set the parking price int setprice () {printf ("Please set the parking price: (For example: 1¥for 1 m) \ n"); scanf ("%d", &price); printf ("You set the unit price is:%d¥1m\n", prices); return price;} int Cost2 (int h,int m,int danjia) {int money; Gets the current hour and minute int Hnow=timeh (); int Mnow=timem (); Return money= ((hnow-h) *60+ (mnow-m)) *danjia;} Calculate the cost of parking consumption based on unit price and time difference (char *time1,int danjia) {int money; Char forein[2]; Char now[2]; int minu; int sec; int t1,t2; for (int i=14; i<=15; i++,time1++) {forein[i-14]=time1[i]; } minu= (forein[0]-48) *10+ (forein[1]-48); for (int i=17; i<=18; i++,time1++) {forein[i-17]=time1[i]; } sec= (forein[0]-48) *10+ (forein[1]-48); T1=minu*60+sec; Char *p=gettime (); for (int i=14; i<=15; i++,time1++) {forein[i-14]=p[i]; } minu= (forein[0]-48) *10+ (forein[1]-48); for (int i=17; i<=18; i++,time1++) {forein[i-17]=p[i]; } sec= (forein[0]-48) *10+ (forein[1]-48); T2=minu*60+sec; money= (T2-T1) *danjia; return money;} Take vehicle void GetCarByAreaNum2 (FILE *fp) {int I,j,k,no_temp,choice2 According to the parking number; printf ("Enter the parking number of the vehicle to be taken: (for example, 1)"); scanf ("%d", &no_temp); I=checknumber (no_temp); if (i>=0) {printtitle (); Showonecarinfo (i)//According to the parking number to query is the first few printf ("Confirm pick-up.") (1. is/2. No) "); scanf ("%d", &choice2); if (choice2==1) {//char *time1=stopedcar[i].stopedtime;//printf (time1); int h=stopedcar[i].h; int m=stopedcar[i].m; printf ("YouNeed to pay ¥%d parking fees \ n ", Cost2 (H,m,price)); For (J=i j<total; j + +) {strcpy (stopedcar[j].carnum, stopedcar[j+1].carnum); strcpy (Stopedcar[j].carshap, Stopedcar[j+1].carshap); } total--; For (k=0 k<total; k++) {inputfile (k, FP); else printf ("No parking on this parking space \ n");} Take vehicle void Getcarbyareanum (FILE *fp) {int I,j,k,no_temp,choice2 According to the parking number; printf ("Enter the parking number of the vehicle to be taken: (for example, 1)"); scanf ("%d", &no_temp); I=checknumber (no_temp); if (i>=0) {printtitle (); Showonecarinfo (i)//According to the parking number to query is the first few printf ("Confirm pick-up.") (1. is/2. No) "); scanf ("%d", &choice2); if (choice2==1) {char *time1=stopedcar[i].stopedtime; printf (time1); printf ("You need to pay ¥%d parking fee \ n", Cost (Time1,price)); For (J=i j<total; j + +) {strcpy (stopedcar[j].carnum, stopedcar[j+1].carnum); strcpy (Stopedcar[j].carshap, StopeDCAR[J+1].CARSHAP); } total--; For (k=0 k<total; k++) {inputfile (k, FP); else printf ("No parking on this parking space \ n");} Set password void set_psw () {char psw_set[20],psw_check[20],c; unsigned int i; FILE *FP; Do {printf ("You must set password first!\n"); printf ("Enter Password: (less than numbers and key". for end) \ n "); for (i=0; (C=getchar ())!= '. '; i++) {//putchar (' * '); Psw_set[i]=c; } psw_set[i]= '; printf ("-----------------\ n"); printf ("Conform password:"); for (i=0; (C=getchar ())!= '. '; i++) {//putchar (' * '); Psw_check[i]=c; } psw_check[i]= '; printf ("------------\ n"); if (strcmp (Psw_set,psw_check) ==0) {printf ("Set password success!\n"); strcpy (Password,psw_set); else printf ("error!\n"); } while (strcmp (PSW_SET,PSW_CHECk)!=0); Clear (); Fp=fopen ("Password.txt", "WB"); fprintf (FP, "%s", password); Fclose (FP); Password Authentication int Psw_check () {unsigned int i=1,j=1; FILE *FP; Char pword[20],c; if ((Fp=fopen ("Password.txt", "RB")) ==null) {Fp=fopen ("Password.txt", "a"); SET_PSW (); FSCANF (FP, "%s", password); Fclose (FP); Do {printf ("\ninput password:key '.") For end (%d/three times) ", j); for (j=0; (C=getchar ())!= '. '; J + +) {//putchar (' * '); Pword[j]=c; } pword[j]= '; i++; while (strcmp (Pword,password)!=0&&i<=3); if (i<=3) return 1; else {printf ("You have tryed for three times,fail to open the file!\n"); return 0; }}int Main (int argc, const char * argv[]) {if (Psw_check ()) {Init (); Clear (); menu (); return 0;}
This article comes from "The monkey who will move the brick" blog, please be sure to keep this source http://silianbo.blog.51cto.com/6627757/1305983