Advertisement
zodiak1

Untitled

Feb 13th, 2023
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.31 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. integer t_type
  10. real t_side, t_angle
  11. logical is_t_configured *1
  12.  
  13. common /triangle/ t_type, t_side, t_angle
  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_type, t_side, t_angle)
  37. elseif (chosen_action .eq. 3) then
  38. print *, 'Min angle = ', calc_t_min_angle(t_type, t_angle)
  39. elseif (chosen_action .eq. 4) then
  40. print *, 'Cosine of the min angle = ',
  41. . calc_t_min_angle_cos(t_type, t_angle)
  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. integer t_type
  95. real t_side, t_angle
  96. logical validate_t_type, validate_t_side, validate_t_angle
  97.  
  98. common /triangle/ t_type, t_side, t_angle
  99.  
  100. * Set triangle type
  101. 30 print *, '------- Start triangle configuration -------'
  102. print *, 'Choose the type of triangle (enter type number):'
  103. print *, '1) Equilateral triangle;'
  104. print *, '2) Right triangle.'
  105.  
  106. read *, t_type
  107.  
  108. if (.not.(validate_t_type(t_type))) then
  109. goto 30
  110. endif
  111.  
  112. * Set triangle angle
  113. if (t_type .eq. 1) then
  114. t_angle = 60
  115. elseif (t_type .eq. 2) then
  116. 31 print *, 'Enter the opposite angle (in degrees):'
  117. read *, t_angle
  118.  
  119. if (.not.(validate_t_angle(t_type, t_angle))) then
  120. goto 31
  121. endif
  122. endif
  123.  
  124. * Set triangle side
  125. 32 if (t_type .eq. 1) then
  126. print *, 'Enter the length of the leg (in m):'
  127. else
  128. print *, 'Enter the length of the side (in m):'
  129. endif
  130.  
  131. read *, t_side
  132.  
  133. if (.not.(validate_t_side(t_side))) then
  134. goto 32
  135. endif
  136.  
  137. print *, '------- End of triangle configuration -------'
  138.  
  139. return
  140. end
  141.  
  142. * Validate chosen triangle type. Show error if type is undefined.
  143. logical function validate_t_type(t_type)
  144.  
  145. integer t_type
  146.  
  147. validate_t_type = .true.
  148.  
  149. if (t_type .lt. 1 .or. t_type .gt. 2) then
  150. print *
  151. print *, '!!! Chosen triangle type is undefined.'
  152. print *
  153.  
  154. validate_t_type = .false.
  155. endif
  156.  
  157. return
  158. end
  159.  
  160. * Validate triangle angle value. Show error if angle isn't correct.
  161. logical function validate_t_angle(t_type, t_angle)
  162.  
  163. integer t_type
  164. real t_angle
  165.  
  166. validate_t_angle = .true.
  167.  
  168. if (t_angle .le. 0) then
  169. print *
  170. print *, '!!! Angle can''t be less than or equals to 0.'
  171. print *
  172.  
  173. validate_t_angle = .false.
  174. endif
  175.  
  176. if (t_type .eq. 2 .and. t_angle .ge. 90) then
  177. print *
  178. print *, '!!! Angle can''t be greater than 90.'
  179. print *
  180.  
  181. validate_t_angle = .false.
  182. endif
  183.  
  184. return
  185. end
  186.  
  187. * Validate triangle side value. Show error if side isn't correct.
  188. logical function validate_t_side(t_side)
  189.  
  190. real t_side
  191.  
  192. validate_t_side = .true.
  193.  
  194. if (t_side .le. 0) then
  195. print *
  196. print *, '!!! Side can''t be less than or equals to 0.'
  197. print *
  198.  
  199. validate_t_side = .false.
  200. endif
  201.  
  202. return
  203. end
  204.  
  205. * Convert degrees to radians
  206. real function deg_to_rad(degrees)
  207.  
  208. real degrees, PI
  209.  
  210. parameter (PI = 3.14)
  211.  
  212. deg_to_rad = degrees * PI / 180
  213.  
  214. return
  215. end
  216.  
  217. * Calculate the area (in meters^2) of a triangle
  218. real function calc_t_area(t_type, t_side, t_angle)
  219.  
  220. real deg_to_rad
  221. integer t_type
  222. real t_side, t_angle
  223.  
  224.  
  225. if (t_type .eq. 1) then
  226. calc_t_area = sqrt(3.0) / 4.0 * t_side ** 2
  227. else
  228. calc_t_area = t_side ** 2 / (2 * tan(deg_to_rad(t_angle)))
  229. endif
  230.  
  231. return
  232. end
  233.  
  234. * Calculate the minimum angle (in degrees) of a triangle
  235. real function calc_t_min_angle(t_type, t_angle)
  236.  
  237. integer t_type
  238. real t_angle
  239.  
  240. if (t_type .eq. 1) then
  241. calc_t_min_angle = 60
  242. else
  243. calc_t_min_angle = min(t_angle, 90.0 - t_angle)
  244. endif
  245.  
  246. return
  247. end
  248.  
  249. * Calculate the cosine of the minimum angle (in degrees)
  250. real function calc_t_min_angle_cos(t_type, t_angle)
  251.  
  252. real calc_t_min_angle, deg_to_rad
  253. integer t_type
  254. real t_angle, t_min_angle
  255.  
  256. t_min_angle = calc_t_min_angle(t_type, t_angle)
  257. calc_t_min_angle_cos = cos(deg_to_rad(t_min_angle))
  258.  
  259. return
  260. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement