* t - triangle
program lab1_v4
implicit none
integer display_actions, chosen_action
real calc_t_area, calc_t_min_angle, calc_t_min_angle_cos
integer t_type
real t_side, t_angle
logical is_t_configured *1
common /triangle/ t_type, t_side, t_angle
is_t_configured = .false.
1 chosen_action = display_actions()
print *
if (chosen_action .eq. 5) stop
* Block the chosen action if the triangle data is not set, and display the actions menu again
if (chosen_action .ne. 1 .and. .not.(is_t_configured)) then
print *, '!!! Can''t calculate because triangle data ' //
. 'is not specified. Specify it.'
print *
goto 1
endif
* Perform the chosen action
if (chosen_action .eq. 1) then
call customize_t()
is_t_configured = .true.
elseif (chosen_action .eq. 2) then
print *, 'Area = ', calc_t_area(t_type, t_side, t_angle)
elseif (chosen_action .eq. 3) then
print *, 'Min angle = ', calc_t_min_angle(t_type, t_angle)
elseif (chosen_action .eq. 4) then
print *, 'Cosine of the min angle = ',
. calc_t_min_angle_cos(t_type, t_angle)
endif
* Display the actions menu again
print *
goto 1
end
* Displays the action menu so that the user choices the one of them
* returns: number of the chosen menu action
integer function display_actions()
logical validate_chosen_action
2 print *, 'Choose an action (enter action number):'
print *, '1) Configure triangle data;'
print *, '2) Calculate the area of a triangle;'
print *, '3) Calculate the minimum angle of a triangle;'
print *, '4) Calculate the cosine of the minimum angle;'
print *, '5) End the program.'
read *, display_actions
if (.not.(validate_chosen_action(display_actions))) then
goto 2
endif
return
end
* Validate chosen actions menu item by it number. Show error if action isn't valid
logical function validate_chosen_action(chosen_action)
integer chosen_action
validate_chosen_action = .true.
if (chosen_action .lt. 1 .or. chosen_action .gt. 5) then
print *
print *, 'Chosen action is undefined. Try again.'
print *
validate_chosen_action = .false.
endif
return
end
* Configure triangle data
subroutine customize_t()
integer t_type
real t_side, t_angle
common /triangle/ t_type, t_side, t_angle
print *, '------- Triangle configuration -------'
print *, 'Choose the type of triangle (enter type number):'
print *, '1) Equilateral triangle;'
print *, '2) Right triangle.'
read *, t_type
if (t_type .eq. 1) then
t_angle = 60
print *, 'Enter the length of the side (in cm):'
elseif (t_type .eq. 2) then
print *, 'Enter the opposite angle (in degrees):'
read *, t_angle
print *, 'Enter the length of the leg (in cm):'
endif
read *, t_side
print *, '------- End of triangle configuration -------'
return
end
* Validate triangle data. Show error if data isn't valid.
logical subroutine validate_t_data(t_type, t_side, t_angle)
integer t_type
real t_side, t_angle
if (t_type .eq. 1)
return
end
* Calculate the area of a triangle
real function calc_t_area(t_type, t_side, t_angle)
integer t_type
real t_side, t_angle
if (t_type .eq. 1) then
calc_t_area = sqrt(3.0) / 4.0 * t_side ** 2
else
calc_t_area = 1
endif
return
end
* Calculate the minimum angle of a triangle
real function calc_t_min_angle(t_type, t_angle)
integer t_type
real t_angle
if (t_type .eq. 1) then
calc_t_min_angle = 60
else
calc_t_min_angle = min(t_angle, 90.0 - t_angle)
endif
return
end
* Calculate the cosine of the minimum angle
real function calc_t_min_angle_cos(t_type, t_angle)
real calc_t_min_angle
integer t_type
real t_angle, t_min_angle, PI
parameter PI = 3.141592
t_min_angle = calc_t_min_angle(t_type, t_angle)
calc_t_min_angle_cos = cos(t_min_angle * PI / 180.0)
return
end