C++ 计算两个日期之间的天数

如果一个月只有30天,输入第一个日期和第二个日期(日,月,年)后,计算出这两个日期之间的天数。如何用C++写出以上的程序,求助回tattackor,这个是假设,这个程序是... 如果一个月只有30天,输入第一个日期和第二个日期(日,月,年)后,计算出这两个日期之间的天数。
如何用C++写出以上的程序,求助
回 tattackor ,这个是假设,这个程序是必须根据一个月30天来写,请看清楚问题
展开
 我来答
sunzhenwei114
推荐于2018-02-27 · 知道合伙人教育行家
sunzhenwei114
知道合伙人教育行家
采纳数:776 获赞数:6173
毕业于阜新矿业学院基础部数学师范专业,擅长初高中数学教学,熟练操作excel,信息技术与数学整合是特长。

向TA提问 私信TA
展开全部
//历法规定,四年一闰,四百年闰,例如2000年是闰年,2100年不闰年,
//公历年份是整百数的,必须是400的倍数的才是闰年,不是400的倍数的就是平年
//计算两个日期之间的天数(C++)
//定义变量year1, month1, day1, year2, month2, day2
//取出2个日期中的年 月 日
//如果年相同,月也相同
//return day1- day2
/*
问题: 两个日期(如"2010-04-13"和"1988-10-24"),求它们之间相差的天数
分析: 这是一年看似简单,实际上却有点复杂的小问题,首先你要考虑到月大月小的问题,其次你要考虑到闰年的问题
当然,你也要检测一下,给定的日期格式是否合法,对这个问题的解法,写出伪代码如下
定义变量year1, month1, day1, year2, month2, day2
取出2个日期中的 年 月 日
如果年相同,月也相同:
Return | day1 - day2
如果年相同,月不同:
D1 = date1是该年的第几天
D2 = date2是该年的第几天
Return | d1 - d2
如果年份不同:
D1 = 年份小的日期,离年低还有多少天
D2 = 年份大的日期是这年的第几天
D3 = 两个日期之间相差多少个整年,共有多少天
Return D1 + D2 + D3
上面的伪代码用户C++实现如下:
*/

#include <iostream>
using namespace std;
//IsLeap函数判断一个年份是否为闰年,方法如下:
bool IsLeap(int year)
{
return (year % 4 ==0 || year % 400 ==0) && (year % 100 !=0);
}

