Train tickets for the first day of this year’s Spring Festival transportation officially went on sale on December 30. Every Spring Festival, many passengers buy a ticket to go home, “Eight Immortals cross the sea and each show their magic power”. However, there are always some passengers “magic power failure”. How to buy train tickets for Spring Festival transportation this year? Why don’t you write your own train booking system.
This is a train ticketing system written a long time ago. It’s very rough. It hasn’t been changed since then. I hope to meet someone and continue to optimize it.
The main functions are: set train number, delete train number, buy ticket, change ticket, refund, query, etc.
#include
#include
#include
#include
#include
#define MAXNUM 10000
#define Num 100
typedef struct train
{
int Train_ num;// Train number
char Train_ end[10];// Terminus
int Train_ price;// Ticket Price
int leftnum;// Number of remaining tickets
}Train_Ticket;
typedef struct person{
int mark;// sign
int snum;// ID number
char sex[10];// Gender of ticket buyer
//char Train_start[8];
char name[10];// Name of buyer
}Person;
typedef struct node
{
Train_Ticket data[MAXNUM];
int len;
}SeqList;
Person pa1[Num],pa2[Num];
void init_Seqlist(SeqList*);
void Sale(SeqList*,FILE*);
void Withdraw(FILE*);
void Inquiry(SeqList*);
void Select(SeqList*,FILE*);
void Delete(SeqList*);
void Init(SeqList*,struct train t);
void Input(SeqList*);
void Exchange(SeqList*,FILE *fp);
int main()
{
SeqList*q;
FILE *fp;
fp=fopen(“train.dat”,”wb”);
q=(SeqList*)malloc(sizeof(SeqList));
int sel;
Input(q);
system(“cls”);
system(“color 3f”);// Change color
system(“cls”);
while(1){
printf(“————————————-\n”);
printf(“—–Welcome to YaiTai trainstation–\n”);
printf(“1– Set Tickets –“);// initialization
printf(“\n2– Book Ticket –“);// Buy a ticket
printf(“\n3– Exchange Ticket –“);// ticket changes
printf(“\n4– Withdraw Ticket –“);// to refund a ticket
printf(“\n5– Delete Ticket –“);// Delete train number
printf(“\n6– Select Ticket –“);// query
printf(“\n7– Inquiry –“);//
printf(“\n8– Exit –“);// sign out
printf(“\n————————————-“);
printf(“\nPlease input your choice:”);
scanf(“%d”,&sel);
getchar();
switch(sel)
{
case 1:
init_Seqlist(q); break;
case 2:
if(q->len==0)
{
printf(“——————\n”);
printf(” No tickets!\n”);
printf(“——————\n”);break;
}else{
Sale(q,fp); break;
}
case 3:
Exchange(q,fp); break;
case 4:
Withdraw(fp);break;
case 5:
Delete(q); break;
case 6:
Select(q,fp); break;
case 7:
Inquiry(q);break;
case 8:
printf(“——————————\n”);
printf(“—Thank you for your use!—\n”);
printf(“——————————\n”);
exit(1);
default : printf(“Enter error!!\n”);
}
Printf (“press enter to return to the main menu \ n”);
getchar();
}
return 0;
}
void Input(SeqList*L)
{
int i;
struct train tra[5]={{206,”beijing”,200,20},
{301,”chengdu”,350,20},
{432,”shangha”,400,20},
{783,”qingdao”,175,20},
{312,”hangzho”,480,20}
};
for(i=0;i<5;i++)
Init(L,tra[i]);
}
void Init(SeqList*L,struct train t)
{
int i=L->len;
L->data[i].Train_num=t.Train_num;
strcpy(L->data[i].Train_end,t.Train_end);
L->data[i].Train_price=t.Train_price;
L->data[i].leftnum=t.leftnum;
L->len++;
}
void init_Seqlist(SeqList*L)
{
int i,d,n;
d=L->len;
Printf (“—————- train number setting ——————— \ n”);
Printf (“please enter the number of trains you want to set:”);
scanf(“%d”,&n);
for(i=d;i
Printf (“please enter the train number:”);
scanf(“%d”,&L->data[i].Train_num);
Printf (“please input train number and terminal:”);
scanf(“%s”,&L->data[i].Train_end);
Printf (“please enter the set fare:”);
scanf(“%d”,&L->data[i].Train_price);
Printf (“please enter the number of remaining tickets:”);
scanf(“%d”,&L->data[i].leftnum);
L->len++;
if((L->len)>1&&ilen-1))
{
Printf (“please enter the off duty train number information \ n”);
}
}
printf(“Initiation finish!\n”);
}
void Sale(SeqList*L,FILE *fp)
{
int i=0,k,a,j;
if((fp=fopen(“train.dat”,”ab+”))==NULL)
{
printf(“Cannot open file!\n”);
exit(1);
}
Printf (“———— ticket buying service ———– \ n”);
Printf (“please enter the number of tickets you want to purchase: \ n”);
scanf(“%d”,&a);
if(a>1){
Printf (“please input the information of% d passengers respectively \ n”, a);
}
else if(a<=1)
{
Printf (“please enter the passenger information \ n”);
}
{
Printf (“please enter your name first:”);
scanf(“%s”,&pa1[i].name);
Printf (“please select gender FM:”);
scanf(“%s”,&pa1[i].sex);
do{
if(strcmp(pa1[i].sex,”F”)!=0 && strcmp(pa1[i].sex,”M”)!=0){
Printf (“gender input error! Please re-enter! \ n”);
scanf(“%s”,&pa1[i].sex);
}
}while(strcmp(pa1[i].sex,”F”)!=0 && strcmp(pa1[i].sex,”M”)!=0);
Printf (please input your ID number first).
scanf(“%d”,&pa1[i].snum);
}
Inquiry(L);
printf(“\nPlease input the Train_number you want to book(1~%d):”);
scanf(“%d”,&k);
char answer[10];
for(i=0;ilen;i++){
if(k==L->data[i].Train_num){
pa1[j].mark=i;// Mark point
Printf (“are you sure you want to buy the train of% d train number (yes or no) \ n”, L – > data [i]. Train_num);
scanf(“%s”,&answer);
do
{
if(strcmp(answer,”yes”)!=0){
Printf (“your input is wrong, please re-enter! \ n”);
scanf(“%s”,&answer);
}
}while(strcmp(answer,”yes”)!=0);
if(strcmp(answer,”yes”)==0){
Printf (“you have paid the ticket price of% d yuan \ n”, L – > data [i]. Train_price);
L->data[i].leftnum–;
}else if(strcmp(answer,”no”)==0){
Printf (“please re select the train number you want to buy \ n”);
}
}
}
printf(“Tickets purchased success!\n”);
}
//printf(“%d\n”,pa1[i].mark);
fwrite((void*)pa1,sizeof(Person),a,fp);
fclose(fp);
}
void Delete(SeqList*L)
{
int n,i,true=0;
SeqList*q;
q=(SeqList*)malloc(sizeof(SeqList));
Printf (“\ n ——————- delete train number according to train number ——————- \ n”);
Printf (“please enter the train number to be deleted \ n”);
scanf(“%d”,&n);
for(i=0;ilen;i++){
if(L->data[i].Train_num==n)
{
true=1;
if(ilen-1)
{
L->data[i].Train_num=L->data[i+1].Train_num;
L->data[i].Train_price=L->data[i+1].Train_price;
L->data[i].leftnum=L->data[i+1].leftnum;
strcpy(L->data[i].Train_end,L->data[i+1].Train_end);L->len–;
printf(“——————–\n”);
Printf (“– the train number information has been deleted — \ n”);
printf(“——————–\n”);
}
if(i==L->len-1)
{
L->len–;
printf(“——————–\n”);
Printf (“– the train number information has been deleted — \ n”);
printf(“——————–\n”);
}
}
}
if(true==0)
{
printf(“No this ticket numbers!\n”);
}
}
void Select(SeqList*L,FILE *fp)
{
SeqList*q;
q=(SeqList*)malloc(sizeof(SeqList));
int i,a,b,true=0;
Printf (“—————— query service ——————— \ n”);
printf(“—————————————-\n”);
Printf (“1. Query by passenger’s name 2. Query by passenger ID number (e.g. “);
printf(“—————————————\n”);
if((fp=fopen(“train.dat”,”rb+”))==NULL)
{
printf(“Canot open file !\n”);
}
fseek(fp,0L,SEEK_END);
int d=ftell(fp)/sizeof(Person);
rewind(fp);
fread((void*)&pa2,sizeof(Person),d,fp);
scanf(“%d”,&a);
switch(a)
{
case 1:
{
char na[20];
Printf (“please enter the passenger’s name: \ n”);
scanf(“%s”,&na);
for(i=0;i
{
if(strcmp(pa2[i].name,na)==0){
b=pa2[i].mark;
true=1;
Printf (“————- train number information of% s ——– \ n”, pa2 [i]. Name);
Printf (“departure station: Yantai Terminal:% s \ n “, L – > data [b]. Train_end);
Printf (“train% d Fare:% d \ n “, L – > data [b]. Train_num, L – > data [b]. Train_price);
Printf (“passenger name:% s \ n”, pa2 [i]. Name);
Printf (“passenger ID information:% d \ n”, pa2 [i]. Snum);
printf(“———————————\n”);
}
}
}
break;
case 2:
{
int w;
Printf (“please enter your ID number: \ n”);
scanf(“%d”,&w);
for(i=0;i
{
if(pa1[i].snum==w)
{
b=pa2[i].mark;
Printf (“————- train number information of% s ——– \ n”, pa2 [i]. Name);
Printf (“departure station: Yantai Terminal:% s \ n “, L – > data [b]. Train_end);
Printf (“train% d Fare:% d \ n “, L – > data [b]. Train_num, L – > data [b]. Train_price);
Printf (“passenger name:% s \ n”, pa2 [i]. Name);
Printf (“passenger ID information:% d \ n”, pa2 [i]. Snum);
printf(“———————————\n”);
}
}
}break;
default:
printf(“——————————\n”);
Printf (“– your input is wrong, please re-enter! — \ n”);
printf(“——————————\n”);
Select(q,fp);
break;
}
if(true==0)
{
Printf (“no information about this passenger! \ n”);
}
fclose(fp);
//getchar();
}
void Exchange(SeqList*L,FILE *fp)
{
Printf (“———— signature change service ———– \ n”);
Printf (“please enter your name: \ n”);
char ta[10];
if((fp=fopen(“train.dat”,”rb+”))==NULL)
{
printf(“Canot open file !\n”);
}
Person p[Num];
fseek(fp,0L,SEEK_END);
int r=ftell(fp)/sizeof(Person);
rewind(fp);
fread((void*)&p,sizeof(Person),r,fp);
int m,i,d,j,y,h;
scanf(“%s”,&ta);
for(i=0;i
{
if(strcmp(p[i].name,ta)==0){
d=p[i].mark;
Printf (“your current destination is% s station \ n”, L – > data [D]. Train_end);
L->data[d].leftnum++;
}
}
Inquiry(L);
Printf (“please re-enter the train number of the destination you want to go:”);
scanf(“%d”,&m);
for(j=0;jlen;j++)
{
if(m == L->data[j].Train_num)
{
L->data[j].leftnum–;
printf(“————————-\n”);
Printf (“— your signature change service has been completed! — \ n”);
printf(“————————-\n”);
d=j;
}
}
for(i=0;i
{
if(strcmp(p[i].name,ta)==0){
p[i].mark=d;
h=i;
}
}
fseek(fp,h*sizeof(Person),SEEK_SET);
fwrite((void*)&p,sizeof(Person),1,fp);
fclose(fp);
}
void Withdraw(FILE *fp)
{
Printf (“———- ticket refund service ——— \ n”);
char na[10];
Printf (“please enter your name: \ n”);
scanf(“%s”,&na);
Person *pe;
int i,j;
if((fp=fopen(“train.dat”,”rb”))==NULL)
{
printf(“The file cannot be opened!\n”);
}
fseek(fp,0L,SEEK_END);
int n=ftell(fp)/sizeof(Person);
if(n==1)
{
fclose(fp);
fp=fopen(“train.dat”,”wb”);
}
pe=(Person*)malloc(sizeof(Person)*(n-1));
rewind(fp);
for(j=i=0;i
{
fread((void*)(pe+j),sizeof(Person),1,fp);
if((strcmp((pe+j)->name,na))==0)
continue;
j++;
}
fclose(fp);
fp=fopen(“train.dat”,”wb”);
fwrite((void*)pe,sizeof(Person),n-1,fp);
printf(“————————-\n”);
Printf (“— your ticket refund service has been completed! — \ n”);
printf(“————————-\n”);
}
void Inquiry(SeqList*L)
{
int i;
if(L->len==0)
{
printf(“No ticket left!\n”);
}
else
{
printf(“—————————-Tickets information———————-\n”);
Printf (“train number Train number route Ticket price Number of remaining votes “);
for(i=0;ilen;i++){
printf(“\n %d Yantai–%s %d %d\n”,L->data[i].Train_num,L->data[i].Train_end,L->data[i].Train_price,L->data[i].leftnum);
printf(“———————————————————————\n”);
}
}
}
The above is the whole content of this article. I hope it will be helpful to your study.

If you want to better improve your programming ability, learn C language and C + + programming well! Overtake at the corner, take a quick step!
【C language c + + Learning Circle】, sharing (source code, project actual combat video, project notes, basic introductory tutorial)
Welcome to change careers and learn programming partners. Use more materials to learn and grow faster than you think!
Programming learning books:

Programming learning video:

Article source: https://blog.csdn.net/chaseqrr/article/details/93521777