Soldato
- Joined
- 10 Apr 2004
- Posts
- 13,497
C++ Programming, tidy code up?
Right basically I have this code:
Ok so is there anyway of making this better? Is there something I should learn that would tidy this up?
Cheers!
Right basically I have this code:
Code:
#include "stdio.h"
#include "windows.h"
#include "FSUIPC_User.h"
#include "stdlib.h"
#include "dos.h"
#define fuelpercentage 0.00001192092896
#define fuelweight 3.037151
// size 1/2 bytes = short int (d)
// size 4 bytes = int (d)
// size 8 bytes = double (f)
int main(int argc, char *argv[]){
//Initiate Variables
short int surface_wind_speed;
signed int surface_wind_direction;
short int acceleration, time_hours, time_minutes, time_seconds;
short int vor1_dist, vor1_speed, vor1_eta, vor2_dist, vor2_speed, vor2_eta;
//char nav1_freq[99], nav2_freq[99];
int IAS, TAS, ground_altitude,ground_speed,vertical_speed;
double compass,cofg;
// Engine Variables
short int eng1_n1, eng1_n2, eng1_egt, eng1_oiltemp, eng2_n1, eng2_n2, eng2_egt, eng2_oiltemp, eng3_n1, eng3_n2, eng3_egt, eng3_oiltemp, eng4_n1, eng4_n2, eng4_egt, eng4_oiltemp;
int eng1_fuel_press, eng1_hydr_press, eng1_vibr, eng1_oil_press, eng2_fuel_press, eng2_hydr_press, eng2_vibr, eng2_oil_press, eng3_fuel_press, eng3_hydr_press, eng3_vibr, eng3_oil_press, eng4_fuel_press, eng4_hydr_press, eng4_vibr, eng4_oil_press;
double eng1_ff, eng2_ff, eng3_ff, eng4_ff;
//Fuel Variables
int tank1_cap, tank1_per, tank2_cap, tank2_per, tank3_cap, tank3_per, tank4_cap, tank4_per, tank5_6_cap, tank5_6_per, tank5A_cap, tank5A_per, tank7_8_cap, tank7_8_per, tank7A_cap, tank7A_per, tank9_cap, tank9_per, tank10_cap, tank10_per, tank11_cap, tank11_per;
DWORD dwResult;
//While loop for main app.
while (1)
{
//Checks that FS2004 is running and thus allows for the data collection.
if (FSUIPC_Open(SIM_FS2K4, &dwResult))
{
while(1){
// Environment Data
FSUIPC_Read(0x0238, 1, &time_hours, &dwResult);
FSUIPC_Read(0x0239, 1, &time_minutes, &dwResult);
FSUIPC_Read(0x023A, 1, &time_seconds, &dwResult);
FSUIPC_Read(0x0020, 4, &ground_altitude, &dwResult);
FSUIPC_Read(0x04D8, 2, &surface_wind_speed, &dwResult);
FSUIPC_Read(0x04DA, 2, &surface_wind_direction, &dwResult);
//Navagation Data
FSUIPC_Read(0x0300, 2, &vor1_dist, &dwResult);
FSUIPC_Read(0x0302, 2, &vor1_speed, &dwResult);
FSUIPC_Read(0x0304, 2, &vor1_eta, &dwResult);
FSUIPC_Read(0x0306, 2, &vor2_dist, &dwResult);
FSUIPC_Read(0x0308, 2, &vor2_speed, &dwResult);
FSUIPC_Read(0x030A, 2, &vor2_eta, &dwResult);
//FSUIPC_Read(0x0350, 2, nav1_freq, &dwResult);
//FSUIPC_Read(0x0352, 2, nav2_freq, &dwResult);
//Aircraft Performance Data
FSUIPC_Read(0x11BA, 2, &acceleration, &dwResult);
FSUIPC_Read(0x02BC, 4, &TAS, &dwResult);
FSUIPC_Read(0x02BC, 4, &IAS, &dwResult);
FSUIPC_Read(0x2B00, 8, &compass, &dwResult);
FSUIPC_Read(0x02B4, 4, &ground_speed, &dwResult);
FSUIPC_Read(0x02C8, 4, &vertical_speed, &dwResult);
FSUIPC_Read(0x2EF8, 8, &cofg, &dwResult);
//Engine Data
FSUIPC_Read(0x0898, 2, &eng1_n1, &dwResult);
FSUIPC_Read(0x0896, 2, &eng1_n2, &dwResult);
FSUIPC_Read(0x08BE, 2, &eng1_egt, &dwResult);
FSUIPC_Read(0x0918, 8, &eng1_ff, &dwResult);
FSUIPC_Read(0x08F8, 4, &eng1_fuel_press, &dwResult);
FSUIPC_Read(0x08B8, 2, &eng1_oiltemp, &dwResult);
FSUIPC_Read(0x08BA, 4, &eng1_oil_press, &dwResult);
FSUIPC_Read(0x08D8, 4, &eng1_hydr_press, &dwResult);
FSUIPC_Read(0x08D4, 4, &eng1_vibr, &dwResult);
FSUIPC_Read(0x0930, 2, &eng2_n1, &dwResult);
FSUIPC_Read(0x092E, 2, &eng2_n2, &dwResult);
FSUIPC_Read(0x0956, 2, &eng2_egt, &dwResult);
FSUIPC_Read(0x09B0, 8, &eng2_ff, &dwResult);
FSUIPC_Read(0x0990, 4, &eng2_fuel_press, &dwResult);
FSUIPC_Read(0x0950, 2, &eng2_oiltemp, &dwResult);
FSUIPC_Read(0x09EA, 4, &eng2_oil_press, &dwResult);
FSUIPC_Read(0x0970, 4, &eng2_hydr_press, &dwResult);
FSUIPC_Read(0x096C, 4, &eng2_vibr, &dwResult);
FSUIPC_Read(0x09C8, 2, &eng3_n1, &dwResult);
FSUIPC_Read(0x09C6, 2, &eng3_n2, &dwResult);
FSUIPC_Read(0x09EE, 2, &eng3_egt, &dwResult);
FSUIPC_Read(0x0A48, 8, &eng3_ff, &dwResult);
FSUIPC_Read(0x0A28, 4, &eng3_fuel_press, &dwResult);
FSUIPC_Read(0x09E8, 2, &eng3_oiltemp, &dwResult);
FSUIPC_Read(0x09EA, 4, &eng3_oil_press, &dwResult);
FSUIPC_Read(0x0A08, 4, &eng3_hydr_press, &dwResult);
FSUIPC_Read(0x0A04, 4, &eng3_vibr, &dwResult);
FSUIPC_Read(0x0A60, 2, &eng4_n1, &dwResult);
FSUIPC_Read(0x0A5E, 2, &eng4_n2, &dwResult);
FSUIPC_Read(0x0A86, 2, &eng4_egt, &dwResult);
FSUIPC_Read(0x0AE0, 8, &eng4_ff, &dwResult);
FSUIPC_Read(0x0AC0, 4, &eng4_fuel_press, &dwResult);
FSUIPC_Read(0x0A80, 2, &eng4_oiltemp, &dwResult);
FSUIPC_Read(0x0A82, 4, &eng4_oil_press, &dwResult);
FSUIPC_Read(0x0AA0, 4, &eng4_hydr_press, &dwResult);
FSUIPC_Read(0x0A9C, 4, &eng4_vibr, &dwResult);
//Fuel Data
FSUIPC_Read(0x0B90, 4, &tank1_cap, &dwResult);
FSUIPC_Read(0x0B8C, 4, &tank1_per, &dwResult);
FSUIPC_Read(0x0B88, 4, &tank2_cap, &dwResult);
FSUIPC_Read(0x0B84, 4, &tank2_per, &dwResult);
FSUIPC_Read(0x1250, 4, &tank3_cap, &dwResult);
FSUIPC_Read(0x124C, 4, &tank3_per, &dwResult);
FSUIPC_Read(0x0BA8, 4, &tank4_cap, &dwResult);
FSUIPC_Read(0x0BA4, 4, &tank4_per, &dwResult);
FSUIPC_Read(0x0B80, 4, &tank5_6_cap, &dwResult);
FSUIPC_Read(0x0B7C, 4, &tank5_6_per, &dwResult);
FSUIPC_Read(0x1258, 4, &tank5A_cap, &dwResult);
FSUIPC_Read(0x1254, 4, &tank5A_per, &dwResult);
FSUIPC_Read(0x0B98, 4, &tank7_8_cap, &dwResult);
FSUIPC_Read(0x0B94, 4, &tank7_8_per, &dwResult);
FSUIPC_Read(0x1260, 4, &tank7A_cap, &dwResult);
FSUIPC_Read(0x125C, 4, &tank7A_per, &dwResult);
FSUIPC_Read(0x0B78, 4, &tank9_cap, &dwResult);
FSUIPC_Read(0x0B74, 4, &tank9_per, &dwResult);
FSUIPC_Read(0x1248, 4, &tank10_cap, &dwResult);
FSUIPC_Read(0x1244, 4, &tank10_per, &dwResult);
FSUIPC_Read(0x1250, 4, &tank11_cap, &dwResult);
FSUIPC_Read(0x124C, 4, &tank11_per, &dwResult);
FSUIPC_Process(&dwResult);
//Print Environment Data
printf("Environment Data\n");
printf("Time: %d:%d:%d\n",time_hours, time_minutes, time_seconds);
printf("Ground Altitude (ft): %0.1f\n",ground_altitude/78.0288);
printf("Surface Wind Speed (knots): %d\n",surface_wind_speed);
printf("Surface Wind Direction: %0.1f\n",surface_wind_direction*0.005493164);
//Print Radios and Navagation Data
printf("\nRadio and Navagation Data\n");
//printf("NAV1 Frequency: 1%s\n",nav1_freq);
//printf("NAV2 Frequency: 1%s\n",nav2_freq);
printf("VOR1 DME Distance (nm): %0.1f\n",vor1_dist/10.00001);
printf("VOR1 DME Speed (kts): %0.1f\n",vor1_speed/10.00001);
printf("VOR1 DME ETA (secs): %0.1f\n",vor1_eta/10.00001);
printf("VOR2 DME Distance (nm): %0.1f\n",vor2_dist/10.00001);
printf("VOR2 DME Speed (kts): %0.1f\n",vor2_speed/10.00001);
printf("VOR2 DME ETA (secs): %0.1f\n",vor2_eta/10.00001);
//Print Aircraft Performance Data
printf("\nAircraft Data\n");
printf("Gyro Compass Heading: %0.1f\n",compass);
printf("Acceleration (g): %0.1f\n",acceleration/608.00001);
printf("TAS (knots): %0.1f\n",TAS/128.00001);
printf("IAS (knots): %0.1f\n",IAS/128.00001);
printf("Ground Speed (m/s): %0.1f\n",ground_speed/65536.0001);
printf("Vertical Speed (ft/min): %0.1f\n",vertical_speed*0.768946875);
printf("Center of mavity: %0.1f\n",cofg*100);
//Print Aircraft Engine Data
printf("\nAircraft Engine Information\n");
printf("Engine 1 N1: %0.1f N2: %0.1f EGT: %0.1f Fuel Flow: %0.1f Fuel Pressure: %0.1f Oil Temp: %0.1f Oil Pressure: %0.1f Hydraulic Pressure: %0.1f Vibration: %0.1f\n",eng1_n1/163.84, eng1_n2/163.84, eng1_egt/19.05, eng1_ff*0.4536, eng1_fuel_press/144.001, eng1_oiltemp/90.5, eng1_oil_press/297.886, eng1_hydr_press/4.0001, eng1_vibr/3276.8);
printf("Engine 2 N1: %0.1f N2: %0.1f EGT: %0.1f Fuel Flow: %0.1f Fuel Pressure: %0.1f Oil Temp: %0.1f Oil Pressure: %0.1f Hydraulic Pressure: %0.1f Vibration: %0.1f\n",eng2_n1/163.84, eng2_n2/163.84, eng2_egt/19.05, eng2_ff*0.4536, eng2_fuel_press/144.001, eng2_oiltemp/90.5, eng2_oil_press/297.886, eng2_hydr_press/4.0001, eng2_vibr/3276.8);
printf("Engine 3 N1: %0.1f N2: %0.1f EGT: %0.1f Fuel Flow: %0.1f Fuel Pressure: %0.1f Oil Temp: %0.1f Oil Pressure: %0.1f Hydraulic Pressure: %0.1f Vibration: %0.1f\n",eng3_n1/163.84, eng3_n2/163.84, eng3_egt/19.05, eng3_ff*0.4536, eng3_fuel_press/144.001, eng3_oiltemp/90.5, eng3_oil_press/297.886, eng3_hydr_press/4.0001, eng3_vibr/3276.8);
printf("Engine 4 N1: %0.1f N2: %0.1f EGT: %0.1f Fuel Flow: %0.1f Fuel Pressure: %0.1f Oil Temp: %0.1f Oil Pressure: %0.1f Hydraulic Pressure: %0.1f Vibration: %0.1f\n",eng4_n1/163.84, eng4_n2/163.84, eng4_egt/19.05, eng4_ff*0.4536, eng4_fuel_press/144.001, eng4_oiltemp/90.5, eng4_oil_press/297.886, eng4_hydr_press/4.0001, eng4_vibr/3276.8);
//Print Fuel Level Data
printf("\nAircraft Fuel Information\n");
printf("Tank 1 Capacity: %0.1f, percentage: %0.1f\n",tank1_cap*fuelweight,tank1_per*fuelpercentage);
printf("Tank 2 Capacity: %0.1f, percentage: %0.1f\n",tank2_cap*fuelweight,tank2_per*fuelpercentage);
printf("Tank 3 Capacity: %0.1f, percentage: %0.1f\n",tank3_cap*fuelweight,tank3_per*fuelpercentage);
printf("Tank 4 Capacity: %0.1f, percentage: %0.1f\n",tank4_cap*fuelweight,tank4_per*fuelpercentage);
printf("Tank 5 & 6 Capacity: %0.1f, percentage: %0.1f\n",tank5_6_cap*fuelweight,tank5_6_per*fuelpercentage);
printf("Tank 5A Capacity: %0.1f, percentage: %0.1f\n",tank5A_cap*fuelweight,tank5A_per*fuelpercentage);
printf("Tank 7 & 8 Capacity: %0.1f, percentage: %0.1f\n",tank7_8_cap*fuelweight,tank7_8_per*fuelpercentage);
printf("Tank 7A Capacity: %0.1f, percentage: %0.1f\n",tank7A_cap*fuelweight,tank7A_per*fuelpercentage);
printf("Tank 9 Capacity: %0.1f, percentage: %0.1f\n",tank9_cap*fuelweight,tank9_per*fuelpercentage);
printf("Tank 10 Capacity: %0.1f, percentage: %0.1f\n",tank10_cap*fuelweight,tank10_per*fuelpercentage);
printf("Tank 11 Capacity: %0.1f, percentage: %0.1f\n",tank11_cap*fuelweight,tank11_per*fuelpercentage);
sleep(300000);
}
}
else{
if(dwResult==12)
printf("ERROR FSUIPC %d\n", dwResult);
printf("Waiting for FS\n");
}
}
return 0;
}
Ok so is there anyway of making this better? Is there something I should learn that would tidy this up?
Cheers!
Last edited: