Advertisement
shamzed

moi

May 30th, 2022
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. // calculating the moment of inertia for different shapes
  2.  
  3. #include <stdio.h>
  4. #include <math.h>
  5.  
  6. // prototype
  7. float MOIx(char c, float r);
  8. float MOIy(char c, float r);
  9.  
  10. int main(void)
  11. {
  12. // creating buffers for storing the filenames for input and output files respectively
  13. char inp[20], out[20];
  14. FILE *input;
  15. FILE *output;
  16.  
  17. printf("=========================\n");
  18. printf("CENTROIDAL MOI CALCULATOR\n");
  19. printf("-------------------------\n");
  20. printf("INPUT:\n\n\n");
  21.  
  22. // taking input filename from user
  23. printf("Please enter the input file name: ");
  24. scanf("%s", &inp);
  25.  
  26. // opening the file in "read" mode
  27. input = fopen(inp, "r");
  28.  
  29. // checking whether the file exists
  30. while (input == NULL)
  31. {
  32. printf("ERROR: Input file does not exist\n");
  33.  
  34. // prompt the user to type in the correct filename repeatedly
  35. printf("Please re-enter the input filename: ");
  36. scanf("%s", &inp);
  37. input = fopen(inp, "r");
  38. }
  39.  
  40. // prompt the user to type in the output fileanme
  41. printf("Please type in the output filename: ");
  42. scanf("%s", &out);
  43. output = fopen(out, "w");
  44.  
  45.  
  46.  
  47. // scanning header from the input file and printing on screen
  48. char header1[5], header2[7];
  49. fscanf(input, "%s %s\n", header1, header2);
  50. printf("\n%s %s %s %s\n", header1, header2, "MOIx", "MOIy");
  51.  
  52. // printing the header for the output file
  53. fprintf(output, "Type Radius MOIx MOIy\n");
  54.  
  55. char ch;
  56. float d;
  57. // creating a while loop which loops over each line until it reaches the end
  58. while(1)
  59. {
  60. // scanning the type and radius from input file
  61. fscanf(input, "%c ", &ch);
  62. fscanf(input, "%f\n", &d);
  63.  
  64. // printing to the screen
  65. printf("%c ", ch);
  66. printf("%0.3e ", d);
  67.  
  68. // printing the type and radius to the output file
  69. fprintf(output, "%c ", ch);
  70. fprintf(output, "%0.3e ", d);
  71.  
  72. // calling the both the functions and storing the return value
  73. float value_MOIx = MOIx(ch, d);
  74. float value_MOIy = MOIy(ch, d);
  75.  
  76. // checking for errors
  77. if (value_MOIx == 0 || value_MOIy == 0)
  78. {
  79. printf("INPUT ERROR\n");
  80. fprintf(output, "INPUT ERROR\n");
  81. }
  82. else
  83. {
  84. // printing the MOI values to screen
  85. printf("%0.3e ", value_MOIx);
  86. printf("%0.3e\n", value_MOIy);
  87.  
  88. // printing the MOI values to output file
  89. fprintf(output, "%0.3e ", value_MOIx);
  90. fprintf(output, "%0.3e\n", value_MOIy);
  91.  
  92. }
  93.  
  94. // breaks out of while loop on reaching the end of the file (EOF)
  95. if (feof(input))
  96. {
  97. break;
  98. }
  99. }
  100.  
  101. // closing both the files when program ends
  102. fclose(input);
  103. fclose(output);
  104. }
  105.  
  106. // MOI about centroidal x-axis
  107. float MOIx(char c, float r)
  108. {
  109. float result;
  110. if (r <= 0)
  111. {
  112. // if the radius is less than equal to zero, then result is zero
  113. result = 0;
  114. return result;
  115. }
  116.  
  117. else
  118. {
  119. if (c == 'A')
  120. {
  121. result = (3.142/4)*(pow(r, 4));
  122. return result;
  123. }
  124. else if (c == 'B')
  125. {
  126. result = ((3.142 / 8) - (8 / (9 * 3.142))) * (pow(r, 4));
  127. return result;
  128. }
  129. else if (c == 'C')
  130. {
  131. result = 0.5 * ((3.142 / 8) - (8 / (9 * 3.142))) * (pow(r, 4));
  132. return result;
  133. }
  134. else
  135. {
  136. // choosing anything other than A, B, C will give return 0
  137. result = 0;
  138. return result;
  139. }
  140. }
  141. }
  142.  
  143. // MOI about centroidal y-axis
  144. float MOIy(char c, float r)
  145. {
  146. float result;
  147.  
  148. // checking for input error
  149. if (r <= 0)
  150. {
  151. result = 0;
  152. return result;
  153. }
  154. else
  155. {
  156. if (c == 'A')
  157. {
  158. result = (3.142/4)*(pow(r, 4));
  159. return result;
  160. }
  161. else if (c == 'B')
  162. {
  163. result = (3.142/8)*(pow(r, 4));
  164. return result;
  165. }
  166. else if (c == 'C')
  167. {
  168. result = (3.142/8)*(pow(r, 4));
  169. return result;
  170. }
  171. else
  172. {
  173. // choosing anything other than A, B, C will give return 0
  174. result = 0;
  175. return result;
  176. }
  177. }
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement