ユーマちゃんのブログ

質問・要望はTwitterもしくはコメントで。返信はTwitterで。

Programming Example

program isbn
implicit none
logical judge
character(len=10)::a
integer::i,b,h,s=0,t,j,y,q,l
print*, 'Type in a 10-digit ISBN marked with "?" for the missing digit.'
read*, a
judge = .false. !This judge will be used to check whether including "?" or not.
b= ichar(a(10:10)) !This b will be used to check whether including "X" or not.
!This is ERROR message when user input wrong number.
if (len_trim(a)/=10 ) then
print*, "Program is terminated deu to incorrect input"
stop
end if

!This "do" checks whether input includes "?" or not.
do i =1,10
if (ichar(a(i:i))==63 ) then ! "?" is 63 in command
judge = .true.
h = i !This shows there is "?" at "h"th place.
end if
end do

if (judge) then
if ( b==120 .or. b==88 ) then !x=120 X=88
!This is when input include "?" and "X".
do i = 1,h-1
read(a(i:i),*) t
s = s + t*(11-i)
end do
do j = h+1,9
read(a(j:j),*) t
s = s + t*(11-j)
end do
s = s + 10
else
!This is when input include "?" but "X".
do i = 1,h-1
read(a(i:i),*) t
s = s + t*(11-i)
end do
do j = h+1,10
read(a(j:j),*) t
s = s + t*(11-j)
end do
end if
!This "do" finds what is "?" from 0 to 10.
do q =0,10
s = s + q*(11-h)
if ( mod(s,11)==0 ) then
l = q
exit
end if
s = s - q*(11-h)
end do
!This is final solution when input includes "?"
if (l==10) then
print*,"The missing digit is X"
else
print*,"The missing digit is",l
end if
stop
end if
!Situation above is When the setences include "?"
!Below one is When the input doesn't include "?"
if ( b==120 .or. b==88 ) then !Checking whether input includes x or not.
do i = 1,9
read(a(i:i),*) t
s = s + t*(11-i)
end do
s = s + 10
else
do i = 1,10
read(a(i:i),*) t
s = s + t*(11-i)
end do
end if
!Final answer
if ( mod(s,11)==0 ) then
print*, "This ISBN is valid"
else
print*, "This ISBN is invalid"
end if
end program isbn