#include <stdio.h> #include <string.h> #include <stdlib.h> #define MAX_FLIGHTCOD
ID: 3742277 • Letter: #
Question
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_FLIGHTCODE_LEN 6
#define MAX_CITYCODE_LEN 3
#define MAX_NUM_FLIGHTS 5
#define DB_NAME "database"
void print_menu (void)
{
printf(" "
"1. add a flight "
"2. display all flights to a destination "
"3. save the flights to the database file "
"4. load the flights from the database file "
"5. exit the program "
"Enter choice (number between 1-6)> ");
}
/* 1. add a flight
Add a new flight to the flights through the terminal. You should collect the input by asking multiple questions from the user.
Enter flight code>
Enter departure info for the flight leaving SYD.
Enter month, date, hour and minute separated by spaces>
Enter arrival city code>
Enter arrival info.
Enter month, date, hour and minute separated by spaces>
Your program should be able to check the format of the flightcode.
The first two characters of the flightcode should be uppercase letters (A-Z)
representing the airline. The rest of the flightcode should be numerals (0-9)
representing the flight number.
The program should works even if the flight code contains 2 letter and 1 number like (VA1)
There must be 1-4 numerals as the flight number part of the flightcode.
No spaces in the flightcode.
2. display all flights to a destination
Prompt the following question
Enter arrival city code or enter * to show all destinations>
The user may enter the abbreviation of MAX_CITYCODE_LEN characters for the arrival city. The program should display all flights to the requested destination. If the user input is *, display all flights.
The display format should is as follows.
Flight Origin Destination
------ --------------- ---------------
VA1 SYD 11-26 09:54 LAX 11-26 18:26
Pay attention to the strict formatting guide:
Flight - left aligned, MAX_FLIGHTCODE_LEN (i.e. 6) chars at most.
Origin and Destination
City - left aligned, MAX_CITYCODE_LEN (i.e. 3) chars at most.
Month, day, hour, minute - two digits with leading zeros
3. save the flights to the database file
Save the flights in the hard disk as a binary/text file named database.
You may use your own format to save the data.
You should overwrite if database file already exists.
It is up to you to create your own data storage format for the database file.
Your program should be able to read the database that was created by itself.
You can create the database as a text or binary file.
You do NOT need to be able to create a database identical to the database of the sample executable.
You do NOT need to be able to read the database of the sample executable.
4. load the flights from the database file
Read the database file and put the data into flights. You may only read the data files created by your own program. You should overwrite the flights array you had in memory when loading from the file.
5. exit the program
Exit the interactive program.
Enter choice (number between 1-5)>
-1
Invalid choice
/*
Explanation / Answer
// flight.cpp
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_FLIGHTCODE_LEN 6
#define MAX_CITYCODE_LEN 3
#define MAX_NUM_FLIGHTS 5
#define DB_NAME "database"
void print_menu ();
void getChoice();
void addFlight();
void displayFlightToDes();
void saveToDatabase();
void loadFromDatabase();
typedef struct
{
int month;
int day;
int hour;
int minute;
} dateTime ;
typedef struct
{
char city_arrival[MAX_CITYCODE_LEN+1];
char flightcode[MAX_FLIGHTCODE_LEN+1];
dateTime departure_data;
dateTime arrival_dte;
} flightStruct ;
int size = 0;
flightStruct flights [MAX_NUM_FLIGHTS];
int main(void)
{
while(1)
{
print_menu();
}
return 0;
}
void print_menu ()
{
printf(" "
"1. add a flight "
"2. display all flights to a destination "
"3. save the flights to the database file "
"4. load the flights from the database file "
"5. exit the program "
"Enter choice (number between 1-5)> ");
getChoice();
}
void getChoice()
{
int ch;
scanf("%d",&ch);
switch(ch)
{
case 1:
addFlight(); //add flight function
break;
case 2:
displayFlightToDes(); //display all flight to given destination function
break;
case 3:
saveToDatabase(); //save content of flights array to database function
break;
case 4:
loadFromDatabase(); //load data from database function
break;
case 5:
exit(0); //exit from program
}
return;
}
int checkDate(int mon,int date,int hour,int minute)
{
if(!(mon>=1 && mon<=12)) //month - integer between 1 and 12 (inclusive)
return 1;
if(!(date>=1 && date<=31)) //day - integer between 1 and 31 (inclusive)
return 2;
if(!(hour>=0 && hour<=23)) //hour - integer between 0 and 23 (inclusive)
return 3;
if(!(minute>=0 && minute<=59)) //minute - integer between 0 and 59 (inclusive)
return 4;
return 0;
}
void addFlight()
{
if(size == MAX_NUM_FLIGHTS)
{
printf("Cannot add more flights - memory full ");
return;
}
int s = 1;
printf("Enter flight code > ");
scanf("%s",flights[size].flightcode);
int mon,date,hour,minute;
int i;
while(s == 1)
{
s = 0;
if(strlen(flights[size].flightcode) < 3 || strlen(flights[size].flightcode) >6 )
{
printf("Invalid Input ");
s= 1;
}
else if((flights[size].flightcode[0]<'A') || (flights[size].flightcode[0]>'Z'))
{
printf("Invalid Input ");
s=1;
}
else if((flights[size].flightcode[1]<'A') || (flights[size].flightcode[1]>'Z'))
{
printf("Invalid Input ");
s=1;
}
if(s==0)
{
for(i = 2; i < strlen(flights[size].flightcode); i++)
{
if((flights[size].flightcode[i]<'0') || (flights[size].flightcode[i]>'9'))
{
printf("Invalid Input ");
s = 1;
break;
}
}
}
if(s==1)
{
printf("Enter flight code> ");
scanf("%6s", flights[size].flightcode);
}
if(s==0)
break;
}
printf("Enter departure info for the flight leaving SYD. Enter month, date, hour and minute separated by spaces > ");
scanf("%d%d%d%d",&mon,&date,&hour,&minute); //get month,date,hour and minutes from user for departure
while((date<1 || date >31) || (mon<1 || mon >12) || (hour<1 || hour >23) || (minute<1 || minute >59))
{
printf("Invalid input ");
printf("Enter month, date, hour and minute separated by spaces> ");
scanf("%d %d %d %d", &date, &mon, &hour, &minute);
}
flights[size].departure_data.month = mon;
flights[size].departure_data.day = date;
flights[size].departure_data.hour = hour;
flights[size].departure_data.minute = minute;
printf("Enter arrival city code > " );
scanf("%s",flights[size].city_arrival);
printf("Enter arrival info. Enter month, date, hour and minute separated by spaces > ");
scanf("%d%d%d%d",&mon,&date,&hour,&minute); //get month,date,hour and minutes from user for arrival
while((date<1 || date >31) || (mon<1 || mon >12) || (hour<1 || hour >23) || (minute<1 || minute >59))
{
printf("Invalid input ");
printf("Enter month, date, hour and minute separated by spaces> ");
scanf("%d %d %d %d", &date, &mon, &hour, &minute);
}
flights[size].arrival_dte.month = mon;
flights[size].arrival_dte.day = date;
flights[size].arrival_dte.hour = hour;
flights[size].arrival_dte.minute = minute;
size++;
return;
}
void displayFlightToDes()
{
char arr[MAX_FLIGHTCODE_LEN+1];
int flag = 0, headFlag = 0, i = 0;
printf("Enter arrival city code or enter * to show all destinations> ");
scanf("%s",arr);
for(i=0;i<size;i++)
{
if(strcmp(arr,flights[i].city_arrival) == 0 || strcmp(arr,"*") == 0)
{
if(headFlag == 0)
{
printf("Flight Origin Destination ");
printf("------ ----------- ----------- ");
headFlag = 1;
}
printf("%s SYD %d-%d %d:%d %3.3s %d-%d %d:%d ",flights[i].flightcode,flights[i].departure_data.month,flights[i].departure_data.day,flights[i].departure_data.hour,flights[i].departure_data.minute,flights[i].city_arrival,flights[i].arrival_dte.month,flights[i].arrival_dte.day,flights[i].arrival_dte.hour,flights[i].arrival_dte.minute);
flag = 1;
}
else if (size <1)
{
flag = 1;
printf("No flights ");
}
}
if(flag == 0) // No flights with arrival city but size is not 0
printf("No flights ");
return;
}
void saveToDatabase()
{
FILE *inputFile;
inputFile = fopen(DB_NAME,"w"); //open file in write mode
int i;
for(i=0;i<size;i++)
{
//save everything to file
fprintf(inputFile,"%s SYD %d %d %d %d %s %d %d %d %d ",flights[i].flightcode,flights[i].departure_data.month,flights[i].departure_data.day,flights[i].departure_data.hour,flights[i].departure_data.minute,flights[i].city_arrival,flights[i].arrival_dte.month,flights[i].arrival_dte.day,flights[i].arrival_dte.hour,flights[i].arrival_dte.minute);
}
fclose(inputFile); //closing file
return;
}
void loadFromDatabase()
{
FILE *inputFile;
inputFile = fopen(DB_NAME,"r"); //open file in read mode
int i = 0;
char syd[MAX_FLIGHTCODE_LEN+1];
while(!feof(inputFile)) //till end of file
{
//load from file
fscanf(inputFile,"%s%s%d%d%d%d%s%d%d%d%d",flights[i].flightcode,syd,&flights[i].departure_data.month,&flights[i].departure_data.day,&flights[i].departure_data.hour,&flights[i].departure_data.minute,flights[i].city_arrival,&flights[i].arrival_dte.month,&flights[i].arrival_dte.day,&flights[i].arrival_dte.hour,&flights[i].arrival_dte.minute);
i++;
}
size = i;
fclose(inputFile); //closing file
return;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.