Advertisement
zodiak1

Untitled

Feb 13th, 2023
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. * Note: 't' means 'triangle'
  2.  
  3. program lab1_v4
  4. implicit none
  5.  
  6. integer display_actions, chosen_action
  7. real calc_t_area, calc_t_min_angle, calc_t_min_angle_cos
  8.  
  9. * t_a1 - adjacent angle, t_a2 - opposite angle, t_a3 - third angle
  10. real t_side, t_a1, t_a2, t_a3
  11. logical is_t_configured *1
  12.  
  13. common /triangle/ t_side, t_a1, t_a2, t_a3
  14.  
  15. is_t_configured = .false.
  16.  
  17. 10 chosen_action = display_actions()
  18. print *
  19.  
  20. if (chosen_action .eq. 5) stop
  21.  
  22. * Block the chosen action if the triangle data is not set, and display the actions menu again
  23. if (chosen_action .ne. 1 .and. .not.(is_t_configured)) then
  24. print *, '!!! Can''t calculate because triangle data ' //
  25. . 'isn''t set.'
  26. print *
  27.  
  28. goto 10
  29. endif
  30.  
  31. * Perform the chosen action
  32. if (chosen_action .eq. 1) then
  33. call set_t_data()
  34. is_t_configured = .true.
  35. elseif (chosen_action .eq. 2) then
  36. print *, 'Area = ', calc_t_area(t_side, t_a1, t_a2, t_a3)
  37. elseif (chosen_action .eq. 3) then
  38. print *, 'Min angle = ', calc_t_min_angle(t_a1, t_a2, t_a3)
  39. elseif (chosen_action .eq. 4) then
  40. print *, 'Cosine of the min angle = ',
  41. . calc_t_min_angle_cos(t_a1, t_a2, t_a3)
  42. endif
  43.  
  44. * Display the actions menu again
  45. print *
  46. goto 10
  47.  
  48. end
  49.  
  50.  
  51. * Displays the action menu so that the user choices the one of them
  52. * returns: number of the chosen menu action
  53. integer function display_actions()
  54.  
  55. logical validate_chosen_action
  56.  
  57. 20 print *, 'Choose an action (enter action number):'
  58. print *, '1) Set triangle data;'
  59. print *, '2) Calculate the area of a triangle;'
  60. print *, '3) Calculate the minimum angle of a triangle;'
  61. print *, '4) Calculate the cosine of the minimum angle;'
  62. print *, '5) End the program.'
  63.  
  64. read *, display_actions
  65.  
  66. if (.not.(validate_chosen_action(display_actions))) then
  67. goto 20
  68. endif
  69.  
  70. return
  71. end
  72.  
  73. * Validate chosen actions menu item by it number. Show error if action isn't valid
  74. logical function validate_chosen_action(chosen_action)
  75.  
  76. integer chosen_action
  77.  
  78. validate_chosen_action = .true.
  79.  
  80. if (chosen_action .lt. 1 .or. chosen_action .gt. 5) then
  81. print *
  82. print *, 'Chosen action is undefined.'
  83. print *
  84.  
  85. validate_chosen_action = .false.
  86. endif
  87.  
  88. return
  89. end
  90.  
  91. * Set triangle data
  92. subroutine set_t_data()
  93.  
  94. real t_side, t_a1, t_a2, t_a3
  95. logical validate_t_side, validate_t_angles
  96.  
  97. common /triangle/ t_side, t_a1, t_a2, t_a3
  98.  
  99. print *, '------- Start triangle configuration -------'
  100.  
  101. * Set triangle adjacent angle
  102. 30 print *, 'Enter the adjacent angle (in degrees):'
  103. read *, t_a1
  104.  
  105. print *, 'Enter the opposite angle (in degrees):'
  106. read *, t_a2
  107.  
  108. if (.not.(validate_t_angles(t_a1, t_a2))) then
  109. goto 30
  110. endif
  111.  
  112. t_a3 = 180 - t_a1 - t_a2
  113.  
  114. * Set triangle side
  115. 31 print *, 'Enter the length of the side (in m):'
  116. read *, t_side
  117.  
  118. if (.not.(validate_t_side(t_side))) then
  119. goto 31
  120. endif
  121.  
  122. print *, '------- End of triangle configuration -------'
  123.  
  124. return
  125. end
  126.  
  127. * Validate triangle angle value. Show error if angle isn't correct
  128. logical function validate_t_angles(t_a1, t_a2)
  129.  
  130. real t_a1, t_a2
  131.  
  132. validate_t_angles = .true.
  133.  
  134. if (t_a1 .le. 0 .or. t_a2 .le.0) then
  135. print *
  136. print *, '!!! Angle can''t be less than or equals to 0.'
  137. print *
  138.  
  139. validate_t_angles = .false.
  140. endif
  141.  
  142. if (t_a1 + t_a2 .ge. 180) then
  143. print *
  144. print *, '!!! Sum of angles can''t be greater than' //
  145. . 'or equals to 180.'
  146. print *
  147.  
  148. validate_t_angles = .false.
  149. endif
  150.  
  151. return
  152. end
  153.  
  154. * Validate triangle side value. Show error if side isn't correct
  155. logical function validate_t_side(t_side)
  156.  
  157. real t_side
  158.  
  159. validate_t_side = .true.
  160.  
  161. if (t_side .le. 0) then
  162. print *
  163. print *, '!!! Side can''t be less than or equals to 0.'
  164. print *
  165.  
  166. validate_t_side = .false.
  167. endif
  168.  
  169. return
  170. end
  171.  
  172. * Convert degrees to radians
  173. real function deg_to_rad(degrees)
  174.  
  175. real degrees, PI
  176.  
  177. parameter (PI = 3.14)
  178.  
  179. deg_to_rad = degrees * PI / 180
  180.  
  181. return
  182. end
  183.  
  184. * Calculate the area (in meters^2) of a triangle
  185. real function calc_t_area(t_side, t_a1, t_a2, t_a3)
  186.  
  187. real deg_to_rad
  188. real t_side, t_a1, t_a2, t_a3
  189.  
  190. calc_t_area = t_side ** 2 * sin(deg_to_rad(t_a3))
  191. . * sin(deg_to_rad(t_a2)) / 2 / sin(deg_to_rad(t_a1))
  192.  
  193. return
  194. end
  195.  
  196. * Calculate the minimum angle (in degrees) of a triangle
  197. real function calc_t_min_angle(t_a1, t_a2, t_a3)
  198.  
  199. real t_a1, t_a2, t_a3
  200.  
  201. calc_t_min_angle = min(t_a1, t_a2, t_a3)
  202.  
  203. return
  204. end
  205.  
  206. * Calculate the cosine of the minimum angle (in degrees)
  207. real function calc_t_min_angle_cos(t_a1, t_a2, t_a3)
  208.  
  209. real calc_t_min_angle, deg_to_rad
  210. real t_a1, t_a2, t_a3
  211.  
  212. t_min_angle = calc_t_min_angle(t_a1, t_a2, t_a3)
  213. calc_t_min_angle_cos = cos(deg_to_rad(t_min_angle))
  214.  
  215. return
  216. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement