Code:
#include<stdio.h>
void menu (int* choice);
void ac (int choice, FILE* plane,int input[], int* MAX_WEIGHT, int* MAX_CG, int arm[]);
void getdata (FILE* plane, int input []);
int calc (int MAX_WEIGHT, int MAX_CG,int input[], int arm[], int* weight, int* balance);
void print( int weight, int balance, int oepration);
int main (void)
{
int choice;
FILE* plane;
int input [15] = {0};
int arm [6] = {0};
int weight, balance = 0;
int MAX_WEIGHT, MAX_CG;//upper limits of the weight and CG per plane
int operation;
menu(&choice);
ac(choice, plane, input, &MAX_WEIGHT, &MAX_CG, arm); // ac or a/c is short for Aircraft. Easier separation between function and file called plane
/* plane = fopen("C172.DAT", "r");
input[0] = 1491;
MAX_WEIGHT = 2550;
MAX_CG = 47;
*/
getdata(plane, input);
operation = calc (MAX_WEIGHT, MAX_CG,input, arm, &weight, &balance); // return value of '1' will be inside limits, '0' will be outside limits
print(weight, balance, operation);
return 0;
}
//=================================menu function================================
void menu (int* choice)
{
printf("****************WELCOME!****************");
printf("\n");
printf("This program enabels you to calculate\n");
printf("your weight and balance. Please enter\n");
printf("a number and key <retrun> to make your\n");
printf("selection from the menu below:\n");
printf("\n");
printf("1| Cessna 172\n");
printf("2| Cherokee 6\n");
printf("3| Piper Cub\n\n\n");
printf("Enter your choice:");
scanf("%d", choice);
return;
}
//=====================================select data=========================
void ac (int choice, FILE* plane, int input[], int* MAX_WEIGHT, int* MAX_CG, int arm[])
{
switch (choice)
{
case 1: plane = fopen("C172.DAT", "r");
input[0] = 1491;
*MAX_WEIGHT = 2550;
*MAX_CG = 47;
arm[0] = 39;
arm[1] = 35;
arm[2] = 35;
arm[3] = 74;
arm[4] = 97;
arm[5] = 116;
break;
case 2: plane = fopen("Piper6.DAT", "r");
input[0] = 1980 ;
*MAX_WEIGHT = 3674 ;
*MAX_CG = 96;
arm[0] = 85;
arm[1] = 85;
arm[2] = 95;
arm[3] = 118;
arm[4] = 156;
arm[5] = 179;
break;
case 3: plane = fopen("Cub.DAT", "r");
input[0] = 765.7;
*MAX_WEIGHT = 1200;
*MAX_CG = 23.7;
arm[0] = 17;
arm[1] = 9;
arm[2] = 36;
arm[3] = -18;
arm[4] = 49;
break;
}
return;
}
//============================================getting data===============
void getdata (FILE* plane, int input[])
{
char charin;
int i = 1;
while( (charin = fgetc(plane)) != EOF)
{
printf("%c", charin);
while( (charin = fgetc(plane)) != '\n' )
{
printf("%c", charin);
}
scanf("%d", &input[i]);
i++;
}
return;
}
//===================calc data=========================
int calc (int MAX_WEIGHT, int MAX_CG, int input[], int arm[], int* weight, int* balance)
{
int moment;
*weight = 0;
for(int i = 0; i <=5; i++)
{
*weight += input[i] ;
*balance += (input[i] * arm [i]); // one easy way to calculate the moment of inertia is to multiply the wieght by the arm and add it all up
}//for
printf("%d, %d", *weight , * balance);
if ( (*weight > MAX_WEIGHT) || ((*balance / *weight) > MAX_CG) )
return 0;
else
return 1;
}
//=========================print data======================
void print( int weight, int balance, int operation)
{
if (operation == 0)
{printf("\nI'm sorry, but your plane is not withing the flight envelope.");
printf("\nPlease make adjustments so it is within proper weight and balance.\n");
}
else
{
printf("\n\nYour total weight is: %d\n", weight);
printf("Your CG is: %d\n", (balance / weight ) );
}
return;
}//print results
]0;flyboy@csc-linux1:~[flyboy@csc-linux1 ~]$ gcc std[K[K[K-std=c99 term.c
]0;flyboy@csc-linux1:~[flyboy@csc-linux1 ~]$ a.out
****************WELCOME!****************
This program enabels you to calculate
your weight and balance. Please enter
a number and key <retrun> to make your
selection from the menu below:
1| Cessna 172
2| Cherokee 6
3| Piper Cub
Enter your choice:1
1
1
1
1
^C
]0;flyboy@csc-linux1:~[flyboy@csc-linux1 ~]$ exit
exit
Script done on Thu 30 Apr 2009 11:08:53 PM CDT