Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "plane_structure.h"
FILE *fr;
char temp[100];
plane aircraft[100];
plane foundAircraft;
int count=0;
void get_aircraft_filename(){
char filename[BUFSIZ];
char *p;
printf("Please enter the name of the file containing the aircraft data\n\n"
"Filename: ");
//scanf("%s",&filename);
if(fgets(filename, sizeof filename, stdin) != NULL){
if ((p = strchr(filename, '\n')) != NULL)
*p = '\0';
fr = fopen (filename, "rt");
}
if(fr==NULL){
printf("ERROR: file \"%s\" does not exist!\n\n",filename);
get_aircraft_filename();
}
load_plane_data();
}
load_plane_data(){
int i;
char seatInfo[5];
while(fgets(temp,100,fr) != NULL){
strcpy(aircraft[count].ident,&temp[7]);
fgets(temp,100,fr);
strcpy(aircraft[count].name,&temp[6]);
fgets(temp,49,fr);
strcpy(temp,&temp[6]);
aircraft[count].maximum_take_off_wieght=atoi(temp);
fgets(temp,49,fr);
strcpy(temp,&temp[4]);
aircraft[count].takeoff_dimension=atoi(temp);
fgets(temp,49,fr);
strcpy(temp,&temp[6]);
aircraft[count].max_landing_weight=atoi(temp);
fgets(temp,49,fr);
strcpy(temp,&temp[4]);
aircraft[count].landing_dimension=atoi(temp);
fgets(temp,49,fr);
strcpy(temp,&temp[4]);
aircraft[count].basic_wieght=atoi(temp);
fgets(temp,49,fr);
strcpy(temp,&temp[4]);
aircraft[count].basic_arm=atoff(temp);
fgets(temp,49,fr);
strcpy(temp,&temp[10]);
aircraft[count].seat_rows=atoi(temp);
char *token;
char *line = temp;
char *search = " ";
for(i=0;i<aircraft[count].seat_rows;i++){
int inner=0;
fgets(temp,49,fr);
char *seat_line=temp;
token=strtok(seat_line,search);
aircraft[count].seats[i].seats_in_row=atoi(token);
token=strtok(NULL,search);
aircraft[count].seats[i].arm=atoff(token);
}
fgets(temp,49,fr);
strcpy(temp,&temp[9]);
aircraft[count].baggage_locations=atoi(temp);
for(i=0;i<aircraft[count].baggage_locations;i++){
int inner=0;
fgets(temp,49,fr);
for(inner=0;inner<3;inner++){
if(temp[inner]==' ')break;
seatInfo[inner]=temp[inner];
aircraft[count].baggage[i].maximum_baggage_weight=atoi(seatInfo);
}
strcpy(temp,&temp[inner]);
aircraft[count].baggage[i].arm=atoff(temp);
}
//fuel
char fuelChars[5];
int fuelCount=0;
fgets(temp,49,fr);
strcpy(temp,&temp[6]);
//get the first bit
char *fuel_line = temp;
/* Token will point to "LINE". */
token = strtok(fuel_line, search);
aircraft[count].fuel.maximum_fuel_weight=atoi(token);
/* Token will point to "TO". */
token = strtok(NULL, search);
aircraft[count].fuel.arm=atoff(token);
token = strtok(NULL, search);
strcpy(fuelChars,fuelChars);
strcpy(aircraft[count].fuel.name_of_fuel,token);
strcpy(temp,&temp[fuelCount]);
aircraft[count].fuel.fuel_used_per_hour=atoi(temp);
fgets(temp,49,fr);
strcpy(temp,&temp[8]);
aircraft[count].cruise=atoi(temp);
fgets(temp,49,fr);
count++;
}
strcpy(aircraft[count].ident,"END");
fclose(fr);
}
void print_aircraft_info(){
int i;
int counter;
for(counter=0;counter<count;counter++){
printf("Aircraft: %d\n",counter);
printf("Ident: %s",aircraft[counter].ident);
printf("Name: %s",aircraft[counter].name);
printf("MTOW: %d\n",aircraft[counter].maximum_take_off_wieght);
printf("TD: %d\n",aircraft[counter].takeoff_dimension);
printf("MXLW: %d\n",aircraft[counter].max_landing_weight);
printf("LD: %d\n",aircraft[counter].landing_dimension);
printf("BW: %d\n",aircraft[counter].basic_wieght);
printf("BA: %f\n",aircraft[counter].basic_arm);
printf("SeatRows %d\n",aircraft[counter].seat_rows);
for(i=0;i<aircraft[counter].seat_rows;i++){
printf("%d %f\n",aircraft[counter].seats[i].seats_in_row,aircraft[counter].seats[i].arm);
}
printf("Baggage: %d\n",aircraft[counter].baggage_locations);
for(i=0;i<aircraft[counter].baggage_locations;i++){
printf("%d %f\n",aircraft[counter].baggage[i].maximum_baggage_weight,aircraft[counter].baggage[i].arm);
}
printf("Fuel: %d %f %s %d\n",aircraft[counter].fuel.maximum_fuel_weight,aircraft[counter].fuel.arm,aircraft[counter].fuel.name_of_fuel,aircraft[counter].fuel.fuel_used_per_hour);
printf("Cruise speed: %d",aircraft[counter].cruise);
}
}
void print_found_aircraft(){
int i;
printf("Aircraft found:\n");
printf("Ident: %s",foundAircraft.ident);
printf("Name: %s",foundAircraft.name);
printf("MTOW: %d\n",foundAircraft.maximum_take_off_wieght);
printf("TD: %d\n",foundAircraft.takeoff_dimension);
printf("MXLW: %d\n",foundAircraft.max_landing_weight);
printf("LD: %d\n",foundAircraft.landing_dimension);
printf("BW: %d\n",foundAircraft.basic_wieght);
printf("BA: %f\n",foundAircraft.basic_arm);
printf("SeatRows: %d\n",foundAircraft.seat_rows);
for(i=0;i<foundAircraft.seat_rows;i++){
printf("seats per row: %d\narm of seats: %f\n",foundAircraft.seats[i].seats_in_row,foundAircraft.seats[i].arm);
}
printf("Baggage: %d\n",foundAircraft.baggage_locations);
for(i=0;i<foundAircraft.baggage_locations;i++){
printf("%d %f\n",foundAircraft.baggage[i].maximum_baggage_weight,foundAircraft.baggage[i].arm);
}
printf("Fuel: %d %f %s %d\n",foundAircraft.fuel.maximum_fuel_weight,foundAircraft.fuel.arm,foundAircraft.fuel.name_of_fuel,foundAircraft.fuel.fuel_used_per_hour);
printf("Cruise speed: %d",foundAircraft.cruise);
}
void find_aircraft_by_ident(char ident[]){
int counter;
for(counter=0;counter<count;counter++){
char temp[7];
strcpy(temp,aircraft[counter].ident);
int diff=strcmp(temp, ident);
if(diff==0){
foundAircraft=aircraft[counter];
print_found_aircraft();
return;
}
}
printf("ERROR: Plane not found\n\n");
}
void find_aircraft_by_name(char name[]){
int counter;
for(counter=0;counter<count;counter++){
char temp[11];
strcpy(temp,aircraft[counter].name);
int diff=strcmp(temp, name);
if(diff==0){
foundAircraft=aircraft[counter];
print_found_aircraft();
return;
}
}
printf("ERROR: Plane not found\n\n");
}
airport_data_manipulation
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* Define the runway structure */
typedef struct _runway{
char runway_designator[50];
int length_in_feet;
}runway;
/* Define the airport structure */
typedef struct _airport{
char letter_code[6];
char name[20];
float latitude;
float longitude;
int number_of_runways;
runway runways[30];
int number_of_fuels;
char fuels[30][30];
}airport;
FILE *fr;
char filename[100];
airport airports[100];
int count;
char temp[100];
get_airport_filename(){
printf("Please enter the name of the file containing the airport data\n\n"
"Filename: ");
scanf("%s",&filename);
fr = fopen (filename, "rt");
if(fr==NULL){
printf("ERROR: file \"%s\" does not exist!",filename);
get_airport_filename();
}
load_airport_data();
}
load_airport_data(){
int i;
count=0;
char info[5];
while(fgets(temp,100,fr) != NULL){
// fgets(temp,100,fr);
strcpy(airports[count].letter_code,temp);
fgets(temp,100,fr);
strcpy(airports[count].name,temp);
fgets(temp,100,fr);
airports[count].latitude=atoff(temp);
fgets(temp,100,fr);
airports[count].longitude=atoff(temp);
fgets(temp,100,fr);
airports[count].number_of_runways=atoi(temp);
fgets(temp,100,fr);
int outer;
for(outer=0;outer<airports[count].number_of_runways;outer++){
char *token;
char *line = temp;
char *search = " ";
token=strtok(line,search);
strcpy(airports[count].runways[outer].runway_designator,token);
strcpy(temp,&temp[6]);
airports[count].runways[outer].length_in_feet=atoi(temp);
}
outer=0;
fgets(temp,100,fr);
airports[count].number_of_fuels=atoi(temp);
//here is where the problem is
//for some reason the number that comes back from
//airports[count].number_of_fuels on the second loop
//of the main loop returns 8 instead of two. Yet if you break the prog
//and check inside the structure it is clearly 2
for(outer=0;outer<airports[count].number_of_fuels;outer++){
fgets(temp,100,fr);
strcpy(airports[count].fuels[outer],temp);
outer++;
}
outer=0;
fgets(temp,100,fr);
count++;
}
fclose(fr);
}
print_airport_info(){
int counter=0;
for(counter=0;counter<count;counter++){
printf("Code: %s",airports[counter].letter_code);
printf("Name: %s",airports[counter].name);
printf("Latitude: %f\n",airports[counter].latitude);
printf("Longitude: %f\n",airports[counter].longitude);
printf("Number of runways: %d\n", airports[counter].number_of_runways);
int i;
for(i=0;i<airports[counter].number_of_runways;i++){
printf("%s %d\n",airports[counter].runways[i].runway_designator,airports[counter].runways[i].length_in_feet);
}
printf("Number of fuels: %d\n",airports[counter].number_of_fuels);
for(i=0;i<airports[counter].number_of_fuels;i++){
printf("%s",airports[counter].fuels[i]);
}
counter++;
}
}
print_found_airport(){
}
main