美文网首页
日期天数之间计算

日期天数之间计算

作者: LuckTime | 来源:发表于2016-06-20 16:13 被阅读65次

不能修改月份的天数,是遗憾。继续修改
。。。。。。。。。。。。。。。。。

include<iostream>

include<time.h>

using namespace std;

int loopyear(int year);
int longtime1(int year,int month,int day);
int longtime2(int year,int month,int day);
int alltotalday(int year[]);
int main()
{
int year[2], month[2], day[2];
int yearday;
int totalday =0;
int i,t1, t2;
cout << "year, month, day " << endl;
for( i = 0; i<2; i++)
{
cout << "输入第" << i+1 << "个" << endl;
cout << "输入年份()" << endl;
cin >> year[i] ;
cout << "输入月份(1-12)" << endl;
cin >> month[i];
cout << "输入日(1-31),请正常情况输入" << endl;
cin >> day[i];
}
if(loopyear(year[0]) == 365)
t1 = longtime1(year[0],month[0],day[0]);
else
t1 = longtime2(year[0],month[0],day[0]); //计算日期距这一年的时间(需要判断是否为闰年)

if(loopyear(year[1]) == 365)
t2 = longtime1(year[1],month[1],day[1]);
else
t2 = longtime2(year[1],month[1],day[1]); //计算第二个输入日期距这一年的时间(需要判断是否为闰年)

// for (; year[0] <= year[1]; year[0]=+1)
// totalday = loopyear(year[0]) + totalday; // 两年相距的时间

yearday = loopyear(year[1]);
totalday =alltotalday(year);
totalday = totalday - t1 - (yearday-t2) ; //两年相距的时间 - 多算的时间 =两日期之间的天数

cout <<"total =" << totalday << endl;
return 0;
}

int loopyear(int year) //判断是否是闰年
{
if(year % 4 == 0 && year %100 !=0 || year % 400 == 0) //判断闰年 !!!!!!!!!!!!!!!!!!!!!!!!!
return 366;
else
return 365;
}

int longtime1(int year,int month,int day) //平年这一年过的时间
{
switch(month)
{
case 1: return day; //break;
case 2: return 31+day; //break;
case 3: return 31+28+day;//break;
case 4: return 31+28+31+day;//break;
case 5: return 31+28+31+30+day;//break;
case 6: return 31+28+31+30+31+day;//break;
case 7: return 31+28+31+30+31+30+day;//break;
case 8: return 31+28+31+30+31+30+31+day;//break;
case 9: return 31+28+31+30+31+30+31+31+day;//break;
case 10: return 31+28+31+30+31+30+31+31+30+day;//break;
case 11: return 31+28+31+30+31+30+31+31+30+31+day;//break;
case 12: return 31+28+31+30+31+30+31+31+30+31+30+day;//break;
}
// return 0 ;
}

int longtime2(int year,int month,int day) //这一年过的时间
{
switch(month)
{
case 1: return day; //break;
case 2: return 31+day; //break;
case 3: return 31+29+day;//break;
case 4: return 31+29+31+day;//break;
case 5: return 31+29+31+30+day;//break;
case 6: return 31+29+31+30+31+day;//break;
case 7: return 31+29+31+30+31+30+day;//break;
case 8: return 31+29+31+30+31+30+31+day;//break;
case 9: return 31+29+31+30+31+30+31+31+day;//break;
case 10: return 31+29+31+30+31+30+31+31+30+day;//break;
case 11: return 31+29+31+30+31+30+31+31+30+31+day;//break;
case 12: return 31+29+31+30+31+30+31+31+30+31+30+day;//break;
}
//return 0;
}

int alltotalday(int year[]) //两年之间的总天数
{
int a,b,sum=0;
a = year[0];
b = year[1];
while(b>=a)
{
sum = sum + loopyear(a);
++a;
}
return sum;
}

相关文章

网友评论

      本文标题:日期天数之间计算

      本文链接:https://www.haomeiwen.com/subject/ewovdttx.html