...一个表示月份的整数,分别代表日期的年、月、日,计算并在屏幕上...
发布网友
发布时间:2024-03-16 04:25
我来回答
共1个回答
热心网友
时间:2024-10-15 00:07
#include<iostream>
#include<cmath>
using namespace std;
class Date
{
public:
Date(int y=1900,int m=1, int d=1);
bool isleapyear();
bool isendofmounth();
void add_day(int dd);
void print_mdy();
int jiange_day();
private:
void setdate(int yy,int mm,int dd);
void add_oneday();
private:
int year,mounth,day;
};
Date::Date(int y,int m, int d)
{
setdate(y,m,d);
}
void Date::setdate(int yy,int mm,int dd)
{
year=(yy>=1900 && yy<=2100)?yy:1900;
mounth=(mm>=1 && mm<=12)?mm:1;
switch(mounth)
{
case 4:
case 6:
case 8:
case 10: day=(dd>=1 && dd<=30)?dd:1;
case 2:
if(isleapyear()) day=(dd>=1 && dd<=29)?dd:1;
else day=(dd>=1 && dd<=28)?dd:1;
default:
day=(dd>=1&&dd<=31)?dd:1;
}
}
bool Date::isleapyear()
{
if(year%4) return false;
else return true;
}
bool Date::isendofmounth()
{
switch(mounth)
{
case 4:
case 6:
case 8:
case 10: return day==30;
case 2:
if(isleapyear()) return day==29;
else return day==28;
default: return day==31;
}
}
void Date::add_day(int dd) //判断n天后是什么日子
{
for(int i=1;i<=dd;i++)
add_oneday();
}
void Date::add_oneday() //一天后是什么日子
{
if(isendofmounth())
{
if(mounth==12)
{
year=year+1;
mounth=1;
day=1;
}
else
{
mounth=mounth+1;
day=1;
}
}
else
{
day=day+1;
}
}
void Date::print_mdy()
{
char *pmounth[12]={"janary","february","march","april","may",
"june","july","august","september","october","nobember","december"};
cout<<"现日期是:"<<pmounth[mounth-1]<<day<<", "<<year<<endl;
}
int Date::jiange_day() //距1900,1,1日相差天数
{
int days;
days=(year-1900)*365;
days=days+(year-1900)/4;
while(mounth!=1)
{
switch(mounth)
{
case 2:
days=days+31;
case 3:
{
if(isleapyear())
days=days+29;
else days=days+28;
}
case 4:
days=days+31;
case 5: days=days+30;
case 6: days=days+31;
case 7: days=days+30;
case 8: days=days+31;
case 9: days=days+31;
case 10: days=days+30;
case 11: days=days+31;
case 12: days=days+30;
}
mounth--;
}
days=days+(day-1);
return days;
}
int main()
{
using namespace std;
int year,mounth,day;
cout<<"可以构造对象计算任意两个日期间隔天数,判断n天后是什么日子:"<<endl;
cout<<"请输入日期:\n";
cin>>year>>mounth>>day;
Date date1(year,mounth,day);
cout<<"请再输入日期:\n";
int year1,mounth1,day1;
cin>>year1>>mounth1>>day1;
Date date2(year1,mounth1,day1);
cout<<"计算日期1间隔n天后日期,请输入天数:\n";
int day2,day3;
cin>>day2;
date1.add_day(day2);
cout<<"日期1和日期2相差天数为:";
day3=abs(date1.jiange_day()-date2.jiange_day());
cout<<day3<<endl;
return 0;
}
//这个是我以前打过的一个类似的程序,其中你不想要的函数
自己删掉就是了。
热心网友
时间:2024-10-15 00:14
#include<iostream>
#include<cmath>
using namespace std;
class Date
{
public:
Date(int y=1900,int m=1, int d=1);
bool isleapyear();
bool isendofmounth();
void add_day(int dd);
void print_mdy();
int jiange_day();
private:
void setdate(int yy,int mm,int dd);
void add_oneday();
private:
int year,mounth,day;
};
Date::Date(int y,int m, int d)
{
setdate(y,m,d);
}
void Date::setdate(int yy,int mm,int dd)
{
year=(yy>=1900 && yy<=2100)?yy:1900;
mounth=(mm>=1 && mm<=12)?mm:1;
switch(mounth)
{
case 4:
case 6:
case 8:
case 10: day=(dd>=1 && dd<=30)?dd:1;
case 2:
if(isleapyear()) day=(dd>=1 && dd<=29)?dd:1;
else day=(dd>=1 && dd<=28)?dd:1;
default:
day=(dd>=1&&dd<=31)?dd:1;
}
}
bool Date::isleapyear()
{
if(year%4) return false;
else return true;
}
bool Date::isendofmounth()
{
switch(mounth)
{
case 4:
case 6:
case 8:
case 10: return day==30;
case 2:
if(isleapyear()) return day==29;
else return day==28;
default: return day==31;
}
}
void Date::add_day(int dd) //判断n天后是什么日子
{
for(int i=1;i<=dd;i++)
add_oneday();
}
void Date::add_oneday() //一天后是什么日子
{
if(isendofmounth())
{
if(mounth==12)
{
year=year+1;
mounth=1;
day=1;
}
else
{
mounth=mounth+1;
day=1;
}
}
else
{
day=day+1;
}
}
void Date::print_mdy()
{
char *pmounth[12]={"janary","february","march","april","may",
"june","july","august","september","october","nobember","december"};
cout<<"现日期是:"<<pmounth[mounth-1]<<day<<", "<<year<<endl;
}
int Date::jiange_day() //距1900,1,1日相差天数
{
int days;
days=(year-1900)*365;
days=days+(year-1900)/4;
while(mounth!=1)
{
switch(mounth)
{
case 2:
days=days+31;
case 3:
{
if(isleapyear())
days=days+29;
else days=days+28;
}
case 4:
days=days+31;
case 5: days=days+30;
case 6: days=days+31;
case 7: days=days+30;
case 8: days=days+31;
case 9: days=days+31;
case 10: days=days+30;
case 11: days=days+31;
case 12: days=days+30;
}
mounth--;
}
days=days+(day-1);
return days;
}
int main()
{
using namespace std;
int year,mounth,day;
cout<<"可以构造对象计算任意两个日期间隔天数,判断n天后是什么日子:"<<endl;
cout<<"请输入日期:\n";
cin>>year>>mounth>>day;
Date date1(year,mounth,day);
cout<<"请再输入日期:\n";
int year1,mounth1,day1;
cin>>year1>>mounth1>>day1;
Date date2(year1,mounth1,day1);
cout<<"计算日期1间隔n天后日期,请输入天数:\n";
int day2,day3;
cin>>day2;
date1.add_day(day2);
cout<<"日期1和日期2相差天数为:";
day3=abs(date1.jiange_day()-date2.jiange_day());
cout<<day3<<endl;
return 0;
}
//这个是我以前打过的一个类似的程序,其中你不想要的函数
自己删掉就是了。