/************************************************/ /* ECE-1011 Spring 2003 */ /* Section:......... 0 */ /* Assignment:...... 4 */ /* Programmer:...... BAHN, William L. */ /* File Contents:... SOURCE CODE */ /* Filename:........ s40_key */ /* Due Date:........ 24 Feb 03 */ /* E-mail Address:.. wbahn@eas.uccs.edu */ /************************************************/ /*============== STANDARD LIBRARY INCLUDE FILES =============================*/ #include /* printf(), scanf(), fprintf(), fscanf() */ /*============== PROGRAMMER DEFINED CONSTANTS ===============================*/ /*****************************************************************************/ /* Version and Author Control */ /*****************************************************************************/ #define VERSION (4) #define REVISION (1) #define PROGRAMMER "BAHN, William" #define PROGRAMMER_EMAIL "wbahn@eas.uccs.edu" #define LASTMODDATE "17 FEB 2003" /*****************************************************************************/ /* Filenames */ /*****************************************************************************/ #define OUTPUT_FILENAME "s40_key.out" #define MODEL_FILENAME "MODEL.dat" #define AIRCRAFT_FILENAME "AIRCRAFT.dat" #define FLIGHT_FILENAME "FLIGHT.dat" /*****************************************************************************/ /* Parameters for a particular type of aircraft */ /*****************************************************************************/ /* AIRCRAFT_TYPE "Text String Description" */ /* MIN_GROSS (Minimum Gross Weight in pounds) */ /* MAX_GROSS (Maximum Gross Weight in pounds) */ /* FWD_CG_MINGW (Fwd CG Limit @ MIN_GROSS) */ /* FWD_CG_MAXGW (Fwd CG Limit @ MAX_GROSS) */ /* FWD_CG_BRKGW (Weight at which FWD CG begins changing) */ /* AFT_CG_MINGW (AFT CG Limit @ MIN_GROSS) */ /* AFT_CG_MAXGW (AFT CG Limit @ MAX_GROSS) */ /* AFT_CG_BRKGW (Weight at which AFT CG begins changing) */ /* */ /* NOTE: If a particular CG Limit is static (does not change with weight) */ /* then code the corresponding Break Weight as MAX_GROSS. */ /*****************************************************************************/ /*****************************************************************************/ /* These will now become variables read from a file. They will be left as */ /* all uppercase, in violation of the normal convention, so that no edits */ /* to the program code is necessary. They can be converted using the search */ /* and replace utility of the editor at a later time. */ /*****************************************************************************/ /* #define AIRCRAFT_TYPE "T41-B" /* #define MIN_GROSS (1500.0) /* #define MAX_GROSS (2500.0) /* /* #define FWD_CG_MINGW (35.0) /* #define FWD_CG_MAXGW (40.5) /* #define FWD_CG_BRKGW (1950.0) /* /* #define AFT_CG_MINGW (47.0) /* #define AFT_CG_MAXGW (47.0) /* #define AFT_CG_BRKGW (MAX_GROSS) /*****************************************************************************/ /*****************************************************************************/ /* Loading Condition Codes */ /*****************************************************************************/ #define WITHIN_CG (0) #define OVR_LOADED (1) #define FWD_LOADED (2) #define AFT_LOADED (3) /*****************************************************************************/ /* Symbolic Constants that are strictly internal to program */ /*****************************************************************************/ #define BLANKLINE printf("\n") #define TRUE (1==1) #define FALSE (!TRUE) /*****************************************************************************/ /* EXIT CODES */ /*****************************************************************************/ #define ERROR_NONE (0) #define ERROR_BAD_OUTPUT_FILE (1) #define ERROR_BAD_MODEL_FILE (2) #define ERROR_BAD_AIRCRAFT_FILE (3) #define ERROR_BAD_FLIGHT_FILE (4) /*****************************************************************************/ /*===========================================================================*/ /*=========================== MAIN FUNCTION =================================*/ /*===========================================================================*/ int main(void) { /* Variable Dictionary */ double item_wt, item_cg; /* Misc. Items Weight and CG */ double gross_wt, gross_cg, gross_mom; /* Total A/C Weight, CG, and Moment */ double dx, dy; /* Dummy variables for slope cals */ double fwd_cg, aft_cg; /* Computed CG Limits */ int item, items; int LoadingCondition; int error_code; /* Previous #define constants that will now be read from a file */ double MIN_GROSS = (1500.0); double MAX_GROSS = (2500.0); double FWD_CG_MINGW = (35.0); double FWD_CG_MAXGW = (40.5); double FWD_CG_BRKGW = (1950.0); double AFT_CG_MINGW = (47.0); double AFT_CG_MAXGW = (47.0); double AFT_CG_BRKGW = (2500.0); FILE *output_fp; FILE *model_fp; FILE *aircraft_fp; FILE *flight_fp; /*========================================================================*/ /* Open and verify all output and data files */ /*========================================================================*/ /* Open and check OUTPUT File */ error_code = ERROR_NONE; output_fp = fopen(OUTPUT_FILENAME, "wt"); if(NULL == output_fp) { printf("Error opening file: %s for writing - abort!\n", OUTPUT_FILENAME); error_code = ERROR_BAD_OUTPUT_FILE; } if(error_code) /* Exit program if an error has been detected */ return(error_code); /* Generate Header Info for Output File */ fprintf(output_fp, "NAME: %s\n", PROGRAMMER); fprintf(output_fp, "FILE: %s\n", OUTPUT_FILENAME); fprintf(output_fp, "E-MAIL: %s\n", PROGRAMMER_EMAIL); fprintf(output_fp, "VERSION: %i.%i\n", VERSION, REVISION); /* Open and check MODEL file */ model_fp = fopen(MODEL_FILENAME, "rt"); fprintf(output_fp, "%s: ", MODEL_FILENAME); if(NULL == model_fp) { printf("Error opening file: %s for reading - abort!\n", MODEL_FILENAME); error_code = ERROR_BAD_MODEL_FILE; fprintf(output_fp, "FAIL\n"); } else { fprintf(output_fp, "SUCCESS\n"); } /* Open and check AIRCRAFT file */ aircraft_fp = fopen(AIRCRAFT_FILENAME, "rt"); fprintf(output_fp, "%s: ", AIRCRAFT_FILENAME); if(NULL == aircraft_fp) { printf("Error opening file: %s for reading - abort!\n", AIRCRAFT_FILENAME); error_code = ERROR_BAD_AIRCRAFT_FILE; fprintf(output_fp, "FAIL\n"); } else { fprintf(output_fp, "SUCCESS\n"); } /* Open and check FLIGHT file */ flight_fp = fopen(FLIGHT_FILENAME, "rt"); fprintf(output_fp, "%s: ", FLIGHT_FILENAME); if(NULL == flight_fp) { printf("Error opening file: %s for reading - abort!\n", FLIGHT_FILENAME); error_code = ERROR_BAD_FLIGHT_FILE; fprintf(output_fp, "FAIL\n"); } else { fprintf(output_fp, "SUCCESS\n"); } if(error_code) /* Exit program if an error has been detected */ { fcloseall(); return(error_code); } /*========================================================================*/ /* Get Aircraft Envelope Parameters from MODEL file */ /*========================================================================*/ fscanf(model_fp, "%lf %lf\n", &MIN_GROSS, &FWD_CG_MINGW); fscanf(model_fp, "%lf %lf\n", &FWD_CG_BRKGW, &FWD_CG_MINGW); fscanf(model_fp, "%lf %lf\n", &MAX_GROSS, &FWD_CG_MAXGW); fscanf(model_fp, "%lf %lf\n", &MIN_GROSS, &AFT_CG_MINGW); fscanf(model_fp, "%lf %lf\n", &AFT_CG_BRKGW, &AFT_CG_MINGW); fscanf(model_fp, "%lf %lf\n", &MAX_GROSS, &AFT_CG_MAXGW); fclose(model_fp); /*========================================================================*/ /* Display CG Envelope Description on Screen. */ /*========================================================================*/ printf("===============================================================\n"); printf("Aircraft CG Calculator Version %i.%i\n", VERSION, REVISION); printf("Programmer: %s\n", PROGRAMMER); printf("Date of Last Mod: %s\n", LASTMODDATE); printf("===============================================================\n"); BLANKLINE; printf("This program computes the weight and balance for an aircraft\n"); printf("described by the following CG Envelope:\n"); BLANKLINE; printf("Gross Weight Limitations:\n"); printf(" The minimum aircraft gross weight is %.1f pounds.\n", MIN_GROSS); printf(" The maximum aircraft gross weight is %.1f pounds.\n", MAX_GROSS); BLANKLINE; printf("Forward CG Limitations:\n"); if(FWD_CG_BRKGW < MAX_GROSS) /* Sloped portion of FWD CG Limit exists */ { printf(" For Aircraft Gross Weights below %.1f lb:\n", FWD_CG_BRKGW); printf(" FWD CG Limit is %.2f\" aft of the datum.\n", FWD_CG_MINGW); printf(" For Aircraft Gross Weight between %.1f lb and %.1f lb:\n", FWD_CG_BRKGW, MAX_GROSS); printf(" FWD CG Limit moves linearly from %.2f\" to %.2f\".\n", FWD_CG_MINGW, FWD_CG_MAXGW); } else /* No sloped portion of FWD CG Limit exists */ { printf(" For Aircraft Gross Weights below %.1f lb:\n", MAX_GROSS); printf(" FWD CG Limit is %.2f\" aft of the datum.\n", FWD_CG_MINGW); } BLANKLINE; if(AFT_CG_BRKGW < MAX_GROSS) /* Sloped portion of AFT CG Limit exists */ { printf("Aft CG Limitations:\n"); printf(" For Aircraft Gross Weights below %.1f lb:\n", AFT_CG_BRKGW); printf(" AFT CG Limit is %.2f\" aft of the datum.\n", AFT_CG_MINGW); printf(" For Aircraft Gross Weights between %.1f lb and %.1f lb:\n", AFT_CG_BRKGW, MAX_GROSS); printf(" AFT CG Limit moves linearly from %.2f\" to %.2f\".\n", AFT_CG_MINGW, AFT_CG_MAXGW); } else /* No sloped portion of AFT CG Limit exists */ { printf(" For Aircraft Gross Weights below %.1f lb:\n", MAX_GROSS); printf(" AFT CG Limit is %.2f\" aft of the datum.\n", AFT_CG_MINGW); } BLANKLINE; /*========================================================================*/ /* Start Report Display */ /*========================================================================*/ printf("===============================================================\n"); printf("AIRCRAFT WEIGHT AND BALANCE REPORT \n"); printf("===============================================================\n"); BLANKLINE; /*========================================================================*/ /* Get Aircraft Empty Weight and CG from AIRCRAFT file */ /*========================================================================*/ fscanf(aircraft_fp, "%i\n", &items); gross_wt = 0.0; gross_mom = 0.0; for (item = 0; item < items; item++) { fscanf(aircraft_fp, "%lf %lf\n", &item_wt, &item_cg); gross_wt += item_wt; gross_mom += item_wt * item_cg; gross_cg = gross_mom / gross_wt; } fprintf(output_fp, "EMPTY (WT, CG): %.1f, %.2f\n", gross_wt, gross_cg); fclose(aircraft_fp); /*========================================================================*/ /* Continue Report Display */ /*========================================================================*/ printf("Your empty CG is %5.2f\" at a gross weight of %6.1f lb.\n", gross_cg, gross_wt); BLANKLINE; /*========================================================================*/ /* Get Flight Specific Data from FLIGHT file */ /*========================================================================*/ while(2 == fscanf(flight_fp, "%lf %lf", &item_wt, &item_cg)) { gross_wt += item_wt; gross_mom += item_wt * item_cg; gross_cg = gross_mom / gross_wt; } fprintf(output_fp, "GROSS (WT, CG): %.1f, %.2f\n", gross_wt, gross_cg); fclose(flight_fp); /*========================================================================*/ /* Continue Report Display */ /*========================================================================*/ printf("Your gross CG is %5.2f\" at a gross weight of %6.1f lb.\n", gross_cg, gross_wt); BLANKLINE; /*========================================================================*/ /* Section 4 - Compute and output the CG Limits --------------------------*/ /* Initial values for these parameters - will be overridden if wrong */ LoadingCondition = WITHIN_CG; fwd_cg = FWD_CG_MINGW; aft_cg = AFT_CG_MINGW; /* Check for Over MAX Gross Weight Condition */ if(gross_wt > MAX_GROSS) { LoadingCondition = OVR_LOADED; } /* Compute CG Limits */ if(OVR_LOADED != LoadingCondition) /* If over MAX_GROSS, no CG Limits exist */ { /* FWD LIMIT */ if(FWD_CG_BRKGW < MAX_GROSS) /* Sloped portion of FWD CG Limit exists */ { if(FWD_CG_BRKGW < gross_wt) /* A/C GW lies on sloped portion */ { /* Compute FWD CG Limit on the sloped portion */ dx = (FWD_CG_MAXGW - FWD_CG_MINGW); dy = (MAX_GROSS - FWD_CG_BRKGW); fwd_cg = FWD_CG_MINGW + (gross_wt - FWD_CG_BRKGW)*(dx/dy); } } /* AFT LIMIT */ if(AFT_CG_BRKGW < MAX_GROSS) /* Sloped portion of AFT CG Limit exists */ { if(AFT_CG_BRKGW < gross_wt) /* A/C GW lies on sloped portion */ { /* Compute AFT CG Limit on the sloped portion */ dx = (AFT_CG_MAXGW - AFT_CG_MINGW); dy = (MAX_GROSS - AFT_CG_BRKGW); aft_cg = AFT_CG_MINGW + (gross_wt - AFT_CG_BRKGW)*(dx/dy); } } /* Determine Loading Condition if out of limits FWD or AFT */ if(gross_cg < fwd_cg) LoadingCondition = FWD_LOADED; if(gross_cg > aft_cg) LoadingCondition = AFT_LOADED; } /* The first switch() statement performs the following pseudocode step: */ /* HANDLE OVER GROSS CONDITION */ /* if (overloaded) */ /* print out overload message including max gross weight value. */ /* else */ /* print out FWD and AFT CG Limits at the total gross weight. */ switch(LoadingCondition) { case (OVR_LOADED) : printf("*** DANGER *** OVERLOADED!\n"); BLANKLINE; printf("Aircraft exceeds the max allowable gross "); printf("weight by %6.1f lb.\n", (gross_wt - MAX_GROSS)); fprintf(output_fp, "LIMITS (FWD, AFT): 0.0, 0.0\n"); fprintf(output_fp, "CONDITION: OVER_GROSS\n"); break; default: printf("At this weight your CG Limits are "); printf("FWD: %5.2f\" and AFT: %5.2f\"\n", fwd_cg, aft_cg); fprintf(output_fp, "LIMITS (FWD, AFT): %.2f, %.2f\n", fwd_cg, aft_cg); fprintf(output_fp, "CONDITION: "); break; } BLANKLINE; /* The second switch() statement performs the following pseudocode */ /* HANDLE UNDER MAX GROSS CONDITIONS */ /* if (overloaded) */ /* do nothing (already handled above) */ /* else */ /* print out message appropriate to condition. */ switch(LoadingCondition) { case (FWD_LOADED) : printf("*** DANGER *** FORWARD LOADED!\n"); BLANKLINE; printf("Aircraft exceeds the forward CG limit by "); printf("%.2f\".\n", (fwd_cg - gross_cg)); fprintf(output_fp, "FWD_LOADED\n"); break; case (AFT_LOADED) : printf("*** DANGER *** AFT LOADED!\n"); BLANKLINE; printf("Aircraft exceeds the aft CG limit by "); printf("%.2f\".\n", (gross_cg - aft_cg)); fprintf(output_fp, "AFT_LOADED\n"); break; case (WITHIN_CG) : printf("*** SAFE ***\n"); BLANKLINE; printf("Aircraft loaded within allowed weight "); printf("and balance limits.\n"); fprintf(output_fp, "SAFE\n"); break; } BLANKLINE; printf("===============================================================\n"); BLANKLINE; /* End of Program --------------------------------------------------------*/ fcloseall(); return(error_code); }