【发布时间】:2014-04-27 07:09:56
【问题描述】:
我必须为以 dd/mm/yyyy 格式接收日期的算法编写伪代码并对其进行验证,这将重复,直到用户输入“否”。 验证必须检查:
- 2 月的闰年检查
- 1-12 范围内的月份
- 天在 1-31 的范围内,并根据各自的天数 月
【问题讨论】:
-
你尝试过什么,你在哪里卡住了?此外,这可能不适合数学部分。
标签: logic
我必须为以 dd/mm/yyyy 格式接收日期的算法编写伪代码并对其进行验证,这将重复,直到用户输入“否”。 验证必须检查:
【问题讨论】:
标签: logic
Declare checkDate
LeapYear is boolean
Year is integer
Month is integer
Day is integer
flag is integer
input Date
Year = val(mid(Date, 7, 4))
Month = val(mid(Date, 4, 2))
Day = val(mid(Date, 1, 2))
flag = 0
if Year Mod 4 = 0 then
LeapYear = True
end If
if Month < 13 then
if Month = 1 Or Month = 3 Or Month = 5
Or Month = 7 Or Month = 8 Or Month = 10
Or Month = 12 then
if Day <= 31 then
flag = 0
else
flag = 1
end if
else
if Month = 4 Or Month = 6 Or Month =
9 Or Month = 11 then
if Day <= 30 Then
flag = 0
else
flag = 1
end if
end if
end if
else
flag = 1
end if
if LeapYear = True then
if Month = 2 then
if Day <= 29 then
flag = 0
else
flag = 1
end if
else
if Month = 2 then
if Date <= 28 then
flag = 0
else
flag = 1
end if
end if
end if
end if
if flag = 0 then
Print "date is correct"
else
Print "date is incorrect"
end if'
【讨论】:
这是非常伪代码......让'oops'意味着'返回错误'
input date
day = int(date[0,1])
month = int(date[2,3])
year = int(date[4,7])
if month > 12 or month < 01:
oops
elif month == 1,3,5,7,8,10,12:
if day > 31 or day < 01:
oops
elif month == 4,6,9,11
if day > 30 or day < 01:
oops
elif month == 2
if year % 4 == 0 or (year % 100 != 0 and year % 1000 == 0) #check for leap year
if day > 29 or day < 01:
oops
else
if day > 28 or day < 01:
oops
【讨论】: