UT源码 020
NextDate函数问题
NextDate函数说明一种复杂的关系,即输入变量之间逻辑关系的复杂性
NextDate函数包含三个变量month、day和year,函数的输出为输入日期后一天的日期。 要求输入变量month、day和year均为整数值,并且满足下列条件:
条件1 1≤ month ≤12 否则输出,月份超出范围
条件2 1≤ day ≤31 否则输出,日期超出范围
条件3 1912≤ year ≤2050 否则输出:年份超出范围
String nextdate(int m,int d,int y)
注意返回值是字符串。
程序要求:
1)先显示“请输入日期”
2)不满足条件1,返回:“月份超出范围”;不满足条件2,返回:“日期超出范围”;不满足条件3,返回:“年份超出范围”;如果出现多个不满足,以最先出现不满足的错误返回信息。
3)条件均满足,则输出第二天的日期:格式“****年**月**日”(如果输入2050年12月31日,则正常显示2051年1月1日)
源码:
#include "cstring"
#include "cstdio"
#include "iostream"
#include "string.h"
#include "sstream"
using namespace std;
int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int checkVal(int y,int m,int d)//check whether the inputs are legal or not
{
if(m<1||m>12)
return 1;
if(d<1||d>month[m])
return 2;
if(y<1912||y>2050)
return 3;
return 0;
}
bool checkLeapYear(int y)//check if y year is leap year return 1 otherwise return 0
{
if(y%4==0&&y%100!=0)
return 1;
if(y%100==0&&y%400==0)
return 1;
return 0;
}
bool checkLastday(int y,int m,int d)//check whether the date is the last day of a year,if it is return 1 otherwise return 0
{
if(m==12&&d==31)
return 1;
else
return 0;
}
string getString(int y,int m,int d)//generate date in string form
{
stringstream ss;
ss<<y<<"年"<<m<<"月"<<d<<"日";
string temp;
ss>>temp;
return temp;
}
string nextdate(int y,int m,int d)
{
if(checkLeapYear(y)==1)
{
month[2]=29;
}
switch (checkVal(y,m,d))
{
case 1:
return "月份超出范围";
break;
case 2:
return "日期超出范围";
break;
case 3:
return "年份超出范围";
default:
if(d<month[m]&&checkLastday(y,m,d)==0)
{
return getString(y, m, d+1);
}
if(d==month[m])
{
if(checkLastday(y, m, d))
{
return getString(y+1, 1, 1);
}
else
{
return getString(y, m+1, 1);
}
}
return "";
break;
}
}
int main()
{
while(1)
{
cout<<"请输入日期";
int y,m,d;
scanf("%d",&y);
if(y==-1)
break;
else
{
scanf("年%d月%d日",&m,&d);
cout<<nextdate(y, m, d)<<endl;
month[2]=28;
}
}
}
posted on 2017-03-10 19:09 StackOverflow! 阅读(169) 评论(0) 收藏 举报
浙公网安备 33010602011771号