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

来源:互联网 发布:oa软件核心应用价值 编辑:程序博客网 时间:2024/05/16 17:23

(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;  }  


 

原创粉丝点击