用C++怎么算任意两个时间段的天数?如1990年1月1日到2008年8月8日期间有几天?
2个回答
展开全部
先做个 tm结构, 然后用 mktime 变成 time_t ,两个 time_t的差就是之间的秒数, 换算天数的话就除 24*3600就好了。代码大致上这样,需要 time.h
struct tm tm1={0}, tm2={0};
tm1.tm_year = 1990;
tm1.tm_mon = 0; // 注意月份是从0开始的
tm1.tm_mday = 1; // 月份日期是从1开始的
tm2.tm_year = 2008;
tm2.tm_mon = 7;
tm2.tm_mday = 8;
int diff = (int)(mktime(tm2)-mktime(tm1));
cout << diff / (24*3600) << endl;
struct tm tm1={0}, tm2={0};
tm1.tm_year = 1990;
tm1.tm_mon = 0; // 注意月份是从0开始的
tm1.tm_mday = 1; // 月份日期是从1开始的
tm2.tm_year = 2008;
tm2.tm_mon = 7;
tm2.tm_mday = 8;
int diff = (int)(mktime(tm2)-mktime(tm1));
cout << diff / (24*3600) << endl;
展开全部
//我以前的一个程序,基本符合你的要求
#include <iostream>
using namespace std;
/*
求任何两人生日相差天数
*/
class date
{
public:
int year;
int month;
int day;
public:
date(int y=1,int m=1,int d=1):year(y),month(m),day(d){}
bool isleapyear ();
int getyeardays ();
int getmonthdays ();
int datetonum();
};
ostream &operator<<(ostream &out,const date &d);
istream &operator>>(istream &in,date &d);
bool date::isleapyear()
{
if(year%4==0 && year%100 !=0 || year%400==0) return true;
else return false;
}
int date::getyeardays()
{
if (isleapyear()) return 366;
else return 365;
}
int date::getmonthdays()
{
int n;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
n=31;
break;
case 4:
case 6:
case 9:
case 11:
n=30;
break;
case 2:
if(isleapyear()) n=29;
else n=28;
}
return n;
}
int date::datetonum()
{
int n=0;
int tempyear=year;
int tempmonth=month;
for (month=1;month<tempmonth;month++)
n+=getmonthdays();
for (year=1;year<tempyear;year++)
n+=getyeardays();
n+=day;
return n;
}
ostream &operator<<(ostream &out,const date &d)
{
out<<d.year<<"-"<<d.month<<"-"<<d.day<<endl;
return out;
}
istream &operator>>(istream &in,date&d)
{
int year,month,day;
cout<<" 输入年份: ";
cin>>year;
cout<<" 输入月份: ";
cin>>month;
cout<<" 输入天 : ";
cin>>day;
d=date(year,month,day);
return in;
}
void main()
{
date d1;
date d2;
int result=0;
cout<<"第一个人:"<<endl;
cin>>d1;
cout<<"二个人:"<<endl;
cin>>d2;
cout<<d1<<d2;
result=d1.datetonum()-d2.datetonum();
if(result<0)
result=-1*result;
cout<<"两人生日相差 "<<result<<" 天。"<<endl;
}
#include <iostream>
using namespace std;
/*
求任何两人生日相差天数
*/
class date
{
public:
int year;
int month;
int day;
public:
date(int y=1,int m=1,int d=1):year(y),month(m),day(d){}
bool isleapyear ();
int getyeardays ();
int getmonthdays ();
int datetonum();
};
ostream &operator<<(ostream &out,const date &d);
istream &operator>>(istream &in,date &d);
bool date::isleapyear()
{
if(year%4==0 && year%100 !=0 || year%400==0) return true;
else return false;
}
int date::getyeardays()
{
if (isleapyear()) return 366;
else return 365;
}
int date::getmonthdays()
{
int n;
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
n=31;
break;
case 4:
case 6:
case 9:
case 11:
n=30;
break;
case 2:
if(isleapyear()) n=29;
else n=28;
}
return n;
}
int date::datetonum()
{
int n=0;
int tempyear=year;
int tempmonth=month;
for (month=1;month<tempmonth;month++)
n+=getmonthdays();
for (year=1;year<tempyear;year++)
n+=getyeardays();
n+=day;
return n;
}
ostream &operator<<(ostream &out,const date &d)
{
out<<d.year<<"-"<<d.month<<"-"<<d.day<<endl;
return out;
}
istream &operator>>(istream &in,date&d)
{
int year,month,day;
cout<<" 输入年份: ";
cin>>year;
cout<<" 输入月份: ";
cin>>month;
cout<<" 输入天 : ";
cin>>day;
d=date(year,month,day);
return in;
}
void main()
{
date d1;
date d2;
int result=0;
cout<<"第一个人:"<<endl;
cin>>d1;
cout<<"二个人:"<<endl;
cin>>d2;
cout<<d1<<d2;
result=d1.datetonum()-d2.datetonum();
if(result<0)
result=-1*result;
cout<<"两人生日相差 "<<result<<" 天。"<<endl;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询