//上面的StringToDate函数用于取出日期中的年月日并判断日期是否合法
//从字符中最得年月日 规定日期的格式是yyyy-mm-dd
bool StringToDate(string date, int& year, int& month, int& day)
{
year = atoi((date.substr(0,4)).c_str());
month = atoi((date.substr(5,2)).c_str());
day = atoi((date.substr(8,2)).c_str());
int DAY[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(IsLeap(year)){
DAY[1] = 29;
}
return year >= 0 && month<=12 && month>0 && day<=DAY[month] && day>0;
}

//DayInYear能根据给定的日期,求出它在该年的第几天,代码如下
int DayInYear(int year, int month, int day)
{
//int _day = 0;
int DAY[12]={31,28,31,30,31,30,31,31,30,31,30,31};
if(IsLeap(year))
DAY[1] = 29;
for(int i=0; i<month - 1; ++i)
{
day += DAY[i];
}
return day;
}

int DaysBetween2Date(string date1, string date2)
{
//取出日期中的年月日
int year1, month1, day1;
int year2, month2, day2;
if(!StringToDate(date1, year1, month1, day1) || !StringToDate(date2, year2,month2,day2))
{
cout<<"输入的日期格式不正确";
return -1;
}
if(year1 == year2 && month1 == month2)
{
return day1 > day2 ? day1 - day2 : day2 - day1;

//如果年相同
}else if(year1 == year2)
{
int d1, d2;
d1 = DayInYear(year1, month1, day1);
d2 = DayInYear(year2, month2, day2);
return d1 > d2 ? d1 - d2 : d2 - d1;

//年月都不相同
}else{
//确保year1年份比year2早
if(year1 > year2)
{
//swap进行两个值的交换
swap(year1, year2);
swap(month1, month2);
swap(day1, day2);
}
int d1,d2,d3;
if(IsLeap(year1))
d1 = 366 - DayInYear(year1,month1, day1); //取得这个日期在该年还于下多少天
else
d1 = 365 - DayInYear(year1,month1, day1);
d2 = DayInYear(year2,month2,day2); //取得在当年中的第几天
cout<<"d1:"<<d1<<", d2:"<<d2;

d3 = 0;
for(int year = year1 + 1; year < year2; year++)
{
if(IsLeap(year))
d3 += 366;
else
d3 += 365;
}
return d1 + d2 + d3;
}
}
int main()
{
int a = DaysBetween2Date("2010-11-11","2011-11-11");
cout<<"2010-11-11到2011-11-11相差 "<<a<<" 天";
system("pause");
return 0;
}
C++是在C语言的基础上开发的一种面向对象编程语言,应用广泛。C++支持多种编程范式 --面向对象编程、泛型编程和过程化编程。最新正式标准C++于2014年8月18日公布。其编程领域众广,常用于系统开发,引擎开发等应用领域,是至今为止最受广大程序员受用的最强大编程语言之一,支持类:类、封装、重载等特性!
魔尊8
2010-05-11 · TA获得超过1125个赞
知道小有建树答主
回答量:535
采纳率:0%
帮助的人:564万
展开全部
#include<iostream.h>
class date
{
public:
date(){}
void set(int p1,int p2,int p3)
{
year=p1;month=p2;day=p3;
}
int year,month,day;
};

int a[]={31,28,31,30,31,30,31,31,30,31,30,31},*p1,*p2,*p3,i=1,k=0;

int main()
{
int days(date k);
date m,n;
int p1,p2,p3;
cout<<"请依次输入年·月·日:"<<endl;
cin>>p1>>p2>>p3;
if(p1%4==0&&p1%100!=0||p1%400==0)
a[1]=29;
if(p2>12||p3>a[p2-1])
cout<<"您的输入有误!"<<endl;
else m.set(p1,p2,p3);
cout<<"这天是该年的第"<<days(m)<<"天。"<<endl;

cout<<"请依次输入年·月·日:"<<endl;
cin>>p1>>p2>>p3;
if(p1%4==0&&p1%100!=0||p1%400==0)
a[1]=29;
if(p2>12||p3>a[p2-1])
cout<<"您的输入有误!"<<endl;
else n.set(p1,p2,p3);
cout<<"这天是该年的第"<<days(n)<<"天。"<<endl;
cout<<"这之间相隔"<<days(n)-days(m)<<"天."<<endl;
return 0;
}

int days(date k)
{int i,c=0;
for(i=0;i<k.month-1;i++)
c+=a[i];
c+=k.day;
return c;
}
不知道这样可以不?

修改:#include<iostream.h>
class date
{
public:
void setdate()
{
int c=1;
while(c)
{
cout<<"请输入年月日:\n";
cin>>year>>month>>day;
if(year>0&&month>0&&month<13&&day>0&&day<31)
c=0;
else cout<<"你的输入有误!\n";
}
}
friend int numday(date t1,date t2)
{
int c;
c=(t1.year-t2.year)*360+(t1.month-t2.month)*30+t1.day-t2.day;
if(c<0)return (-c);
else return c;
}
private:
int year,month,day;

};
void main()
{
date t1,t2;
t1.setdate();
t2.setdate();
cout<<numday(t1,t2)<<endl;
}
这下子应该可以了,不过按照一般来说呢,这是不行的,不符合实际,当然你要这样也没有办法,呵呵。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
jcy3610
2010-05-10 · TA获得超过137个赞
知道答主
回答量:82
采纳率:0%
帮助的人:104万
展开全部
我这个是用C语言写的,应该算是更符合你的要求了吧,C++兼容C嘛。
也是按实际每个月的天数算的,希望能帮到你。

#include <stdio.h>
unsigned findday(unsigned y,unsigned m,unsigned d)
{
int leap,day;
if(y%4==0) leap=1;
else leap=0;
day=d;
switch(m-1)
{
case 11:day+=30;
case 10:day+=31;
case 9:day+=30;
case 8:day+=31;
case 7:day+=31;
case 6:day+=30;
case 5:day+=31;
case 4:day+=30;
case 3:day+=31;
case 2:
if(leap==1) day+=29;
else day+=28;
case 1:day+=31;
}
return day;
}

void main()
{
unsigned y1,m1,d1,y2,m2,d2,i,p0,p1,c=0,p2,s;
printf("日期1(格式:年.月.日):");
scanf("%u.%u.%u",&y1,&m1,&d1);
printf("日期2(格式:年.月.日):");
scanf("%u.%u.%u",&y2,&m2,&d2);
if(y1==y2) s=findday(y2,m2,d2)-findday(y1,m1,d1);
else
{
if(y1%4==0)
p0=366-findday(y1,m1,d1);
else
p0=365-findday(y1,m1,d1);

for(i=y1+1;i<y2;i++)
if(i%4==0)
c+=1;
p1=(y2-y1-1)*365+c;

p2=findday(y2,m2,d2);
s=p0+p1+p2;
}
printf("两日期相差%u天\n",s);
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
tattackor
推荐于2017-11-23 · TA获得超过3.5万个赞
知道大有可为答主
回答量:5083
采纳率:94%
帮助的人:852万
展开全部
“如果一个月只有30天”?

你这是在拍戏呢还是写小说呢?

我们常用的公历有个问题, 有闰年之分

因此不同年份之间并不是均匀的

需要考虑这个进去

c++里面应该有日期的相关类和函数

请你自己再查查资料。

2019.5.10.23:31

Lz不高兴了

好, 我就来写些看, 希望能够如您的意

每个月都30天, 那肯定就没润平之分了

ok,这样就比较好写了。

大概思路是:先把年月日转化成以日为单位的数

再相减取绝对值, 注意, 这里不区分AD/BC

我写得比较简单, 不明白的再联系我

#include <iostream>
using namespace std;

int getDays(int day,int month,int year){
return day+(month-1)*30+year*360;
}

void main(){
int day1,mon1,yea1;
cin>>day1>>mon1>>yea1;
int day2,mon2,yea2;
cin>>day2>>mon2>>yea2;

cout<<abs(getDays(day1,mon1,yea1)-getDays(day2,mon2,yea2));

}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(2)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式