- #1
BrownianMan
- 134
- 0
I'm currently taking my first programming course (1st year student - using FORTRAN 77) and the way the course is taught is atrocious. We listen to the prof give his lecture (3/4 of which is him going off on tangents). We spend no time in a computer lab. The only "practice" we get is 1 lab per week (for which he gives us 80% of the answer) - which isn't really suficient for understanding the material well enough to apply it to different scenarios. Needless to say, a lot of people are struggling.
I have a question, and any help is appreciated. Having trouble with this lab.
Date Validation & Conversion
Given a date like 20/2/2002, we like to convert it to: Wed. Feb 20, 2002. The process involves validating the input to ensure it has the proper format and then extracting from it the day, month, and year as strings. Assuming the extracted fields are indeed numeric, we convert them to integers and then verify that the date exists; e.g. it is not the 31st of September or the 29th of February in a non-leap year. Once properly validated, we employ the Zeller algorithm to find the day of the week (dow) and then output the result looking up the month name and the day name.
Here is how the sought system should behave:
Enter the date ...
test
Invalid format!
Enter the date ...
test/
Invalid format!
Enter the date ...
t/mm/yyyy
Non-numeric values!
Enter the date ...
12/45/98
None existent date!
Enter the date ...
10/2/1983
Thu. Feb 10, 1983
This is what I have so far:
I have to create the following subroutines:
readDate()
prompt for a date, read it as a string, return it to the caller.
checkFormat
verify that the date string has two slashes and extract and return the three substrings delimited by these slashes. Return false if the extraction was not possible. Note that this sub does not care about the content of the three strings, only that they exist; e.g. they can contain non-digits but it still returns true.
existDMY
verify that the passed integers do indeed represent a date that exists (month between 1 and 12 and day between 1 and 28 or 29 or 30 or 31 depending on the month and year). If not, return false.
isLeap
verify that the passed year is a leap year. If not, return false.
printDate
Output the date by translating Zeller's dow code into Sun or Mon, or ... Sat and outputting this day name followed by a dot. The month number is also translated into a month name (Jan Feb Mar ... Dec). The final format is: ddd. mmm dd, yyyy (where ddd is the day name and mmm is the month name).
dow
determine the day of the week and return it as 0 for Sunday, 1 for Monday, ... and 6 for Saturday. This sub uses an algorithm due to Zeller.
I have a question, and any help is appreciated. Having trouble with this lab.
Date Validation & Conversion
Given a date like 20/2/2002, we like to convert it to: Wed. Feb 20, 2002. The process involves validating the input to ensure it has the proper format and then extracting from it the day, month, and year as strings. Assuming the extracted fields are indeed numeric, we convert them to integers and then verify that the date exists; e.g. it is not the 31st of September or the 29th of February in a non-leap year. Once properly validated, we employ the Zeller algorithm to find the day of the week (dow) and then output the result looking up the month name and the day name.
Here is how the sought system should behave:
Enter the date ...
test
Invalid format!
Enter the date ...
test/
Invalid format!
Enter the date ...
t/mm/yyyy
Non-numeric values!
Enter the date ...
12/45/98
None existent date!
Enter the date ...
10/2/1983
Thu. Feb 10, 1983
This is what I have so far:
Code:
program LongDate
implicit none
character*10 date, day, month, year, readDate
integer d, m, y
logical ok
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
call existDMY(d, m, y, ok)
if (.not. ok) then
print*, "Non-numeric date!"
else
call printDate(d, m, y)
end if
end if
end if
if (ok) exit
end do
stop
end
subroutine checkValue(day, month, year, d, m, y, ok)
character*(*) day, month, year
integer d, m, y
logical ok, isDigits
integer 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
I have to create the following subroutines:
readDate()
prompt for a date, read it as a string, return it to the caller.
checkFormat
verify that the date string has two slashes and extract and return the three substrings delimited by these slashes. Return false if the extraction was not possible. Note that this sub does not care about the content of the three strings, only that they exist; e.g. they can contain non-digits but it still returns true.
existDMY
verify that the passed integers do indeed represent a date that exists (month between 1 and 12 and day between 1 and 28 or 29 or 30 or 31 depending on the month and year). If not, return false.
isLeap
verify that the passed year is a leap year. If not, return false.
printDate
Output the date by translating Zeller's dow code into Sun or Mon, or ... Sat and outputting this day name followed by a dot. The month number is also translated into a month name (Jan Feb Mar ... Dec). The final format is: ddd. mmm dd, yyyy (where ddd is the day name and mmm is the month name).
dow
determine the day of the week and return it as 0 for Sunday, 1 for Monday, ... and 6 for Saturday. This sub uses an algorithm due to Zeller.
Last edited: