Advertisement
zodiak1

Untitled

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