Programming requires writing a simple train ticketing system in C language, with the main functions implemented as:
Enter shift information
Browse shift information
Query by shift number
Query by terminal
Sort by number of remaining tickets
Ticket sale
Refund ticket
Update shift information
Exit the system
All shift information is saved in a file, and the sorted ones are saved in (.dat is a binary file).
During the writing process, I think it is worth exploring the state of the train. Here we assume that the train has four main states:
1. Not departed
2. Departed
3. Stop ticket inspection
4. Stop refunding
In the program, the idea is to convert the string representing the departure time into an integer, and then compare the size with the current time of the system, mainly using if to judge various situations. Among them, atime represents the integer number of departure time, and btime represents the integer number of system time. The specific implementation is as follows:
if(atime<=btime) //It has already leftreturn 1; if(((atime-btime<=30)&&(atime-btime>5)&&(atime/100==btime/100))||(((atime%100+(60-btime%100))<=30)&&(atime%100+(60-btime%100))>5&&(atime/100-btime/100==1))) // Within half an hour before departure, the ticket refund will be stopped, % means the remaining amount will be withdrawn.return 2; if(((atime-btime<=5)&&(atime/100==btime/100))||((atime%100+(60-btime%100)&&(atime/100-btime/100==1))<=5)) //Ticket inspection stops within five minutes before departurereturn 3; return 0; //Can apply for a refund
When judging the refund, if the number of hours of the two times is the same, then if the difference between their minutes is within 30, that is, within half an hour, or the hour of the departure time and the hour of the system time is one, and the number of minutes of the departure time is less than 30, and the minutes of the system time is greater than 30, then the difference between them is within 30, which means that the refund will be stopped.
The judgment that the ticket check is roughly the same as the above idea.
For reference research, post the code:
#include<> #include<> #include<> #include<> #include "" //Ship Information Table#define SIZELIMIT 10 //The number of characters for specific information per shift is limited to less than 10#define MAXNUM 1000 //Set only 1000 train information can be entered at mosttypedef struct cardbase //Define a structure about shift information, named CardBase{ char C_ID[SIZELIMIT];//Ship timeschar C_TIME[SIZELIMIT];//Departure timechar C_ANAME[SIZELIMIT];//First pointchar C_BNAME[SIZELIMIT];// Terminalchar C_USETIME[SIZELIMIT];//Driving timechar C_MAXNUM[SIZELIMIT];//Rated load capacitychar C_REMAINNUM[SIZELIMIT];//The number of remaining tickets}CardBase; int cbNum=0;//Record the number of shiftsCardBase cBList[MAXNUM];//Ship list//Read shift informationvoid readcardbasefile() { FILE *fp; //Create a file if it fails to open the fileif((fp=fopen("./","r"))==NULL) { //Create a file for the first timeif ((fp=fopen("./","w"))==NULL) { exit(0); //return} else { fclose(fp); } return ; } /*File position pointer moves to the end of the file*/ fseek(fp,0,2);//Relocate the internal position pointer of the file/* int fseek( FILE *stream, long offset, int origin ); The first parameter stream is a file pointer The second parameter offset is an offset, a positive number represents a positive offset, and a negative number represents a negative offset The third parameter origin sets where to start offset in the file, and may be taken as: SEEK_CUR, SEEK_END or SEEK_SET SEEK_SET: Start of the file SEEK_CUR: Current location SEEK_END: end of file Among them, SEEK_SET, SEEK_CUR and SEEK_END are 0, 1 and 2 in turn. In short: fseek(fp,100L,0); move the stream pointer to 100 bytes from the beginning of the file; fseek(fp,100L,1); move the stream pointer to 100 bytes from the current location of the file; fseek(fp,-100L,2); return the stream pointer to 100 bytes from the end of the file. */ if (ftell(fp)>0)//The file is not empty/*ftell function is used to obtain the offset number of bytes of the current position of the file position pointer relative to the head of the file That is to get the number of bytes contained in the file. If it is greater than 0, it means that the file is not empty*/ { //Move the file position pointer to the file startrewind(fp); char buff[10]={0}; for (cbNum=0;!feof(fp) && fread(&cBList[cbNum],sizeof(CardBase),1,fp);cbNum++) /*For the feof function, if the end of the file is encountered, the value of the function feof(fp) is a non-zero value, otherwise it is 0. That is, if the file ends, !feof(fp) is 0, and the loop will be jumped out. For fread function, it is to read data from a file stream. If the call succeeds, return the number of items actually read. If unsuccessful or read to the end of the file, return 0*/ fgets(buff,10,fp); /*char *fgets(char *buf, int bufsize, FILE *stream); Read data from the file structure pointer stream, one line at a time. The read data is saved in the character array pointed to by buf. Read up to bufsize-1 character (bufsize character '\0') at a time. If the line in the file is less than bufsize characters. Then the line is finished. If the number of characters in this line (including the last newline) exceeds bufsize-1, fgets only returns an incomplete line. However, the buffer always ends with NULL characters, and the next call to fgets continues to read the line. The function will return buf successfully, or will return NULL if it fails or reads to the end of the file. Therefore, we cannot directly judge whether the function terminates with an error by the return value of fgets. We should use the feof function or the ferror function to judge. */ fclose(fp); } else { fclose(fp); } return; } //Save shift informationvoid writecardbasefile() { int i; FILE *fp; if ((fp=fopen("./","w"))==NULL) { printf("System Error"); } char buff[10]={0}; strcpy(buff,"\r\n"); for (i=0;i<cbNum;i++) { if (fwrite(&cBList[i],sizeof(CardBase),1,fp)!=1) { printf("System Error"); } if (fwrite(buff,2,1,fp)!=1) { printf("System Error"); } } fclose(fp); } //Save sorting informationvoid writesortfile() { int i; FILE *fp; if ((fp=fopen("./","w"))==NULL) { printf("System Error"); } char buff[10]={0}; strcpy(buff,"\r\n"); for (i=0;i<cbNum;i++) { if (fwrite(&cBList[i],sizeof(CardBase),1,fp)!=1) { printf("System Error"); } if (fwrite(buff,2,1,fp)!=1) { printf("System Error"); } } fclose(fp); } //Return after printing and inputvoid printReturn(char *info) { printf("\n\n\t %s",info); fflush(stdin);//Clear the input buffer, usually to ensure that it does not affect subsequent data reading.getchar(); } //Input informationvoid setInfo(char pinfo[1024],char desinfo[]) { printf("\n\t%s:",pinfo); fflush(stdin); scanf("%s",desinfo); } //System initializationvoid initsystem() { readcardbasefile(); }; //Enter shiftsvoid infoinput() { setInfo("Ship",cBList[cbNum].C_ID); setInfo("Departure time(24Hourly system)",cBList[cbNum].C_TIME); setInfo("First Site",cBList[cbNum].C_ANAME); setInfo("Terminal Station",cBList[cbNum].C_BNAME); setInfo("Driving time",cBList[cbNum].C_USETIME); setInfo("Rated Load",cBList[cbNum].C_MAXNUM); setInfo("Number of remaining tickets",cBList[cbNum].C_REMAINNUM); cbNum++; writecardbasefile(); printReturn("\n\tEnter successful, Enter key returns"); }; //Time comparisonint timecmp(char A[10]) { //Convert train time to integerchar tempa[10]={0}; int ta=0; int i; for(i=0;i<strlen(A);i++) if(A[i]!=':'&&A[i]!=':') //Chinese and English input of colon{ tempa[ta]=A[i]; ta++; } int atime=atoi(tempa);//Turn string into integer//Get system timechar tempb[10]={0}; time_t t = time(0); strftime( tempb, 10, "%H%M",localtime(&t) ); int btime=atoi(tempb); //Compareif(atime<=btime) //It has already leftreturn 1; if(((atime-btime<=30)&&(atime-btime>5)&&(atime/100==btime/100))||(((atime%100+(60-btime%100))<=30)&&(atime%100+(60-btime%100))>5&&(atime/100-btime/100==1))) // Within half an hour before departure, the ticket refund will be stopped, % means the remaining amount will be withdrawn.return 2; if(((atime-btime<=5)&&(atime/100==btime/100))||((atime%100+(60-btime%100)&&(atime/100-btime/100==1))<=5)) //Ticket inspection stops within five minutes before departurereturn 3; return 0; //You can apply for a refund} //Browse all shiftsvoid queryallinfo() { printf("Ship Information\n"); printf("Flight Departure Time Starting Station Terminal Driving Time Rated Load Number of Remaining Tickets Status\n"); int i; for(i=0;i<cbNum;i++) { char temp[20]={0}; strcpy(temp,"Not Departure"); if(1==timecmp(cBList[i].C_TIME)) strcpy(temp,"Departed"); if(2==timecmp(cBList[i].C_TIME)) strcpy(temp,"Stop refunding"); if(3==timecmp(cBList[i].C_TIME)) strcpy(temp,"Stop ticket checking"); printf("%-010s%-010s%-010s%-010s%-010s%-010s%-010s%s\n", cBList[i].C_ID,cBList[i].C_TIME, cBList[i].C_ANAME,cBList[i].C_BNAME,cBList[i].C_USETIME, cBList[i].C_MAXNUM,cBList[i].C_REMAINNUM,temp ); } printReturn("\n\tEnter key returns"); }; //Check the route through the shift numbervoid queryinfobyID() { char ID[20]={0}; setInfo("Enter shift number",ID); int i; for( i=0;i<cbNum;i++) { if(strcmp(cBList[i].C_ID,ID)==0) { printf("Ship Information\n"); printf("Flight Departure Time Starting Station Terminal Driving Time Rated Load Number of Remaining Tickets\n"); printf("%-010s%-010s%-010s%-010s%-010s%-010s%-010s\n", cBList[i].C_ID,cBList[i].C_TIME, cBList[i].C_ANAME,cBList[i].C_BNAME,cBList[i].C_USETIME, cBList[i].C_MAXNUM,cBList[i].C_REMAINNUM ); printReturn("\n\tEnter key returns"); return; } } printReturn("\n\tSpecify information does not exist, the Enter key returns"); }; //Check the route through the terminal stationvoid queryinfobyBNAME() { char Name[20]={0}; setInfo("Enter the terminal",Name); int i; for(i=0;i<cbNum;i++) { if(strcmp(cBList[i].C_BNAME,Name)==0) { printf("Ship Information\n"); printf("Flight Departure Time Starting Station Terminal Driving Time Rated Load Number of Remaining Tickets\n"); printf("%-010s%-010s%-010s%-010s%-010s%-010s%-010s\n", cBList[i].C_ID,cBList[i].C_TIME, cBList[i].C_ANAME,cBList[i].C_BNAME,cBList[i].C_USETIME, cBList[i].C_MAXNUM,cBList[i].C_REMAINNUM ); printReturn("\n\tEnter key returns"); return; } } printReturn("\n\tSpecify information does not exist, the Enter key returns"); }; //Sort by number of remaining tickets Savevoid sortSave() { //Bubbling sortint i,j; for(i=0;i<cbNum;i++) for(j=0;j<cbNum-i-1;j++) { if(atoi(cBList[j].C_REMAINNUM)<atoi(cBList[j+1].C_REMAINNUM)) { CardBase temp=cBList[j]; cBList[j]=cBList[j+1]; cBList[j+1]=temp; } } //Print sorting resultsqueryallinfo(); //Save the sorting resultwritesortfile(); }; //Ticket salesvoid sale() { char ID[20]={0}; setInfo("Enter shift number",ID); int i; for(i=0;i<cbNum;i++) { if(strcmp(cBList[i].C_ID,ID)==0) { if(cBList[i].C_REMAINNUM==0) { printReturn("\n\tThere are insufficient remaining tickets, please return to return"); return; } //Reducedint temp=atoi(cBList[i].C_REMAINNUM)-1; if(temp<0) temp=0;//Make sure that the remaining votes are not negative_itoa(temp,cBList[i].C_REMAINNUM,10); //Save to filewritecardbasefile(); printf("Ship Information\n"); printf("Flight Departure Time Starting Station Terminal Driving Time Rated Load Number of Remaining Tickets\n"); printf("%-010s%-010s%-010s%-010s%-010s%-010s%-010s\n", cBList[i].C_ID,cBList[i].C_TIME, cBList[i].C_ANAME,cBList[i].C_BNAME,cBList[i].C_USETIME, cBList[i].C_MAXNUM,cBList[i].C_REMAINNUM ); printReturn("\n\tTicket sale is successful, return to return"); return; } } printReturn("\n\tThe specified shift does not exist, the Enter key returns"); }; //Refund ticketvoid back() { char ID[20]={0}; setInfo("Enter shift number",ID); int i; for(i=0;i<cbNum;i++) { if(strcmp(cBList[i].C_ID,ID)==0) { //The remaining tickets increaseint temp=atoi(cBList[i].C_REMAINNUM)+1; _itoa(temp,cBList[i].C_REMAINNUM,10); //Save to filewritecardbasefile(); printf("Ship Information\n"); printf("Flight Departure Time Starting Station Terminal Driving Time Rated Load Number of Remaining Tickets\n"); printf("%-010s%-010s%-010s%-010s%-010s%-010s%-010s\n", cBList[i].C_ID,cBList[i].C_TIME, cBList[i].C_ANAME,cBList[i].C_BNAME,cBList[i].C_USETIME, cBList[i].C_MAXNUM,cBList[i].C_REMAINNUM ); printReturn("\n\t\tRefund successfully, return to return"); return; } } printReturn("\n\tThe specified shift does not exist, the Enter key returns"); }; //Update train number informationvoid update() { int flag; char ID[20]={0}; setInfo("Please enter the number of trains you want to delete or modify (please make sure the number of trains is entered correctly)",ID); do { printf("\n\tClick the following prompt to update the train number information (please press 0 if you exit the modification):"); printf("\n\t1: Delete the train number; 2: Modify the departure time; 3: Modify the starting point; 4: Modify the end point;"); printf("\n\t5: Modify driving time; 6: Modify the rated load; 7: Modify the remaining tickets; 0: Exit the modification;"); printf("\n\tPlease select:"); scanf("%d",&flag); int i; CardBase temp; for(i=0;i<cbNum;i++) { if(strcmp(cBList[i].C_ID,ID)==0) { if(flag==1) //Delete the train number information{ int j; for(j=i;j<cbNum;j++) cBList[j]=cBList[j+1];//The following data covers the previous datacbNum--;//Reduce the number of trains by one//Save to filewritecardbasefile(); } if(flag==2) //Modify the departure time{ setInfo("Please enter a new departure time",cBList[i].C_TIME); temp=cBList[i]; //Save to filewritecardbasefile(); } if(flag==3) //Modify the starting point{ setInfo("Please enter a new starting site",cBList[i].C_ANAME); temp=cBList[i]; //Save to filewritecardbasefile(); } if(flag==4) //Modify the terminal{ setInfo("Please enter a new terminal",cBList[i].C_BNAME); temp=cBList[i]; //Save to filewritecardbasefile(); } if(flag==5) //Modify driving time{ setInfo("Please enter a new driving time",cBList[i].C_USETIME); temp=cBList[i]; //Save to filewritecardbasefile(); } if(flag==6) //Modify the rated load capacity{ setInfo("Please enter the new rated load",cBList[i].C_MAXNUM); temp=cBList[i]; //Save to filewritecardbasefile(); } if(flag==7) //Modify the remaining ticket number{ setInfo("Please enter the new remaining ticket count",cBList[i].C_REMAINNUM); temp=cBList[i]; //Save to filewritecardbasefile(); } } } //After the change is completed, the updated shift information will not be displayed. If the train is deleted, the update information will be displayed. If the flag is not equal to 0, the update information will be displayed.if(flag!=1&&flag!=0) { printf("Updated shift information\n"); printf("Flight Departure Time Starting Station Terminal Driving Time Rated Load Number of Remaining Tickets\n"); printf("%-010s%-010s%-010s%-010s%-010s%-010s%-010s\n", temp.C_ID,temp.C_TIME, temp.C_ANAME,temp.C_BNAME,temp.C_USETIME, temp.C_MAXNUM,temp.C_REMAINNUM ); } }while(flag); printReturn("\n\tComplete the update of the train number information, return the Enter key"); }; void mainmenu() { while(1) { char select; do{ system("cls");//Clear the screenprintf("\n\t ╭═════════■□■□═══╮"); printf("\n\t│ Train Flight System │"); printf("\n\t╰═══■□■□══════════╯"); printf("\n\t ┌────────────────┐"); printf("\n\t │1. Enter shift │"); printf("\n\t │2. View all shifts │"); printf("\n\t │3. Check the route through the shift number │"); printf("\n\t │4. Check routes through the terminal │"); printf("\n\t │5.Sort Save │"); printf("\n\t │6. Ticket Sales │"); printf("\n\t │7.Refund │"); printf("\n\t │8.Update train number information │"); printf("\n\t │0.Login │"); printf("\n\t └────────────────┘"); printf("\n\tPlease select:"); fflush(stdin);//Clear the input buffer, usually to ensure that it does not affect subsequent data reading.select=getchar();//Waiting for the user to enter data}while (select<'0'||select>'8'); switch(select) { case '0':exit(0);break; case '1':infoinput();break; case '2':queryallinfo();break; case '3':queryinfobyID();break; case '4':queryinfobyBNAME();break; case '5':sortSave();break; case '6':sale();break; case '7':back();break; case '8':update();break; } } } //Main functionint main() { initsystem(); //System initializationwhile(1) { mainmenu(); } }
There must be many problems in the program. I still have a lot to learn about calling system functions and I am not very skilled in file operation. Everyone is welcome to discuss and communicate together.