CSE 1540.03
Week 11.1 March 24, 2014
Lab 9
Implementation of checkValue along with isDigits and s2n_.
Assumption: a number consists of a string of digits with possibly
imbedded and trailing spaces (blanks).
program Lab9
implicit none
character*10 day, month, year
integer d, m, y
logical ok
day = "10"
month = "2"
year = "19 3"
ok = .true.
call checkValue (day, month, year, d, m, y, ok)
print*, "d = ", d
print*, "m = ", m
print*, "y = ", y
print*, "ok = ", ok
end
subroutine checkValue (day, month, year, d, m, y, ok)
character*(*) day, month, year
integer d, m, y
logical ok, isDigits
integer i, j, s2n_
ok = isDigits(day)
if (.not. ok) return
d = s2n_(day)
ok = isDigits(month)
if (.not. ok) return
m = s2n_(month)
ok = isDigits(year)
if (.not. ok) return
y = s2n_(year)
return
end
logical function isDigits (str)
character*(*) str
character*11 digits /" 0123456789"/
integer i
do i = 1, len(str)
if (index(digits,str(i:i)) .eq. 0) then
isDigits = .false.
return
end if
end do
isDigits = .true.
return
end
integer function s2n_ (str)
character*(*) str
character*10 digits /"0123456789"/
integer i, nextDigit
s2n_ = 0
do i = 1, len(str)
nextDigit = index(digits,str(i:i))
if (nextDigit .gt. 0) then
s2n_ = 10*s2n_ + nextDigit - 1
end if
end do
return
end
Implement: checkFormat
checkFormat
- specs say to verify the date string has two slashes
- interpretation: has at least two slashes, with first two encountered
acting as the separators between day, month, year
Revised main program for Lab 9:
program LongDate
implicit none
character*10 date, day, month, year, readDate
integer d, m, y
logical ok, existDMY
do
ok = .true.
date = readDate()
call checkFormat(date, day, month, year, ok)
if (.not. ok) then
print*, "Invalid format!"
else
call checkValue(day, month, year, d, m, y, ok)
if (.not. ok) then
print*, "Non-numeric values!"
else
if (existDMY(d, m, y)) then
call printDate(d, m, y)
else
print*, "Non-existent date!"
ok = .false.
end if
end if
end if
if (ok) exit
end do
stop
end
Some issues:
Does ok need to be initialized each time through the loop?
checkFormat, checkValue are subroutines and existDMY is a function that returns a logical. Why not make them all subroutines or functions?
CSE 1540 Week 11.1 – March 24, 2014 page 1 of 2