C language for aircraft ticketing system
- 2020-06-23 01:24:02
- OfStack
Plane ticket system design requirements: login system interface, account number, password; Establish flight information function; Check the air ticket information from Zhengzhou to a terminal; Air ticket purchase function; Add the function of ticket purchaser information; Display ticket purchaser information function; Cancellation of air tickets;
The music in the program can be changed to its own specified, when I use the music line of code comment out.
The code is as follows:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<Windows.h>
#include<conio.h>// mainly 1 Some users generate corresponding actions by pressing the keyboard, such as getch() function
#include<time.h> // Time library
#include<mmsystem.h>//windows Most of the interfaces related to multimedia
#pragma comment(lib,"winmm.lib")// Preprocessing, support pair windows Multimedia programming .
//#include<graphics.h>// Graphics library
#define N 15 // Most flights 15 . N Is a global variable
typedef struct traininfor
{
char qishi[30]; // Originating station
char zhongdian[30]; // The terminal
char trainnumber[10]; // Flight no.
float price; // Air ticket per person
int piaonumber; // The number of votes generated per flight
char time[30]; // Departure time
}hang;
struct ticket
{
char buyer[20]; // Name of ticket purchaser
char ID[20]; // Id number of ticket purchaser
int buynumber; // The number of tickets available for air travel
float money;// The total amount of money the user paid for the ticket
hang buy; // Information about airline tickets purchased by the user
struct ticket *next;// Pointer to the node
};
int d=0;// Count the number of flights
struct ticket *yonghu;// User purchase information header
hang plane[N]; // An array of flight information structures
int menu();// Print the main menu function
void DengLu();// Log in to the ticketing system function
void JianFile();// Set up the document flight information function
void OpenFile();// Read file information function
void check(); // Check the air ticket information function from Zhengzhou to a terminal
void buy(); // Air ticket purchase function
void add();// Add ticket purchaser information
int TicketMenu();// User ticket menu
void XianShi();// Display ticket buyer information
void tui(); // Cancel flight ticket function
void exit(); // Exit the program
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int main()
{
DengLu();// Log in to the ticketing system function
JianFile();// Establish documentation and flight information
//OpenFile();// Open the file and read the flight information
while(1)
{// Automatically returns to the main menu after each operation
switch(menu())
{//menu() The function is the input option
case 1:// Options for 1 when
OpenFile();// Open the file and read the flight information
printf(" Flight information has been imported successfully \n\n\n");
break;
case 2: // Options for 2 when
check();// Check the air ticket information function from Zhengzhou to a terminal
break;
case 3:// Options for 3 when
buy();// Air ticket purchase function
break;
case 4:// Options for 4 when
tui();// Refund function
break;
case 5:// Options for 5 when
system("cls");// Clear the screen
printf(" Thank you for your use. Have a nice trip \n");
Sleep(5000);// delay 15 seconds
exit(1);//
break;
}
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Log on to the ticketing system
void DengLu()
{
//getch()= Input from the keyboard 1 Three characters and no display getche()= Input from the keyboard 1 Two characters and display
printf(" Welcome to ticketing system. Please register your ticketing system account number 10 Bit, password is 6 A) : \n");
printf(" Tips: If the login password input error exceeds 3 The computer will shut down automatically: \n\n");
char ch;// Heavy keyboard input 1 A character
char password[10];//passsword The array represents the initial password
char input[10];//input The array represents the password to be entered
char Szhanghao[20];// zhanghao The array represents the account of the application
char Dzhanghao[20];// zhanghao The array represents the login account
int i=0,j;// Control input times
int number,find,count;
printf(" Application Account: \n");
fflush(stdin);// Empty the input stream
while(1)
{
ch=getche();// Input from the keyboard 1 Characters, save to ch variable
// Add filter, as long as the number, other do not do any processing
if(ch>='0' && ch<='9') //if( (ch>='a' && ch<='z') || (ch>='A' && ch<='Z') )
{
printf("\a");// Escape character \a=bell
Szhanghao[i++]=ch;// Store the application account in an array
if(10==i)// When writing conditional statements, put constants first
{
Szhanghao[i]='\0';
break;// Account for the 10 Bit stops input
}
}
}
//1 Prompt for password
printf("\n Set the password of the card: \n");
//2 Enter the password
i=0;
while(1)
{
ch=getch();// Input from the keyboard 1 Characters, save to ch variable
// Add filter, as long as the number, other do not do any processing
if(ch>='0' && ch<='9') //if( (ch>='a' && ch<='z') || (ch>='A' && ch<='Z') )
{
printf("*\a");// Escape character \a=bell
password[i++]=ch;// Store the initial password in an array
if(6==i)// When writing conditional statements, put constants first
{
password[i]='\0';
break;// Initial password is 6 Bit stops input
}
}
}
Sleep(2000);
system("cls");
printf(" Tips: If the login password input error exceeds 3 The computer will shut down automatically: \n\n");
printf(" Please log in to your account :\n");
while(1)
{
gets(Dzhanghao);
if(strcmp(Dzhanghao,Szhanghao)!=0)
printf(" The account you entered is incorrect, please re-enter: \n");
else
break;
}
number=0;// The number of times a password is entered
count =3;// There are 3 Second chance
while(count--)
{// cycle 3 time
find=0;j=0;//find Initialized to 0 , indicates that the password is entered correctly
memset(input,0,sizeof(input));// Initializes the input cipher array
printf(" Please enter your account password :\n");
for(i=0;i<6;i++)
{// The input 6 A password
ch=getch();// Input from the keyboard 1 Characters, save to ch variable
if(ch>='0' && ch<='9')
{// Adds an array if the password is a number
printf("*\a");
input[j++]=ch;
}
}
input[j]='\0';// Array to \0 The end of the
for(i=0;i<=5;i++)
{// Whether the password is entered correctly
if(input[i]!=password[i])
{// The entered password does not match the original password
find=1;
break;
}
}
if(find==0)
{// Enter password correctly
printf("\n Enter the correct entry, will enter the ticketing system home page ");
PlaySound("20100814_5c610cfa0dec6116aa701YT6vHJzW9Zw.wav", NULL, SND_FILENAME | SND_SYNC); // Music playback
printf("\n");
//Sleep(3000);// Time delay 3 seconds
system("cls");// Clear the screen
break;
}
else
{// Enter password number plus 1
number++;
printf("\n Enter the wrong password, ");
}
}
if(3==number)
{// When the number of input errors reached 3 When the time
printf("\n The input 3 Second password error, cannot be entered again, the computer will be in 10 Automatic shutdown after seconds \n");
system("shutdown -s -t 10");// The computer is about to shut down.
}
return ;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void JianFile()
{
FILE *fp;// Definition file pointer
if((fp=fopen("txt.in","w"))==NULL)
{// File creation failed
printf(" Build failed! \n");
exit(0);
}
// Enter flight information into the file
fprintf(fp," zhengzhou nanyang T1230 110.50 200 2018 years 11 month 6 day 12 point 15 points \n");
fprintf(fp," zhengzhou xinyang T1231 120.50 200 2018 years 11 month 6 day 13 point 15 points \n");
fprintf(fp," zhengzhou shangqiu T1232 100.00 200 2018 years 11 month 6 day 14 point 15 points \n");
fprintf(fp," zhengzhou kaifeng T1234 115.50 200 2018 years 11 month 6 day 15 point 15 points \n");
fprintf(fp," zhengzhou luoyang T1235 95.00 200 2018 years 11 month 6 day 16 point 15 points \n");
fprintf(fp," zhengzhou 3 Gate gorge T1236 97.30 200 2018 years 11 month 6 day 17 point 15 points \n");
fprintf(fp," zhengzhou hebi T1237 130.50 200 2018 years 11 month 6 day 18 point 15 points \n");
fprintf(fp," zhengzhou zhoukou T1238 117.00 200 2018 years 11 month 6 day 19 point 15 points \n");
fprintf(fp," zhengzhou zhumadian T1239 135.00 200 2018 years 11 month 6 day 20 point 15 points ");
fclose(fp);
return ;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// The file reads the flight information
void OpenFile()
{
FILE *fp; // Definition file pointer
d=0;// Indicates that the current file location was read
if((fp=fopen("txt.in","r"))==NULL)
{// Open the file
printf(" File opening failed! \n");
return ;
}
while(!feof(fp))
{// Reading to the file is empty
//printf(" Please set the first %d Location, train number, price, tickets left, arrival time, tickets purchased: \n",d);
fscanf(fp,"%s%s%s%f%d%s",plane[d].qishi,plane[d].zhongdian,plane[d].trainnumber,&plane[d].price,&plane[d].piaonumber,plane[d].time);
d++;
}
fclose(fp);// Close the file
return ;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int menu()
{
char s[10];// Input string options
int n;// The integer options
// Menu framework
printf("-------------------- Ticketing system directory --------------------\n\n");
printf("1, Import flight information \n");
printf("2, Check air tickets \n");
printf("3, Purchase a plane ticket \n");
printf("4, Cancel your flight \n");
printf("5, Exit the program \n");
printf(" Note: The plane can reach from this site to Nanyang, Xinyang, Shangqiu \n");
printf(" Kaifeng, Luoyang, 3 Gate gorge, Hebi, Zhoukou, Zhumadian \n");
printf(" If you would like more information please enter the option number \n \n");
printf(" Producer of this program: 2017 Level I Internet of Things engineering 1 class Mr. Hu \n");
printf(" Please enter options: ");
while(1)
{// Dead cycle state
fflush(stdin);// Empty the input stream
gets(s);// get 1 String of a string
n=atoi(s);// String converted to integer
if(n<1||n>5)// Determine if the options entered are correct
printf(" Input error, please re-enter: \n");
else// Meet the criteria and close the loop
break;
}
// Return option value
return n;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Check the air ticket information function from Zhengzhou to a terminal
void check()
{
int i,j;// Loop variables
int find=1,flag=0,count=0;// Mark variable
char qi[30];// Originating station
char mudi[30];// The terminal
printf(" Please enter the place of origin ( Hint: It starts from Zhengzhou ) : \n");
scanf("%s",qi);// Enter the place of origin
printf(" Please enter destination: \n");
scanf("%s",mudi);// Input destination
while(1)
{// Dead cycle state
if(0==strcmp(qi," zhengzhou "))
{// It started in Zhengzhou
flag=1;// The flag variable is 1 , indicates that there is no error in the starting place
for(j=0;j<d;j++)
{// cycle d Flight information
if(0==strcmp(plane[j].zhongdian,mudi))
{// Enter the terminal and flight information terminal 1 to
count=1;// The flag variable is 1
printf(" The flight number: ");// Export flight number
printf("%s\n",plane[j].trainnumber);
printf(" The origin : ") ;// Output origin
printf("%s\n",plane[j].qishi);
printf(" Destination: ");// Output destination
printf("%s\n",plane[j].zhongdian);
printf(" Single fare of this flight: ");// Output the fare
printf("%.2f\n",plane[j].price);
printf(" Flight time: ");// Output flight time
printf("%s\n",plane[j].time);
printf(" Number of tickets for this flight: ");// Output votes
printf("%d\n\n\n\n",plane[j].piaonumber);
return ;
}
}
}
else
{
flag=1;// Typo at the beginning
printf(" Error typing, return to menu !\n\n\n");
return ;
}
if(0==count&&flag==1)
{// Origin and destination incorrect
printf("sorry The results of your query have no information !\n\n\n");
return ;
}
}
return ;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Manipulate the shopping cart
void buy()
{
while(1)
{// In an infinite loop
switch(TicketMenu())
{//ShopCartMenu() For input options
case 1:// options 1 Display the user's ticket list information
XianShi();
break;
case 2: // options 2 Add user purchase information
add();
break;
case 3: // options 3 exit
printf(" About to exit the subroutine \n");
Sleep(2000);// Delay two seconds
return ;
}
}
return ;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Print shopping cart menu
int TicketMenu()
{
int n;// The input options
char s[5];// Input string
// Ticket subroutine menu framework
printf("\n\n Please select operation: \n");
printf("--------------------------------------------\n");
printf("1. Displays the current ticket list \n");
printf("2. Add ticket purchaser information \n");
printf("3. exit \n");
printf("--------------------------------------------\n");
printf(" Please select the corresponding number 1~3\n");
while(1)
{// In an infinite loop
fflush(stdin);// Empty the input stream
gets(s);// The input 1 String of a string
n=atoi(s);// String converted to integer
if(n<1||n>3)// Determine if the options entered are correct
printf(" Input error, please retype :\n");
else// Correct input, end loop
break;
}
return n;// Return to input options
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Display the user's ticket purchase information
void XianShi()
{
FILE *fp;// Definition file pointer
struct ticket *p;// define 1 A new node
p=yonghu;// Assign the entire ticket purchase information to p
if((fp=fopen("goupiao.txt","r"))==NULL)
{// Determine if the file is open
printf(" Failed to open file \n");
printf(" There is no ticket purchase information for users at the moment \n\n");
return ;
}
//printf("\n The name Id card Originating station destination Buy votes \n");
while(p!=NULL)
{// Export ticket buyer information
//fscanf(fp,"%s%s%s%s%d",p->buyer,p->ID,p->buy.qishi,p->buy.zhongdian,&p->buynumber);
printf(" Name: ");
printf("%s\n",p->buyer);
printf(" Id Number: ");
printf("%s\n",p->ID);
printf(" The origin : ") ;
printf("%s\n",p->buy.qishi);
printf(" Destination: ");
printf("%s\n",p->buy.zhongdian);
printf(" Tickets purchased: ");
printf("%d\n",p->buynumber);
printf(" Total money for this flight: ");
printf("%.2f\n",p->money);
printf(" Flight time: ");
printf("%s\n\n\n\n",p->buy.time);
//printf(" The name Id card The origin destination Buy votes The price of the ticket The time of departure \n");
//printf("%-11s%-20s%-10s%-10s%-7d%-7.2f%-20s\n",p->buyer,p->ID,p->buy.qishi,p->buy.zhongdian,,p->buy.price,p->buy.time);// Export ticket buyer information
p=p->next;// Point to the 1 A node
}
fclose(fp);// Close the file
return ;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Add user purchase information
void add()
{
FILE *fp1,*fp;// Define two file Pointers
struct ticket *p,*p1;// Define two nodes
int i,j;// Define control loop variables
int count,flag;// Define flag variables
int buynumber1;// The number of tickets purchased by the user
char choice1[10];// Select the variable
char choice2[10];// Select the variable
char mingzi[20];// The user name
char shenfen[20];// User ID number
char qi[30];// Originating station
char mudi[30];// The terminal
do{// Start going into a loop
printf(" Please enter your name: \n");
scanf("%s",mingzi);
printf(" Please enter your ID number: \n");
scanf("%s",shenfen);
printf(" Please enter the place of origin ( Hint: It starts from Zhengzhou ) : \n");
scanf("%s",qi);
printf(" Please enter destination: \n");
scanf("%s",mudi);
count=0;flag=0;// Flag variable initialization
while(1)
{// In an infinite loop
if(0==strcmp(qi," zhengzhou "))
{// The starting station is Zhengzhou
flag=1;// Mark is 1
for(j=0;j<d;j++)
{// cycle d Flight information
// printf("%s %s %.2f %d %s %d",pointtr->station[j],pointtr->trainnumber[j],pointtr->price[j],pointtr->piaonumber[j],pointtr->time[j],pointtr->buynumber[j]);
if(0==strcmp(plane[j].zhongdian,mudi))
{// destination 1 Cause when
count=1;// Mark is 1
// Output flight information
printf(" The flight number: ");
printf("%s\n",plane[j].trainnumber);
printf(" The origin : ") ;
printf("%s\n",plane[j].qishi);
printf(" Destination: ");
printf("%s\n",plane[j].zhongdian);
printf(" Single fare of this flight: ");
printf("%.2f\n",plane[j].price);
printf(" Flight time: ");
printf("%s\n",plane[j].time);
printf(" Number of tickets for this flight: ");
printf("%d\n",plane[j].piaonumber);
break;// End of cycle
}
}
break ;// End of cycle
}
else
{
flag=1;
printf(" Incorrect input: \n\n\n");
fflush(stdin);// Empty the input stream
break; // End of cycle
}
if(0==count&&flag==0)
{// The input criteria do not match
printf("sorry The results of your query have no information !\n\n\n");
return ;
}
}
if(1==count)
{// Find the flight information the user needs
printf(" Are you sure you want to buy this ticket? (1. is 2. no )\n");
scanf("%s",choice1);// The input options
if(0==strcmp(choice1,"1"))
{// Options for 1 when
printf(" Please enter the number of tickets you want to purchase: \n");
scanf("%d",&buynumber1);// Enter purchase ticket
if(buynumber1<0)
{// Does not conform to the objective situation
printf(" Input is wrong , Please re-enter! \n\n\n");
break;
}
if(plane[j].piaonumber-buynumber1>=0)
{// Input the correct
plane[j].piaonumber=plane[j].piaonumber-buynumber1;// Calculate the remaining air tickets
p=new ticket;// Generate new nodes
if(p==NULL)
{// Failed to allocate memory
printf(" Memory request failed! \n\n\n");
exit(1);
}
p->buynumber=buynumber1;// Number of tickets purchased by users
strcpy(p->buyer,mingzi);// The user name
strcpy(p->ID,shenfen);// User ID number
p->buy=plane[j];// The flight information
p->next=NULL;// Point to the 1 The nodes are empty
p1=yonghu;// Assign user information to p1
if(yonghu==NULL)// The head node is null
yonghu=p;// Assign a node to a header node
else
{
while(p1->next!=NULL)
p1=p1->next;// Loop the list to the end
p->next=p1->next;
p1->next=p;// The new node p At the end of the list
}
}
else
{
printf(" Tickets are sold out or not enough to buy !\n\n\n");
break;
}
}
else if(0==strcmp(choice1,"2"))
{// Input options are 2 when
printf(" Give up to buy , Return! \n\n\n");
break;
}
else
{// Incorrect input options
printf(" Error typing, return! \n\n\n");
break;
}
}
else// No flights were found for the customer
printf(" Input is wrong ");
printf(" Whether to continue buying tickets ? ( 1. Is that, 2. No) \n");
fflush(stdin);// Empty the input stream
scanf("%s",choice2);// The input options
}while(strcmp(choice2,"1")==0);// Elected to 1 when
if((fp1=fopen("goupiao.txt","w"))==NULL)
{// Determines whether the file creation failed
printf(" File creation failed. \n");
return ;
}
p=yonghu;// Assign user information to p
while(p!=NULL)
{//1 Loop straight to the list empty
p->money=p->buy.price*p->buynumber;// Calculate the total fare
fprintf(fp1,"%s %s %s %s %d %.2f %s\n",p->buyer,p->ID,p->buy.qishi,p->buy.zhongdian,p->buynumber,p->money,p->buy.time);
p=p->next;// Point to the 1 A node
}
fclose(fp1);// Close the file
return ;
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Cancel your flight
void tui()
{
FILE *fp,*fp1;// Define two file Pointers
char s[20];// Id number
char s1[20];// destination
char choice[10];// The option
int buynumber2;// Number of unsubscribe tickets
int i,j;// Control loop variable
int find=0;// Mark variable
struct ticket *p;// Define a new node
printf(" Are you sure you want to refund the ticket? 1. is 2. no \n");
scanf("%s",choice);// The input options
if(0==strcmp(choice,"1"))
{// Options for 1 when
printf(" Please enter your ID number: \n");
scanf("%s",s);// Enter your ID number
printf(" Please enter destination: \n");
scanf("%s",s1);// Input destination
if((fp=fopen("goupiao.txt","r"))==NULL)
{// Open the file
printf(" Failed to open file \n");
return ;
}
p=yonghu;// Assign user information to p
while(p!=NULL)
{// Loop to empty list
//fscanf(fp,"%s%s%s%s%f%d%s",p->buyer,p->ID,p->buy.qishi,p->buy.zhongdian,p->buy.price,&p->buynumber,p->buy.time);
if(0==strcmp(s,p->ID)&&(0==strcmp(s1,p->buy.zhongdian)))
{// Find the corresponding user
find=1;// The flag variable is 1
printf(" Please enter the number of tickets returned: \n");
scanf("%d",&buynumber2);// Enter the unsubscribe ticket
if(buynumber2<0)
{// Does not conform to the objective situation
printf(" Input is wrong , Return to the menu !\n");
return ;
}
if(p->buynumber>=buynumber2)
{// The input of votes meets the criteria
fclose(fp);// Shut down yonghu file
if((fp=fopen("goupiao.txt","w"))==NULL)
{// Determines whether the file creation failed
printf(" File creation failed \n");
return ;
}
if((fp1=fopen("txt.in","w"))==NULL)
{// Determines whether the file creation failed
printf(" File creation failed. \n");
return ;
}
printf(" Successful refund, return to menu !\n\n\n");
for(i=0;i<d;i++)
{// cycle d Flight information
if(0==strcmp(plane[i].zhongdian,p->buy.zhongdian))
{// Find the corresponding terminal and count the remaining votes
plane[i].piaonumber+=buynumber2;
}
fprintf(fp1,"%s %s %s %f %d %s\n",plane[i].qishi,plane[i].zhongdian,plane[i].trainnumber,plane[i].price,plane[i].piaonumber,plane[i].time);
}
p->buynumber-=buynumber2;// Number of tickets purchased by users
p->money=p->buynumber*p->buy.price;// The total price of tickets
p=yonghu;// Assign user information to p
while(p!=NULL)
{// When the loop list is empty
fprintf(fp,"%s %s %s %s %.2f %d %s\n",p->buyer,p->ID,p->buy.qishi,p->buy.zhongdian,p->money,p->buynumber,p->buy.time);
p=p->next;// Point to the 1 A node
}
fclose(fp);// Close the file
fclose(fp1);// Close the file
return ;
}
else
{
printf(" Error typing, return to menu! \n\n\n");
return ;
}
}
p=p->next;// Point to the 1 A node
}
if(0==find)
{// Input is wrong
printf(" Unable to query information, return to menu \n\n\n");
return ;
}
}
else if(0==strcmp(choice,"2"))
{// Input options are 2 when
printf(" Abandon the refund and return to the menu !\n\n\n");
return ;
}
else
{// Input is wrong
printf(" Error typing, return to menu! \n\n\n");
return ;
}
return ;
}
For more information, please pay attention to the topic management System Development.