係統粉 > 電腦問答 > 其他問答

已解決輸入出生年月日,和今天的年月日,然後求出出生到今天的總天數(上次沒記錄)

提問者:愛你無花果群  |  瀏覽次  |  提問時間: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
相關問答
最新其他問答
解決方法