Advertisement
zodiak1

Untitled

Feb 12th, 2023
2,147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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. 1     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.      .            'is not specified. Specify it.'
  26.          print *
  27.          goto 1
  28.       endif
  29.  
  30. * Perform the chosen action
  31.       if (chosen_action .eq. 1) then
  32.          call customize_t()
  33.          is_t_configured = .true.
  34.       elseif (chosen_action .eq. 2) then
  35.          print *, 'Area = ', calc_t_area(t_type, t_side, t_angle)
  36.       elseif (chosen_action .eq. 3) then
  37.          print *, 'Min angle = ', calc_t_min_angle(t_type, t_angle)
  38.       elseif (chosen_action .eq. 4) then
  39.          print *, 'Cosine of the min angle = ',
  40.      .            calc_t_min_angle_cos(t_type, t_angle)
  41.       endif
  42.  
  43. * Display the actions menu again
  44.       print *
  45.       goto 1
  46.  
  47.       end
  48.  
  49.  
  50. * Displays the action menu so that the user choices the one of them
  51. * returns: number of the chosen menu action
  52.       integer function display_actions()
  53.  
  54.       logical validate_chosen_action
  55.  
  56. 2     print *, 'Choose an action (enter action number):'
  57.       print *, '1) Configure triangle data;'
  58.       print *, '2) Calculate the area of a triangle;'
  59.       print *, '3) Calculate the minimum angle of a triangle;'
  60.       print *, '4) Calculate the cosine of the minimum angle;'
  61.       print *, '5) End the program.'
  62.  
  63.       read *, display_actions
  64.  
  65.       if (.not.(validate_chosen_action(display_actions))) then
  66.          goto 2
  67.       endif
  68.  
  69.       return
  70.       end
  71.  
  72. * Validate chosen actions menu item by it number. Show error if action isn't valid
  73.      logical function validate_chosen_action(chosen_action)
  74.  
  75.      integer chosen_action
  76.  
  77.      validate_chosen_action = .true.
  78.  
  79.      if (chosen_action .lt. 1 .or. chosen_action .gt. 5) then
  80.         print *
  81.         print *, 'Chosen action is undefined. Try again.'
  82.         print *
  83.  
  84.         validate_chosen_action = .false.
  85.      endif
  86.  
  87.      return
  88.      end
  89.  
  90. * Configure triangle data
  91.      subroutine customize_t()
  92.  
  93.      integer t_type
  94.      real t_side, t_angle
  95.  
  96.      common /triangle/ t_type, t_side, t_angle
  97.  
  98.      print *, '------- Triangle configuration -------'
  99.      print *, 'Choose the type of triangle (enter type number):'
  100.      print *, '1) Equilateral triangle;'
  101.      print *, '2) Right triangle.'
  102.  
  103.      read *, t_type
  104.  
  105.      if (t_type .eq. 1) then
  106.         t_angle = 60
  107.  
  108.         print *, 'Enter the length of the side (in cm):'
  109.      elseif (t_type .eq. 2) then
  110.         print *, 'Enter the opposite angle (in degrees):'
  111.         read *, t_angle
  112.  
  113.         print *, 'Enter the length of the leg (in cm):'
  114.      endif
  115.  
  116.      read *, t_side
  117.  
  118.  
  119.      print *, '------- End of triangle configuration -------'
  120.  
  121.      return
  122.      end
  123.  
  124. * Validate triangle data. Show error if data isn't valid.
  125.       logical subroutine validate_t_data(t_type, t_side, t_angle)
  126.  
  127.       integer t_type
  128.       real t_side, t_angle
  129.  
  130.       if (t_type .eq. 1)
  131.  
  132.       return
  133.       end
  134.  
  135. * Calculate the area of a triangle
  136.       real function calc_t_area(t_type, t_side, t_angle)
  137.  
  138.       integer t_type
  139.       real t_side, t_angle
  140.  
  141.       if (t_type .eq. 1) then
  142.          calc_t_area = sqrt(3.0) / 4.0 * t_side ** 2
  143.       else
  144.          calc_t_area = 1
  145.       endif
  146.  
  147.       return
  148.       end
  149.  
  150. * Calculate the minimum angle of a triangle
  151.       real function calc_t_min_angle(t_type, t_angle)
  152.  
  153.       integer t_type
  154.       real t_angle
  155.  
  156.       if (t_type .eq. 1) then
  157.          calc_t_min_angle = 60
  158.       else
  159.          calc_t_min_angle = min(t_angle, 90.0 - t_angle)
  160.       endif
  161.  
  162.       return
  163.       end
  164.  
  165. * Calculate the cosine of the minimum angle
  166.       real function calc_t_min_angle_cos(t_type, t_angle)
  167.  
  168.       real calc_t_min_angle
  169.       integer t_type
  170.       real t_angle, t_min_angle, PI
  171.  
  172.       parameter PI = 3.141592
  173.  
  174.       t_min_angle = calc_t_min_angle(t_type, t_angle)
  175.       calc_t_min_angle_cos = cos(t_min_angle * PI / 180.0)
  176.  
  177.       return
  178.       end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement