Day 2 in Fortran 90!

This commit is contained in:
Steph 2022-12-01 21:02:00 +01:00
parent e742759626
commit b55cc9aa20
4 changed files with 95 additions and 0 deletions

3
2021/2/.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
depth
depth2
input

11
2021/2/Makefile Normal file
View File

@ -0,0 +1,11 @@
.PHONY := all build run
all: build run
build:
gfortran -o depth depth.f90
gfortran -o depth2 depth2.f90
run:
./depth
./depth2

40
2021/2/depth.f90 Normal file
View File

@ -0,0 +1,40 @@
program depth
implicit none
integer :: ios, space_index, second_int, nforward = 0, ndepth = 0
integer, parameter :: read_unit = 99
character(len=30) :: line, first_part, second_part
open(unit=read_unit, file='input', iostat=ios)
if ( ios /= 0 ) stop "Error opening file"
do
read(read_unit, "(A)", iostat=ios) line
if (ios /= 0) exit
space_index = scan(line, ' ')
first_part = line(1:space_index)
second_part = line(space_index+1:)
read(second_part, *, iostat=ios) second_int
select case (first_part)
case ("forward")
nforward = nforward + second_int
case ("up")
ndepth = ndepth - second_int
case ("down")
ndepth = ndepth + second_int
end select
end do
print*, "Total forward:", nforward
print*, "Total depth:", ndepth
print*, "Product:", nforward * ndepth
end program depth

41
2021/2/depth2.f90 Normal file
View File

@ -0,0 +1,41 @@
program depth2
implicit none
integer :: ios, space_index, second_int, nforward = 0, ndepth = 0, naim = 0
integer, parameter :: read_unit = 99
character(len=30) :: line, first_part, second_part
open(unit=read_unit, file='input', iostat=ios)
if ( ios /= 0 ) stop "Error opening file"
do
read(read_unit, "(A)", iostat=ios) line
if (ios /= 0) exit
space_index = scan(line, ' ')
first_part = line(1:space_index)
second_part = line(space_index+1:)
read(second_part, *, iostat=ios) second_int
select case (first_part)
case ("forward")
nforward = nforward + second_int
ndepth = ndepth + (naim * second_int)
case ("up")
naim = naim - second_int
case ("down")
naim = naim + second_int
end select
end do
print*, "Total forward:", nforward
print*, "Total depth:", ndepth
print*, "Product:", nforward * ndepth
end program depth2