* 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.
10 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 ' //
. 'isn''t set.'
print *
goto 10
endif
* Perform the chosen action
if (chosen_action .eq. 1) then
call set_t_data()
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 10
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
20 print *, 'Choose an action (enter action number):'
print *, '1) Set 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 20
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.'
print *
validate_chosen_action = .false.
endif
return
end
* Set triangle data
subroutine set_t_data()
integer t_type
real t_side, t_angle
logical validate_t_type, validate_t_side, validate_t_angle
common /triangle/ t_type, t_side, t_angle
30 print *, '------- Start triangle configuration -------'
print *, 'Choose the type of triangle (enter type number):'
print *, '1) Equilateral triangle;'
print *, '2) Right triangle.'
read *, t_type
if (.not.(validate_t_type(t_type))) then
goto 30
endif
if (t_type .eq. 1) then
t_angle = 60
elseif (t_type .eq. 2) then
31 print *, 'Enter the opposite angle (in degrees):'
read *, t_angle
if (.not.(validate_t_angle(t_type, t_angle))) then
goto 31
endif
endif
32 if (t_type .eq. 1) then
print *, 'Enter the length of the leg (in m):'
else
print *, 'Enter the length of the side (in m):'
endif
read *, t_side
if (.not.(validate_t_side(t_side))) then
goto 32
endif
print *, '------- End of triangle configuration -------'
return
end
* Validate chosen triangle type. Show error if type is undefined.
logical function validate_t_type(t_type)
integer t_type
validate_t_type = .true.
if (t_type .lt. 1 .or. t_type .gt. 2) then
print *
print *, '!!! Chosen triangle type is undefined.'
print *
validate_t_type = .false.
endif
return
end
* Validate triangle angle value. Show error if angle isn't correct.
logical function validate_t_angle(t_type, t_angle)
integer t_type
real t_angle
validate_t_angle = .true.
if (t_angle .le. 0) then
print *
print *, '!!! Angle can''t be less than or equals to 0.'
print *
validate_t_angle = .false.
endif
if (t_type .eq. 2 .and. t_angle .ge. 90) then
print *
print *, '!!! Angle can''t be greater than 90.'
print *
validate_t_angle = .false.
endif
return
end
* Validate triangle side value. Show error if side isn't correct.
logical function validate_t_side(t_side)
real t_side
validate_t_side = .true.
if (t_side .le. 0) then
print *
print *, '!!! Side can''t be less than or equals to 0.'
print *
validate_t_side = .false.
endif
return
end
real function deg_to_rad(degrees)
real degrees, PI
parameter (PI = 3.14)
deg_to_rad = degrees * PI / 180
return
end
* Calculate the area of a triangle
real function calc_t_area(t_type, t_side, t_angle)
real deg_to_rad
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 = t_side ** 2 / (2 * tan(deg_to_rad(t_angle)))
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, deg_to_rad
integer t_type
real t_angle, t_min_angle
t_min_angle = calc_t_min_angle(t_type, t_angle)
calc_t_min_angle_cos = cos(deg_to_rad(t_min_angle))
return
end