Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <math.h>
- #define PI 3.141
- //Used to store prototype
- float MOIx(char c, float r);
- float MOIy(char c, float r);
- int main (void)
- {
- //Print the title of the results produced
- printf("=========================\n");
- printf("CENTROIDAL MOI CALCULATOR\n");
- printf("-------------------------\n");
- printf("INPUT:\n\n\n");
- // Used as file pointers
- char inp[50], out[50];
- FILE *input;
- FILE *output;
- // Asking user for input file name
- printf("Please enter the input file name: ");
- scanf("%s", &inp);
- input = fopen(inp, "r");
- while (input == NULL)
- {
- printf("ERROR: Input file does not exist\n");
- // prompt the user to type in the correct filename repeatedly
- printf("Please re-enter the input filename: ");
- scanf("%s", &inp);
- input = fopen(inp, "r");
- }
- // Asking the user for output file name
- printf("Please type in the output filename: ");
- scanf("%s", &out);
- output = fopen(out, "w");
- // Scanning and adding the file heading
- char heading1[10], heading2[10];
- fscanf(input, "%s %s\n", heading1, heading2);
- printf("\n%s %s %s %s\n", heading1, heading2, "MOIx", "MOIy");
- fprintf(output, ("\n%s %s %s %s\n", heading1, heading2, "MOIx", "MOIy"));
- char ch;
- float d;
- // While loop is used
- while(1)
- {
- // Scan the radius and type
- fscanf(input, "%c ", &ch);
- fscanf(input, "%f\n", &d);
- // Print the result
- printf("%c ", ch);
- printf("%0.3e ", d);
- fprintf(output, "%c ", ch);
- fprintf(output, "%0.3e ", d);
- // Results stored for the funtions
- float value_MOIx = MOIx(ch, d);
- float value_MOIy = MOIy(ch, d);
- // Error check
- if (value_MOIx == 0 || value_MOIy == 0)
- {
- printf("INPUT ERROR\n");
- fprintf(output, "INPUT ERROR\n");
- }
- else
- {
- // Print the MOI values
- printf("%0.3e ", value_MOIx);
- printf("%0.3e\n", value_MOIy);
- fprintf(output, "%0.3e ", value_MOIx);
- fprintf(output, "%0.3e\n", value_MOIy);
- }
- if (feof(input))
- {
- break;
- }
- }
- }
- // Formula for calculating MOI about x-axis
- float MOIx(char c, float r)
- {
- float result;
- if (r <= 0)
- {
- // If the radius is zero the result returned will also be zero
- result = 0;
- return result;
- }
- else
- {
- if (c == 'A')
- {
- result = (PI/4)*(pow(r, 4));
- return result;
- }
- else if (c == 'B')
- {
- result = ((PI/ 8) - (8 / (9 * PI))) * (pow(r, 4));
- return result;
- }
- else if (c == 'C')
- {
- result = 0.5 * ((PI / 8) - (8 / (9 * PI))) * (pow(r, 4));
- return result;
- }
- else
- {
- // If formula that is not defined is used the result returned will be zero
- result = 0;
- return result;
- }
- }
- }
- // Formula to calculate MOI about y-axis
- float MOIy(char c, float r)
- {
- float result;
- // If the radius is zero the result returned will also be zero
- if (r <= 0)
- {
- result = 0;
- return result;
- }
- else
- {
- if (c == 'A')
- {
- result = (PI/4)*(pow(r, 4));
- return result;
- }
- else if (c == 'B')
- {
- result = (PI/8)*(pow(r, 4));
- return result;
- }
- else if (c == 'C')
- {
- result = (PI/8)*(pow(r, 4));
- return result;
- }
- else
- {
- // If formula that is not defined is used the result returned will be zero
- result = 0;
- return result;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement