方法一:
- int DayNum(int year,int mounth, int day) //定义一个整型函数,并带有三个整型形参
- {
- int result=0;
- int mounth_day[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};//用数组给每个月天数赋值
- if(year%400 == 0 || (year%100 !=0 && year%4 == 0)) //判断是否是闰年
- { //是闰年计算如下
- if (mounth > 2) {
- for (int i = 1;i < mounth ; i++) {
- result +=mounth_day[i];
- }
- //闰年要多家一天
- result+=1+day;
- }
- else
- {
- for (int i = 1;i < mounth ; i++) {
- result +=mounth_day[i]+day;
- }
- }
-
- }
- else //不是闰年
- {
- for (int i = 1;i < mounth ; i++) {
- result +=mounth_day[i]; //正常计算天数,把前几个月加起来+天数
- }
- result +=+day;
- }
- return result; //返回总天数
- }
复制代码
方法二:
- int DayNum1(int year,int mounth, int day)
- {
- int sum=0;
- //用数组存储天数
- int summonth[13]={0,31,59,90,120,151,181,212,243,273,304,334};
- if(year%400 == 0 || (year%100 !=0 && year%4 == 0))
- {
- if (mounth > 2) {
- sum=summonth[mounth-1]+day+1;
- }
- else sum=summonth[mounth-1]+day;
- }
- else sum=summonth[mounth-1]+day;
- return sum;
- }
复制代码 |
|