系统粉 > 电脑问答 > 其他问答

已解决输入出生年月日,和今天的年月日,然后求出出生到今天的总天数(上次没记录)

提问者:爱你无花果群  |  浏览次  |  提问时间:2017-03-02  |  回答数量:2

输入出生年月日,和今天的年月日,然后求出出生到今天的总天数(上次没记录) 版主,对不起哈,又来打扰你了,上次那个程序我确实不小心电脑重启了丢了,现在我把它记录在笔记本上,给我重新整理一下可以吗?输入出生年月日,和今天的年月日,然后求出出生到今天的总天数上次的代码还涉及到一个局部变量赋值的问题,

已有2条答案
小kscsy

小kscsy

回答数:218  |  被采纳数:131

#include "stdio.h"
#include "conio.h"

main()
{
int year1,month1,date1;
int year2,month2,date2;
int i,days=0,day1,day2;
int maxmonth;
int d[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
scanf("%d%d%d\n%d%d%d",&year1,&month1,&date1,&year2,&month2,&date2);
for(i=year1;i<year2;i++)
{
if(i%4==0||i%400==0)
{
days+=366;
}
else
{
days+=365;
}
}
if(month1>month2)
{
maxmonth = month1;
month1 = month2;
month2 = maxmonth;
}
for(i=month1;i<month2;i++)
{

days=days+d;
}
days=days+date2-date1;
if(year2%4==0||year2%400==0&&month2>2)days++;
printf("the days is %d",days);
getch();
}


这是上次版主提示后我自己回忆做的,但是这样肯定是有问题的, 版主请纠正一下吧??(1)
2017-03-02 12:06:07
赞 21
此一时彼一时37

此一时彼一时37

回答数:57  |  被采纳数:122

// 很久没来关注程序版了。。。
#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;
}
2017-03-02 13:20:24
赞 5
相关问答
最新其他问答
解决方法