A simple _c language for 12306 train ticketing system based on C language

Source: Internet
Author: User
Tags clear screen fread stdin strcmp ticket

Program design requires the use of C language to write a simple train ticketing system, the main functions to achieve:

Input shift Information

Browse Shift Information

Query by shift number

Query by terminal

Sort save by number of remaining votes

Ticket

Refund

Update shift Information

Exit system

All shift information is saved in the Number.dat file, sorted in the Sort.dat (. dat is a binary file).

In the process of writing I think it is worth examining the state of the train. This assumes that there are four main states of the train:

1. Not on the grid

2. Already on the grid

3. Stop check-in

4. Stop refund

In the program, the idea is to convert the string representing the departure time into the integer type, and then compare the time with the system now, mainly using if to judge all kinds of situations. Where Atime represents the starting time of the integer number, btime represents the system time of the integer number, specifically implemented as follows:

if (atime<=btime)//has started return
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 )//distance from the start within half an hour, stop the refund,% said to take more than return
2;
if ((atime-btime<=5) && (atime/100==btime/100) | | ((atime%100+ (60-btime%100) && (atime/100-btime/100==1)) <=5) Stop check-in five minutes before departure return
3;
return 0; Can deal with the purchase refund

When judging the refund, if two hours is the same number, then their number of minutes, if the difference between 30 within half an hour or the time of the grid time and the hour difference between 1, and the number of minutes from the departure time is less than 30, the system time of more than 30 minutes, then they are in the difference between 30, The representative stops the refund at this time.

The ticket is roughly the same as the above thought.

For reference research, post the code:

#include <stdio.h> #include <stdlib.h> #include <conio.h> #include <string.h> #include "time.h" Shift Information Table #define SIZELIMIT 10//frequency of the specific information of the number of characters limit within 10 #define MAXNUM 1000//Set up can only input 1000 train information typedef struct CARDBASE//define A Structure for shift information, named Cardbase {char c_id[sizelimit];//shift char c_time[sizelimit];//departure time Char c_aname[sizelimit];//starting point Char C_ bname[sizelimit];//Terminal Char c_usetime[sizelimit];//driving time Char c_maxnum[sizelimit];//rated load char C_remainnum[sizelimit]
;//The number of votes}cardbase; int cbnum=0;//record number of shifts cardbase cblist[maxnum];//shift list/read shift information void Readcardbasefile () {file *fp; 
N ("./number.dat", "R")) ==null {////First run Create file if ((Fp=fopen ("./number.dat", "W")) ==null) {exit (0);//return} else {fclose (FP);
} return;
/* File position pointer moved to end of File/fseek (fp,0,2);//reposition file internal position pointer/int fseek (file *stream, long offset, int origin); The first argument stream is the second parameter of the file pointer offset, the positive offset, the negative offset, and the third parameter origin set the offset from where the file begins, possibly with a value of: Seek_cur, Seek_end, or Seek_set seek_ SET: File opening seek_cur: Current position seek_end: textAt the end of which Seek_set,seek_cur and Seek_end are 0, 1 and 2 in sequence. 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 position of the file; fseek (fp,-100l,2); Return the stream pointer to 100 bytes from the end of the file.
/if (Ftell (FP) >0)//file is not empty/*ftell function is used to get file position pointer the current position relative to the top of the file is the number of bytes that are included in the file, if greater than 0, the file is not empty/{//File position pointer moved to the file start
Rewind (FP);
Char buff[10]={0}; for (cbnum=0;! feof (FP) && fread (&cblist[cbnum],sizeof (cardbase), 1,FP); cbnum++)/* For the feof function, the value of function feof (FP) is a value other than 0 if the file end is encountered
, otherwise it is 0.
That is, if the file ends,!feof (FP) is 0, out of the loop for the Fread function, read the data from a file stream, if the call successfully returns the number of entries actually read, if unsuccessful or read to the end of the file to return to 0*/fgets (BUFF,10,FP);
/*char *fgets (char *buf, int bufsize, FILE *stream); Reads the data from the file-structure pointer stream and reads one row at a time. The read data is saved in an array of characters pointed to by BUF, which reads bufsize-1 characters (the BufSize word Assignments ' ") each time, and ends when the line in the file is less than bufsize characters. If the number of characters in the line (including the last line break) exceeds bufsize-1, Fgets returns only an incomplete row, but the buffer always ends with a null character, and the next call to Fgets continues to read the row.
The function succeeds in returning BUF, failing or reading to the end of the file to return null.
Therefore, we cannot directly judge whether a function is an error by means of the return value of the fgets, and it should be judged by the feof function or the ferror function.
* * fclose (FP);
else {fclose (FP);} return;
//Save shift Information void Writecardbasefile () {int i; FILE *fp
if ((Fp=fopen ("./number.dat", "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 sort Info void Writesortfile () {int i;
FILE *FP;
if ((Fp=fopen ("./sort.dat", "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);
///print and enter to return void Printreturn (char *info) {printf ("\n\n\t%s", info); fflush (stdin);//Empty the input buffer, usually to ensure that the subsequent data reads are not affected.
GetChar ();
}//input information void SetInfo (char Pinfo[1024],char desinfo[]) {printf ("\n\t%s:", pinfo);
Fflush (stdin);
scanf ("%s", Desinfo);
}//System initialization void Initsystem () {readcardbasefile ();}; Entry shift void Infoinput () {setInfo ("Shift", Cblist[cbnum].
C_ID); SetInfo ("Departure time (24 hour system)", Cblist[cbnum].
C_time); SetInfo ("Starting point", CblisT[cbnum].
C_aname); SetInfo ("terminus", Cblist[cbnum).
C_bname); SetInfo ("Journey Time", Cblist[cbnum].
C_usetime); SetInfo ("Rated load", Cblist[cbnum].
C_maxnum); SetInfo ("Number of votes", Cblist[cbnum].
C_remainnum);
cbnum++;
Writecardbasefile ();
Printreturn ("\n\t entry succeeds, return key returns");
}; Time comparison int timecmp (char a[10]) {//Convert train time to integer char tempa[10]={0}; int ta=0; int i; for (I=0;i<strlen (A); i++) if (a[i]!= ': ' &&a[i]!= ': ')//colon Chinese input and English input {tempa[ta]=a[i]; ta++} int Atime=atoi (TEMPA);//To get the system time char tempb[10]={
0};
time_t t = time (0);
Strftime (TEMPB, "%h%m", LocalTime (&t));
int Btime=atoi (TEMPB);
Compare if (atime<=btime)//Already on grid return 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
)//distance from the start within half an hour, stop the refund,% said to take more than return 2; if ((atime-btime<=5) && (atime/100==btime/100) | | ((atime%100+ (60-btime%100) && (atime/100-btime/100==1)) <=5) Stop check-in in five minutes before departure
return 3; return 0; can handle purchase refund}//Browse all shifts void Queryallinfo () {printf ("shift information \ \ \"), printf ("Shift departure time starting point terminal journey time rating load remaining ticket quantity state \ n"); int i; for (i=0 ; i<cbnum;i++) {char temp[20]={0}; strcpy (temp, "not on grid"); if (1==timecmp (cblist[i).
C_time)) strcpy (temp, "Already on the grid"); if (2==timecmp (cblist[i).
C_time) strcpy (temp, "stop refund"); if (3==timecmp (cblist[i).
C_time)) strcpy (temp, "Stop Ticket"); 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\t enter return");
}; Route void Queryinfobyid () {char id[20]={0}; SetInfo ("Input shift number", ID); int i; for (i=0;i<cbnum;i++) {if strcmp (cblist [i]. C_id,id) ==0) {printf ("Shift information \ \"), printf ("Shift departure time starting point terminal journey time rating of the number of remaining votes \ n"); printf ("%-010s%-010s%-010s%-010s%-010s%-0 10s%-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 carriage returnKey return ");
Return
} printreturn ("\n\t specifies that no information exists, return key returns");
}; Check the route through the terminal void Queryinfobybname () {char name[20]={0}; setInfo (input terminal, Name); int i; for (i=0;i<cbnum;i++) {if strcmp (Cblist[i]. C_bname,name) ==0) {printf ("Shift information \ \"), printf ("Shift departure time starting point terminal journey time rating of the number of remaining votes \ 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 enter return");
Return
} printreturn ("\n\t specifies that no information exists, return key returns");
}; Sort saving by number of votes save void Sortsave () {//bubble sort int i,j; for (i=0;i<cbnum;i++) for (j=0;j<cbnum-i-1;j++) {if (atoi). C_remainnum) <atoi (cblist[j+1].
C_remainnum)) {cardbase temp=cblist[j]; cblist[j]=cblist[j+1]; cblist[j+1]=temp;}}
Print sort result queryallinfo ();
Save sort results writesortfile ();
}; ticket void Sale () {char id[20]={0}; SetInfo ("Input shift number", ID); int i; for (i=0;i<cbnum;i++) {if (strcmp). C_id,id) ==0) {if (Cblist[i]. c_remainnum==0) {Printreturn ("\n\tLess than the ticket, return ");
Return }//More votes reduced int temp=atoi (Cblist[i].
C_remainnum)-1; if (temp<0) temp=0;//ensure that the remaining ticket is not a negative number _itoa (temp,cblist[i).
c_remainnum,10);
Save to file Writecardbasefile ();
printf ("Shift information \ n");
printf ("Shift departure time starting point terminal journey time rating of the number of remaining votes)"); 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 ticket success, return returns");
Return
} printreturn ("\n\t specified shift does not exist, enter return");
}; refund void back () {char id[20]={0}; SetInfo ("Input shift number", ID); int i; for (i=0;i<cbnum;i++) {if (strcmp (cblist[i). C_id,id) {//==0 + ticket Increase int temp=atoi (cblist[i).
C_remainnum) +1; _itoa (Temp,cblist[i].
c_remainnum,10);
Save to file Writecardbasefile ();
printf ("Shift information \ n");
printf ("Shift departure time starting point terminal journey time rating of the number of remaining votes)"); 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\t refund success, return");
Return
} printreturn ("\n\t specified shift does not exist, enter return");
}; Update train information void Update () {int flag; char id[20]={0}; SetInfo ("Please enter the train you want to delete or modify (make sure the train is entered correctly)", ID); do {printf ("\n\t" updates the train information by following the prompts (fallback
Please press 0 for the change: ");
printf ("\N\T1: Delete train; 2: Modify Departure time; 3: Modify starting point; 4: Modify terminal;");
printf ("\n\t5: Modify the driving time; 6: Modify the rated load; 7: Modify the number of remaining votes; 0: Exit modification;");
printf ("\n\t Please 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 train information {int J; for (j=i;j<cbnum;j++) cblist[j]=cblist[j+1];//the data that follows overwrites the preceding data cbnum--;//
Car count minus one//save to File Writecardbasefile (); } if (flag==2)//Modify departure time {setInfo ("Please enter a new departure time", Cblist[i].
C_time);
Temp=cblist[i];
Save to file Writecardbasefile (); } if (flag==3)//Modify starting point {setInfo ("Please enter a new starting point", cblist[i).
C_aname);
Temp=cblist[i];
Save to file Writecardbasefile (); } if (flag==4)//Modify terminal {setInfo ("Please enter a new terminal", Cblist[i].
C_bname);
Temp=cblist[i];
Save to file Writecardbasefile (); } if (flag==5)//Modify journey time {SetInfo ("Please enter a new journey time", Cblist[i].
C_usetime);
Temp=cblist[i];
Save to file Writecardbasefile ();if (flag==6)//Modify the rated load {setInfo ("Please enter a new rating load", Cblist[i].
C_maxnum);
Temp=cblist[i];
Save to file Writecardbasefile (); } if (flag==7)//Modify the number of remaining votes {setInfo ("Please enter a new number of remaining votes", Cblist[i].
C_remainnum);
Temp=cblist[i];
Save to file Writecardbasefile (); After the change is completed, the updated shift information is displayed, if it is deleted the train does not show, that is, flag is not equal to 0 when the display of update information if (flag!=1&&flag!=0) {printf ("Updated shift information \ n"); printf ("
Shift departure time starting point terminal journey time rating load remaining votes quantity \ 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\t Complete the Train information update, return key");
}; void MainMenu () {while (1) {char select; do{System ("CLS");/clear Screen printf ("\n\t╭═════════--═══╮"); printf ("\n\t│ Train shift system │
");
printf ("\n\t╰═══--══════════╯");
printf ("\n\t┌────────────────┐");
printf ("\n\t│1. Entry frequency │");
printf ("\n\t│2. Browse all Shifts │");
printf ("\n\t│3." Search route │ via shift);
printf ("\n\t│4. Through the terminal check route │");
printf ("\n\t│5. Sort save │");
printf ("\n\t│6. Ticketing │");
printf ("\n\t│7. Refund │");
printf ("\n\t│8. Update the train information │"); PriNTF ("\n\t│0. Exit │");
printf ("\n\t└────────────────┘");
printf ("\n\t Please select:");
Fflush (stdin);//empties the input buffer, usually to ensure that the subsequent data reads are not affected. Select=getchar ()//Waiting for user input data}while (select< ' 0 ' | |
Select> ' 8 '); Switch (SELECT) {case ' 0 ': Exit (0), break, Case ' 1 ': Infoinput (), break, Case ' 2 ': Queryallinfo (), Break, Case ' 3 ':
Queryinfobyid ();
Case ' 4 ': Queryinfobybname ();
Case ' 5 ': Sortsave ();
Case ' 6 ': sale ();
Case ' 7 ': Back ();
Case ' 8 ': Update (); }}//main function int main () {Initsystem ();//System initialization while (1) {MainMenu ();}}

There are many problems in the program, there is a lot of call system functions need to learn, file operation is not very skilled, welcome to discuss exchanges.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.