C language implements a simple flight management system
- 2020-06-23 01:25:11
- OfStack
This paper shares the specific code of C language flight management system for your reference. The specific content is as follows
/*C Language implementation of a simple flight management system (single file) */
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
#include<assert.h>
#define PERSON_MAXNUM 100
#define PERSONNOTICKET_MAX 10
#define MAX_SIZE 10
int _size = 0;
int _person_size = 0;
int _personnoticket_size = 0;
// Define the flight information structure
typedef struct plane
{
char ID[10];
char Start_Place[10];
char End_Place[10];
float price;
int Buy_num;
int Max_num;
char time[20];
}Plane;
// Define the passenger information structure
typedef struct Person
{
int AirPlane_Num;
char person_name[20];
}Person;
void home(Plane* p,Person* person, Person* person_noticket);// The initial page
void InitAirPlane(Plane** p);// Initializes flight information memory
void InputAirPlane(Plane* p);// Enter flight information
void CheckAirPlane(Plane* p);// View flight information
void DelAirPlane(Plane* p);// Delete flight information
void CheckAirPlaneOrder(Plane* p,Person* person);// View flight orders
void PrintPerson(Plane* p, Person* person, int i);// Print flight passenger
void InitPerson(Person** person);// Initializes passenger information memory
void BookAirPlane(Plane* p, Person* person,Person* person_noticket);// Book a flight
int CheckPerson(Plane* p, Person* person);// View passenger order information
void ChangeAirPlane(Plane* p, Person* person);// endorse
void ReturnTicket(Plane* p, Person* person);// refund
void PrintPerson(Plane* p, Person* person, int i);
void CheckPersonNoTicket(Plane* p, Person* person_noticket);// Check the waiting list
///////////////////////////////////////
void home(Plane* p, Person* person, Person* person_noticket)
{
int a, i, j;
printf("\n***************************** Welcome to the airline ticketing system *****************************\n");
printf("\n************* Hello, now I want to confirm your identity! Please press the ticket agent 1 Please press 0 ***********\n");
printf(" Please select: ");
scanf("%d", &a);
// Flight management personnel operate
if (a == 1)
do{
printf("\n****************** 1. Enter flight information *******************\n");
printf("\n****************** 2. Delete flight information *******************\n");
printf("\n****************** 3. Browse flight information *******************\n");
printf("\n****************** 4. Browse current booking information *************\n");
printf("\n****************** 5. Check the waiting list *******************\n");
printf("\n****************** 0. exit ********************\n");
printf(" Please select: ");
scanf("%d", &i);
switch (i)
{
case 0: break;
case 1:InputAirPlane(p);break;
case 2:DelAirPlane(p);break;
case 3:CheckAirPlane(p);break;
case 4:CheckAirPlaneOrder(p, person); break;
case 5:CheckPersonNoTicket(p, person_noticket); break;
default:
printf(" Typo! \n");
break;
}
} while (i != 0);
// Passenger operation
if (a == 0)
do{
printf("\n*********************** 1. booking **********************\n");
printf("\n*********************** 2. endorse **********************\n");
printf("\n*********************** 3. refund **********************\n");
printf("\n*********************** 4. Browse flight information **************\n");
printf("\n*********************** 5. Query personal booking information **********\n");
printf("\n*********************** 0. exit **********************\n");
printf(" Please select: ");
scanf("%d", &j);
switch (j)
{
case 0:break;
case 1:BookAirPlane(p, person,person_noticket);break;
case 2:ChangeAirPlane(p, person);break;
case 3:ReturnTicket(p, person);break;
case 4:CheckAirPlane(p);break;
case 5:CheckPerson(p, person); break;
}
} while (j != 0);
}
// Initialize the
void InitAirPlane(Plane** p)
{
assert(p);
// To apply for space
*p = (Plane*)malloc(sizeof(Plane)*MAX_SIZE);
if (NULL == *p)
{
printf(" Opening up space failed! \n");
return;
}
(*p)->Buy_num = 0;
}
// Enter flight information
void InputAirPlane(Plane* p)
{
if (_size <= MAX_SIZE)
{
printf(" Flight Number: %d\n", _size);
printf(" Enter the flight ID:");
scanf("%s", (p+_size)->ID);
printf(" Input origin: ");
scanf("%s", (p + _size)->Start_Place);
printf(" Input Destination: ");
scanf("%s", (p + _size)->End_Place);
printf(" Enter flight departure time: ");
scanf("%s", (p + _size)->time);
printf(" Input price: ");
scanf("%f", &(p + _size)->price);
printf(" Enter the maximum number of passengers allowed on the flight: ");
scanf("%d", &(p + _size)->Max_num);
do
{
printf(" Current number of tickets sold: ");
scanf("%d", &(p + _size)->Buy_num);
if ((p + _size)->Buy_num > (p + _size)->Max_num)
{
printf(" Error entering the number of tickets sold! \n");
printf(" Please re-enter! \n\n");
}
} while ((p + _size)->Buy_num > (p + _size)->Max_num);
_size++;
printf(" Add flight information finished! \n\n");
}
else
{
printf(" Memory is full! \n");
return;
}
}
// Browse flight information
void CheckAirPlane(Plane* p)
{
int i = 0;
printf(" Available flight information: \n\n");
for (i = 0; i < _size; i++)
{
printf(" Flight Number: %d\n", i);
printf("ID:%s\n", (p+i)->ID);
printf(" Provenance: %s\n", (p + i)->Start_Place);
printf(" destination :%s\n", (p + i)->End_Place);
printf(" Departure time: %s\n", (p + i)->time);
printf(" Air ticket price: %2f\n", (p + i)->price);
printf(" Remaining tickets: %d\n", ((p + i)->Max_num - (p + i)->Buy_num) );
printf("\n");
}
}
// Delete flight information
void DelAirPlane(Plane* p)
{
int j = 0;
int i = -1;
assert(p);
printf(" Please enter the flight number to be deleted: ");
scanf("%d", &i);
for (j = i; j < _size; j++)
{
strcpy((p + j)->ID, (p + j + 1)->ID);
strcpy((p + j)->Start_Place, (p + j + 1)->Start_Place);
strcpy((p + j)->End_Place, (p + j)->End_Place);
strcpy((p + j)->time, (p + j + 1)->time);
(p + j)->price = (p + j + 1)->price;
(p + j)->Max_num = (p + j + 1)->Max_num;
(p + j)->Buy_num = (p + j + 1)->Buy_num;
}
_size--;
}
// Initialize booking information
void InitPerson(Person** person)
{
assert(person);
*person = (Person*)malloc(sizeof(Person)*PERSON_MAXNUM);
if (NULL == *person)
{
printf(" Opening up space failed! \n");
return;
}
}
// booking
void BookAirPlane(Plane* p, Person* person, Person* person_noticket)
{
int i = -1;
CheckAirPlane(p);
printf(" Please select the flight number you want to book: ");
scanf("%d", &i);
if ((p + i)->Max_num == (p + i)->Buy_num)
{
printf(" Sorry, this flight is sold out! \n");
printf(" Please enter your name (we will add you to the waiting list) : ");
scanf("%s", (person_noticket+_personnoticket_size)->person_name);
(person_noticket + _personnoticket_size)->AirPlane_Num = i;
_personnoticket_size++;
printf(" Add waiting list successfully! \n\n");
return;
}
else
{
if (_person_size > PERSON_MAXNUM)
{
printf(" Insufficient storage capacity! \n");
return;
}
printf(" Please enter your name: ");
scanf("%s", (person + _person_size)->person_name);
(person + _person_size)->AirPlane_Num = i;
_person_size++;
(p + i)->Buy_num++;
printf(" The reservation is successful! \n");
}
}
// View personal booking information
int CheckPerson(Plane* p, Person* person)
{
int flag = 0;
int i = 0;
char _name[20] = { 0 };
printf(" Please enter your name: ");
scanf("%s", &_name);
printf(" Your personal booking information: \n\n");
for (i = 0; i < _person_size; i++)
{
if (0 == strcmp(_name, (person + i)->person_name))
{
flag = 1;
printf(" Order No. : %d\n", i);
printf(" Name: %s\n", (person + i)->person_name);
printf(" Flight Number: %d\n", (person + i)->AirPlane_Num);
printf(" flight ID:%s\n", (p + (person + i)->AirPlane_Num)->ID);
printf(" Provenance: %s\n", (p + (person + i)->AirPlane_Num)->Start_Place);
printf(" Destination: %s\n", (p + (person + i)->AirPlane_Num)->End_Place);
printf(" Is the departure time :%s\n", (p + (person + i)->AirPlane_Num)->time);
printf(" Air ticket price: %f\n", (p + (person + i)->AirPlane_Num)->price);
printf("\n");
}
}
if (0 == flag)
{
printf(" The passenger booking information was not found! \n");
return 0;
}
return 1;
}
// endorse
void ChangeAirPlane(Plane* p, Person* person)
{
int i = -1;
int j = -1;
if (0 == CheckPerson(p, person))
{
return;
}
printf(" Please enter the order number to be changed: ");
printf("\n");
scanf("%d", &i);
(p + i)->Buy_num--;
CheckAirPlane(p);
do
{
printf(" Please enter the flight number you want to change: ");
scanf("%d", &j);
if ((p + j)->Buy_num >= (p + j)->Max_num)
{
printf(" This flight is full, please choose again! \n");
}
} while ((p + j)->Buy_num >= (p + j)->Max_num);
(person + i)->AirPlane_Num = j;
(p + j)->Buy_num++;
printf(" Change successfully! \n\n");
}
// Delete the order
void ReturnTicket(Plane* p, Person* person)
{
int i = -1;
int j = 0;
CheckPerson(p, person);
printf(" Please enter the order number to delete: ");
scanf("%d", &i);
for (j = i; j < _person_size; j++)
{
(person + j)->AirPlane_Num = (person + j + 1)->AirPlane_Num;
strcpy((person + j)->person_name, (person + j + 1)->person_name);
}
printf(" Successful refund! \n");
}
// View current booking information
void CheckAirPlaneOrder(Plane* p, Person* person)
{
int i = -1;
for (i = 0; i < _size; i++)
{
printf(" Flight Number: %d\n", i);
printf("ID:%s\n", (p + i)->ID);
printf(" Provenance: %s\n", (p + i)->Start_Place);
printf(" destination :%s\n", (p + i)->End_Place);
printf(" Departure time: %s\n", (p + i)->time);
printf(" Air ticket price: %2f\n", (p + i)->price);
printf(" Remaining tickets: %d\n", ((p + i)->Max_num - (p + i)->Buy_num));
printf(" Ticket buyer information: "); PrintPerson(p,person, i);
}
}
void PrintPerson(Plane* p, Person* person,int i)
{
int ticket_num = 0;
int j = 0;
for (j = 0; j < _person_size; j++)
{
if ((person + j)->AirPlane_Num == i)
{
printf("%s ,", (person + j)->person_name);
ticket_num++;
}
}
printf("\n\n");
}
// Waiting list initializes
void InitPersonNoTicket(Person** person_noticket)
{
assert(person_noticket);
*person_noticket = (Person*)malloc(sizeof(Person)*PERSONNOTICKET_MAX);
if (NULL == *person_noticket)
{
printf(" Opening up space failed! \n");
return;
}
}
// Check the waiting list
void CheckPersonNoTicket(Plane* p, Person* person_noticket)
{
int i = 0;
printf(" Waiting list: \n\n");
if (0 == _personnoticket_size)
{
printf(" The waiting list is empty! \n\n");
return;
}
for (i = 0; i < _personnoticket_size; i++)
{
printf(" Name: %s\n", (person_noticket + i)->person_name);
printf(" Flight Number required: %d\n", (person_noticket + i)->AirPlane_Num);
printf(" flight ID:%s\n", (p + ((person_noticket + i)->AirPlane_Num))->ID);
printf(" Provenance: %s\n", ((p + ((person_noticket + i)->AirPlane_Num))->Start_Place));
printf(" Destination: %s\n", (p + ((person_noticket + i)->AirPlane_Num))->End_Place);
}
}
// The main function
int main()
{
Plane* p=NULL;
Person* person=NULL;
Person* person_noticket = NULL;
InitPersonNoTicket(&person_noticket);
InitAirPlane(&p);
InitPerson(&person);
while (1)
{
home(p, person, person_noticket);
}
system("pause");
return 0;
}
For more information, please pay attention to the topic management System Development.