您的位置:首页 > 其它

判断某年某月是这一年的第几天(多种方法)

2013-11-14 16:18 330 查看
(1) 判断某年某月是这一年的第几天------方法1-----非函数调用方式

 

/*(1)判断某年月日是这一年的第几天----非函数调用形式

判断某年某月某日这一年中是第几天。

程序分析:以2011年3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊
      情况,闰年且输入月份大于3时需考虑多加一天。
程序实现:
在主程序中输入输入某年某月某日,例如2012 9 18(年月日之间用空格隔开), 输出某年某月某日这一年中是第几天。
注意 在判断前要保证输入的日期是合法的。
*/

#include <iostream>
using namespace std;
int main()
{
int year,month,day; //year表示年,month存月,day存天
int sum; //存放该天是本年中的第几天
int leap;  //是否是闰年的标志

cout<<"please input year,month,day(for example input 2012 9 18):";
cin>>year>>month>>day;  //输入要计算的年月日

//以下循环是对输入日期(年、月、日)的合法性判断,如果输入的日期有误,需要重新输入
if(year%400==0||(year%4==0&&year%100!=0))//判断是不是闰年
leap=1;  //闰年
else
leap=0; //非闰年

while((year<=0||month>12||month<=0||day<=0||day>31)||
(month==4||month==6||month==9||month==11)&&(day==31)||
(leap==1&&month==2&&day>29)||(leap==0&&month==2&&day>28))
{cout<<"input date error!"<<endl;
cout<<"please input year,month,day(for example input 2012 9 18):";
cin>>year>>month>>day;
if(year%400==0||(year%4==0&&year%100!=0))//判断是不是闰年
leap=1;  //闰年
else
leap=0; //非闰年
}

switch(month)//先计算某月以前月份的总天数
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
}
sum=sum+day; //再加上某天的天数

if(leap==1&&month>2)//如果是闰年且月份大于2,总天数应该加一天
sum++;
cout<<"It is the "<<sum<<"th day."<<endl;
return 0;
}

(2)方法2----通过调用一个函数

/*判断某年某月某日这一年中是第几天---函数调用形式(调用1个函数)
编写函数,判断某年某月某日这一年中是第几天。

程序分析:以2011年3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊
      情况,闰年且输入月份大于3时需考虑多加一天。
程序实现:
编写dayInYear函数,有三个参数,分别表示要判断的年、月、日,函数返回值为该参数日期在这一年中的第几天
在主程序中输入输入某年某月某日,例如2012 9 18(年月日之间用空格隔开),调用dayInYear函数得到函数值
注意 在判断前要保证输入的日期是合法的。
*/

#include <iostream>
using namespace std;
int dayInYear(int year,int month,int day); //判断某年某月某日这一年中是第几天的函数
int main()
{
int year,month,day; //分别存放年、月、日
int leap;  //是否是闰年的标志
cout<<"please input year,month,day(for example input 2012 9 18):";
cin>>year>>month>>day;  //输入年 月 日

if(year%400==0||(year%4==0&&year%100!=0))//判断是不是闰年
leap=1;  //闰年
else
leap=0; //非闰年

//以下循环是对输入日期(年、月、日)的合法性判断,如果输入的日期有误,需要重新输入
while((year<=0||month>12||month<=0||day<=0||day>31)||
(month==4||month==6||month==9||month==11)&&(day==31)||
(leap==1&&month==2&&day>29)||(leap==0&&month==2&&day>28))
{cout<<"input date error!"<<endl;
cout<<"please input year,month,day(for example input 2012 9 18):";
cin>>year>>month>>day;
if(year%400==0||(year%4==0&&year%100!=0))//判断是不是闰年
leap=1;  //闰年
else
leap=0; //非闰年
}

cout<<"It is the "<<dayInYear(year,month,day)<<"th day."<<endl;  //调用函数获取第几天
return 0;
}

int dayInYear(int year,int month,int day)//函数定义,返回本年中的第几天
{
int sum;
int leap;
if(year%400==0||(year%4==0&&year%100!=0))//判断是不是闰年
leap=1;  //闰年
else
leap=0; //非闰年

switch(month)//先计算某月以前月份的总天数
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
}
sum=sum+day; //再加上某天的天数

if(leap==1&&month>2)//如果是闰年且月份大于2,总天数应该加一天
sum++;

return sum;
}


(3)调用多个函数

/* 判断某年某月某日这一年中是第几天----函数调用形式(嵌套函数调用)

编写函数,判断某年某月某日这一年中是第几天。

程序分析:以2011年3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊
      情况,闰年且输入月份大于3时需考虑多加一天。
程序实现:
编写dayInYear函数,有三个参数,分别表示要判断的年、月、日,函数返回值为该参数日期在这一年中的第几天
在主程序中输入输入某年某月某日,例如2012 9 18(年月日之间用空格隔开),调用dayInYear函数得到函数值
注意 在判断前要保证输入的日期是合法的。
编写函数isValid函数,判断年月日是否有效,有效返回true,无效返回false
编写函数isLeap函数,判断是否是闰年,是返回true,不是返回false
*/

#include <iostream>
using namespace std;
bool isLeap(int year);  //判断是否是闰年的函数
bool isValid(int year, int month, int day);  //判断输入的年月日是否合法
int dayInYear(int year,int month,int day);  //获取当前的年月日组成的日期是本年中的第几天
int main()
{
int year,month,day;  //分别存放年、月、日
cout<<"please input year,month,day(for example input 2012 9 18):";
cin>>year>>month>>day;  //输入年月日

//以下循环是对输入日期(年、月、日)的合法性判断,如果输入的日期有误,需要重新输入
while(!isValid(year,month,day))  //通过函数调用判断年月日是否合法
{
cout<<"input date error!"<<endl;
cout<<"please input year,month,day(for example input 2012 9 18):";
cin>>year>>month>>day;
}

cout<<"It is the "<<dayInYear(year,month,day)<<"th day."<<endl;  //调用函数输出是第几天
return 0;
}

bool isLeap(int year) //判断是否是闰年
{
if(year%400==0||(year%4==0&&year%100!=0))//判断是不是闰年
return true;  //闰年
else
return false; //非闰年
}

bool  isValid(int year, int month, int day) //判断输入年月日是否有效
{
bool leap;
leap=isLeap(year);
if ((year<=0||month>12||month<=0||day<=0||day>31)||
(month==4||month==6||month==9||month==11)&&(day==31)||
(leap==1&&month==2&&day>29)||(leap==0&&month==2&&day>28))
return false;
return true;
}

int dayInYear(int year,int month,int day)//函数定义,返回本年中的第几天
{
int sum;
int leap=isLeap(year);

switch(month)//先计算某月以前月份的总天数
{
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
}
sum=sum+day; //再加上某天的天数

if(leap==1&&month>2)//如果是闰年且月份大于2,总天数应该加一天
sum++;

return sum;
}


 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: