根据公历历法,设计NextDate(mm-dd-yyyy):mm-dd-yyyy。根据上面的要求完成一下几个题目,第一题:写一段伪代码来表达程序流程;第二题:根据第1题的程序流程设计结构测试测试用;第三题:根据现代历法规则设计等价类,并设计等价类测试用
时间: 2024-02-19 11:58:37 浏览: 70
第一题:伪代码如下:
```
function NextDate(month, day, year):
if year < 1812 or year > 2012 or month < 1 or month > 12 or day < 1 or day > 31:
return "Invalid input"
else if month == 2:
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
if day < 1 or day > 29:
return "Invalid input"
else if day == 29:
return "03-01-" + year
else:
return "02-" + str(day + 1) + "-" + year
else:
if day < 1 or day > 28:
return "Invalid input"
else if day == 28:
return "03-01-" + year
else:
return "02-" + str(day + 1) + "-" + year
else if month == 4 or month == 6 or month == 9 or month == 11:
if day < 1 or day > 30:
return "Invalid input"
else if day == 30:
return str(month + 1) + "-01-" + year
else:
return str(month) + "-" + str(day + 1) + "-" + year
else:
if day < 1 or day > 31:
return "Invalid input"
else if day == 31:
if month == 12:
return "01-01-" + str(year + 1)
else:
return str(month + 1) + "-01-" + year
else:
return str(month) + "-" + str(day + 1) + "-" + year
```
第二题:根据伪代码设计结构测试用例如下:
```
assert NextDate(2, 28, 2020) == "02-29-2020"
assert NextDate(2, 29, 2020) == "03-01-2020"
assert NextDate(2, 29, 2021) == "Invalid input"
assert NextDate(4, 30, 2020) == "05-01-2020"
assert NextDate(6, 30, 2020) == "07-01-2020"
assert NextDate(9, 30, 2020) == "10-01-2020"
assert NextDate(11, 30, 2020) == "12-01-2020"
assert NextDate(12, 31, 2020) == "01-01-2021"
assert NextDate(13, 1, 2020) == "Invalid input"
assert NextDate(1, 0, 2020) == "Invalid input"
assert NextDate(1, 32, 2020) == "Invalid input"
assert NextDate(4, 31, 2020) == "Invalid input"
```
第三题:根据现代历法规则设计等价类测试用例如下:
- 有效日期:年份在1812-2012之间,月份在1-12之间,日期在1-31之间
- 非法日期:年份不在1812-2012之间,月份不在1-12之间,日期不在1-31之间
- 2月份闰年:年份为闰年,日期在1-29之间
- 2月份平年:年份为平年,日期在1-28之间
- 4、6、9、11月份:日期在1-30之间
- 1、3、5、7、8、10、12月份:日期在1-31之间
根据以上等价类设计测试用例如下:
```
# 有效日期测试
assert NextDate(2, 28, 2020) == "02-29-2020"
assert NextDate(4, 30, 2020) == "05-01-2020"
assert NextDate(12, 31, 2020) == "01-01-2021"
# 非法日期测试
assert NextDate(0, 1, 2020) == "Invalid input"
assert NextDate(1, 32, 2020) == "Invalid input"
assert NextDate(13, 1, 2020) == "Invalid input"
assert NextDate(2, 29, 2021) == "Invalid input"
assert NextDate(2, 30, 2020) == "Invalid input"
# 2月份闰年测试
assert NextDate(2, 29, 2020) == "03-01-2020"
assert NextDate(2, 1, 2020) == "02-02-2020"
assert NextDate(2, 28, 2020) == "02-29-2020"
# 2月份平年测试
assert NextDate(2, 1, 2021) == "02-02-2021"
assert NextDate(2, 28, 2021) == "03-01-2021"
assert NextDate(2, 29, 2021) == "Invalid input"
# 4、6、9、11月份测试
assert NextDate(4, 30, 2020) == "05-01-2020"
assert NextDate(6, 30, 2020) == "07-01-2020"
assert NextDate(9, 30, 2020) == "10-01-2020"
assert NextDate(11, 30, 2020) == "12-01-2020"
# 1、3、5、7、8、10、12月份测试
assert NextDate(1, 31, 2020) == "02-01-2020"
assert NextDate(3, 31, 2020) == "04-01-2020"
assert NextDate(5, 31, 2020) == "06-01-2020"
assert NextDate(7, 31, 2020) == "08-01-2020"
assert NextDate(8, 31, 2020) == "09-01-2020"
assert NextDate(10, 31, 2020) == "11-01-2020"
assert NextDate(12, 31, 2020) == "01-01-2021"
```
阅读全文