Advertisement
shamzed

ishmum-main

Jun 5th, 2022
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. #define PI 3.141
  4.  
  5.  
  6. //Used to store prototype
  7. float MOIx(char c, float r);
  8. float MOIy(char c, float r);
  9.  
  10.  
  11. int main (void)
  12.  
  13.  
  14. {
  15.  
  16. //Print the title of the results produced
  17. printf("=========================\n");
  18. printf("CENTROIDAL MOI CALCULATOR\n");
  19. printf("-------------------------\n");
  20. printf("INPUT:\n\n\n");
  21.  
  22.  
  23. // Used as file pointers
  24. char inp[50], out[50];
  25. FILE *input;
  26. FILE *output;
  27.  
  28.  
  29. // Asking user for input file name
  30. printf("Please enter the input file name: ");
  31. scanf("%s", &inp);
  32. input = fopen(inp, "r");
  33.  
  34. while (input == NULL)
  35. {
  36. printf("ERROR: Input file does not exist\n");
  37.  
  38. // prompt the user to type in the correct filename repeatedly
  39. printf("Please re-enter the input filename: ");
  40. scanf("%s", &inp);
  41. input = fopen(inp, "r");
  42. }
  43.  
  44. // Asking the user for output file name
  45. printf("Please type in the output filename: ");
  46. scanf("%s", &out);
  47. output = fopen(out, "w");
  48.  
  49.  
  50.  
  51. // Scanning and adding the file heading
  52. char heading1[10], heading2[10];
  53. fscanf(input, "%s %s\n", heading1, heading2);
  54. printf("\n%s %s %s %s\n", heading1, heading2, "MOIx", "MOIy");
  55. fprintf(output, ("\n%s %s %s %s\n", heading1, heading2, "MOIx", "MOIy"));
  56.  
  57.  
  58. char ch;
  59. float d;
  60.  
  61.  
  62. // While loop is used
  63. while(1)
  64. {
  65. // Scan the radius and type
  66. fscanf(input, "%c ", &ch);
  67. fscanf(input, "%f\n", &d);
  68.  
  69. // Print the result
  70. printf("%c ", ch);
  71. printf("%0.3e ", d);
  72. fprintf(output, "%c ", ch);
  73. fprintf(output, "%0.3e ", d);
  74.  
  75. // Results stored for the funtions
  76. float value_MOIx = MOIx(ch, d);
  77. float value_MOIy = MOIy(ch, d);
  78.  
  79. // Error check
  80. if (value_MOIx == 0 || value_MOIy == 0)
  81. {
  82. printf("INPUT ERROR\n");
  83. fprintf(output, "INPUT ERROR\n");
  84. }
  85. else
  86. {
  87. // Print the MOI values
  88. printf("%0.3e ", value_MOIx);
  89. printf("%0.3e\n", value_MOIy);
  90. fprintf(output, "%0.3e ", value_MOIx);
  91. fprintf(output, "%0.3e\n", value_MOIy);
  92. }
  93.  
  94. if (feof(input))
  95.  
  96. {
  97. break;
  98. }
  99. }
  100.  
  101. }
  102. // Formula for calculating MOI about x-axis
  103. float MOIx(char c, float r)
  104. {
  105. float result;
  106. if (r <= 0)
  107. {
  108. // If the radius is zero the result returned will also be zero
  109. result = 0;
  110. return result;
  111. }
  112. else
  113. {
  114. if (c == 'A')
  115. {
  116. result = (PI/4)*(pow(r, 4));
  117. return result;
  118. }
  119. else if (c == 'B')
  120. {
  121. result = ((PI/ 8) - (8 / (9 * PI))) * (pow(r, 4));
  122. return result;
  123. }
  124. else if (c == 'C')
  125. {
  126. result = 0.5 * ((PI / 8) - (8 / (9 * PI))) * (pow(r, 4));
  127. return result;
  128. }
  129. else
  130. {
  131. // If formula that is not defined is used the result returned will be zero
  132. result = 0;
  133. return result;
  134. }
  135. }
  136. }
  137.  
  138.  
  139. // Formula to calculate MOI about y-axis
  140.  
  141. float MOIy(char c, float r)
  142. {
  143. float result;
  144.  
  145. // If the radius is zero the result returned will also be zero
  146. if (r <= 0)
  147. {
  148. result = 0;
  149. return result;
  150. }
  151. else
  152. {
  153. if (c == 'A')
  154. {
  155. result = (PI/4)*(pow(r, 4));
  156. return result;
  157. }
  158. else if (c == 'B')
  159. {
  160. result = (PI/8)*(pow(r, 4));
  161. return result;
  162. }
  163. else if (c == 'C')
  164. {
  165. result = (PI/8)*(pow(r, 4));
  166. return result;
  167. }
  168. else
  169. {
  170. // If formula that is not defined is used the result returned will be zero
  171. result = 0;
  172. return result;
  173. }
  174. }
  175. }
  176.  
  177.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement