C++编程:输入今天的日期,输出昨天和明天的日期~~?

2024-12-05 02:12:47
推荐回答(4个)
回答1:

文件 “Data.h”

#ifndef DATA_H

#define DATA_H

#pragma once

#include 

using std::cout;

using std::cin;

using std::istream;

using std::ostream;

class CData

{

public:

int m_iYear;

int m_iMonth;

int m_iDay;

private:

bool IsLeapYear(const int iYear);

int  GetMonthsType(const int iYear,const int iMonth);

public:

CData(void){};

CData(int iYear,int iMonth,int iDay);

CData& operator=(const CData& data);

CData operator+(const int i) const;

CData operator-(const int i) const;

friend ostream& operator<<(ostream &out,const CData &data);

friend istream& operator>>(istream &in, CData &data);

};

#endif

----------------------------------------------------

文件“Data.cpp”

#include "Data.h"

#define AD_BIG 31

#define AD_SMALL 30

#define AD_FEBRUATY_YES 29

#define AD_FEBRUATY_NO 28

CData::CData(int iYear,int iMonth,int iDay)

{

while(iMonth > 12)

{

iMonth -= 12;

iYear++;

}

while(iMonth < 1)

{

iYear--;

iMonth += 12;

}

while(iDay > 28)

{

iDay-=GetMonthsType(iYear,iMonth);

iMonth++;

if (iMonth > 12)

{

iMonth = 1;

iYear++;

}

}

while(iDay < 1)

{

if(iMonth <= 1)

{

iYear--;

iMonth += 12;

}

iMonth--;

iDay += GetMonthsType(iYear,iMonth);

}

m_iYear = iYear;

m_iMonth = iMonth;

m_iDay = iDay;

}

bool CData::IsLeapYear(const int iYear)

{

if((iYear%4==0&&iYear%100!=0)||(iYear%400==0))

return true;

else

return false;

}

int CData::GetMonthsType(const int iYear,const int iMonth)

{

int iRet = AD_BIG;

switch(iMonth)

{

case 2:

IsLeapYear(iYear)?iRet=AD_FEBRUATY_YES:iRet=AD_FEBRUATY_NO;

break;

case 4:

case 6:

case 9:

case 11:

iRet = AD_SMALL;

break;

}

return iRet;

}

CData& CData::operator=(const CData& data)

{

this->m_iYear = data.m_iYear;

this->m_iMonth = data.m_iMonth;

this->m_iDay = data.m_iDay;

return *this;

}

CData CData::operator+(const int i) const

{

 return CData(m_iYear,m_iMonth,m_iDay+i);

}

CData CData::operator-(const int i) const

{

return CData(m_iYear,m_iMonth,m_iDay-i);

}

ostream& operator<<(ostream &out,const CData &data)

{

out << data.m_iYear << "年" << data.m_iMonth << "月" << data.m_iDay << "日";

return out;

}

istream& operator>>(istream &in, CData &data)

{

cout << "请输入年份:";

in >>data.m_iYear;

cout << "请输入月份:";

in >>data.m_iMonth;

cout << "请输入日:";

in >>data.m_iDay;

data = CData(data.m_iYear,data.m_iMonth,data.m_iDay);

return in;

}

--------------------------------------------------------------------

文件“main.cpp”

#include 

#include "Data.h"

int main()

{

CData data;

cin >> data;

        cout << "\n输入日期:" << data <

cout << "\t昨天:" << data -1 <

cout << "\t明天:" << data +1<

return 0;

}

回答2:

#include
using namespace std;
int day_tab[2][12]={{31,28,31,30,31,30,31,31,30,31,30,31},{31,29,31,30,31,30,31,31,30,31,30,31}};
class Date
{
int year,month,day;
bool leap(int);
int dton(Date &);
Date ntod(int);
public:
Date() {} //默认构造函数;
Date(int y,int m,int d) {year=y;month=m;day=d;}//重载构造函数;
Date(const Date &d) //复制构造函数;
{
year=d.year;
month=d.month;
day=d.day;
}
Date operator+(int days) //返回一日期加上天数得到的日期;
{
static Date date;
int number=dton(*this)+days;
date=ntod(number);
return date;
}
Date operator-(int days) //返回一日期减去天数得到的日期;
{
static Date date;
int number=dton(*this)-days;
date=ntod(number);
return date;
}
int operator-(Date &b) //返回两日期相差的天数;
{
int days=dton(*this)-dton(b)-1;
return days;
}
bool operator < (const Date d)
{
if((year return true;
else
return false;
}
bool operator == (const Date d)
{
if((year==d.year && month==d.month && day==d.day))
return true;
else
return false;
}
bool operator > (const Date d)
{
if((year>d.year) || (year==d.year && month>d.month) ||(year==d.year && month==d.month && day>d.day))
return true;
else
return false;
}
void getDate()
{
cout<<"请输入日期:"< cout<<"年份:";
cin>>year;
cout<<"月份:";
cin>>month;
cout<<"日期:";
cin>>day;
}
void display() {cout<};
bool Date::leap(int year) //判断指定的年份日否为闰年;
{
if(year%4==0 && year%100!=0 || year%400==0)
return true;
else
return false;
}
int Date::dton(Date &d) //将指定的日期转换成从0年0月0日起的天数;
{
int y,m,days=0;
for(y=1;y<=d.year;y++)
if(leap(y)) days+=366;
else days+=365;
for(m=0;m if(leap(d.year)) days+=day_tab[1][m];
else days+=day_tab[0][m];
days+=d.day;
return days;
}
Date Date::ntod(int n) //将指定的0年0月0日起的天数转换成对应的日期;
{
int y=1,m=1,d,rest=n,lp;
while(1)
{
if(leap(y))
{
if(rest<=366) break;
else rest-=366;
}
else
{
if(rest<=365) break;
else rest-=365;
}
y++;
}
y--;
lp=leap(y);
while(true)
{
if(lp)
{
if(rest>day_tab[1][m-1]) rest-=day_tab[1][m-1];
else break;
}
else
{
if(rest>day_tab[0][m-1]) rest-=day_tab[0][m-1];
else break;
}
m++;
}
d=rest;
return Date(y,m,d);
}
void main()
{
Date now;
now.getDate();
cout<<"now:";now.display();
Date d1=now+1,d2=now-1;
cout<<"now+1:";d1.display();
cout<<"now-1:";d2.display();
}

回答3:

#include
using namespace std;

class Data{
private:
int myear,mmonth,mday;
public:
Data(int year=0,int month=0,int day=0):myear(year),mmonth(month),mday(day){}
void add(){int i=myear;
int j=mmonth;
if(mday==28){
if(mmonth==2){
if(i%400==0||i%4==0&&i%100!=0){
mmonth++;
mday=1;
}
else
mday++;
}
else{
mday++;
}
}
else if(mday==29){
if(mmonth==2){
if(i%400==0||i%4==0&&i%100!=0){
cout<<"不存在这日期";
exit(0);
}
else{
mmonth++;
mday=1;
}
}
else
mday++;
}
else if(mday==30){
if(j==4||j==6||j==9||j==11){
mmonth++;
mday=1;
}
else
mday++;
}
else if(mday==31){
if(j==4||j==6||j==9||j==11){
cout<<"不存在这日期";
exit(0);
}
else if(mmonth==12){
myear++;
mmonth=1;
mday=1;
}
else{
mmonth++;
mday=1;
}

}
else
mday++;
}
void display(){
cout< }

};
int main(){
int i,j,k;
cout<<"请输入日期";
cin>>i>>j>>k;
Data obj(i,j,k);
obj.add();
obj.display();
}

回答4:

#include

using namespace std;
class Date
{
public:
int temp=1;
void Cinnum(int y,int m,int d);
void setDate();
void Print();
void PrintNext();
void Summeasure();
private:
int year;
int month;
int day;
int flag;
};
void Date::Cinnum(int y,int m,int d)
{
year=y;
month=m;
day=d;
}
void Date::PrintNext()
{
cout<<"明天是:"<year<<"-"<month<<"-"<day<}
void Date::setDate()
{
if(this->year % 4 == 0 && ( this->year % 100 != 0 || this->year % 400 == 0))
flag=1;
else
flag=0;
if(month==2)
{
if((flag==0&&day>28)||(flag==1&&day>29))
{
temp=0;
return;
}
else if((flag==0&&day==28)||(flag==1&&day==29))
{
day=1;
month=month++;
}
else
day++;
}

else if(month==1||month==3|| month==5|| month==7
||month==8 ||month==10|| month==12)
{
if(day==31)
{
day=1;
if(month==12)
{
month=1;
year++;
}
else
month=month++;
}
else
day++;
}
else
{
if(day==30)
{
day=1;
month=month++;
}
else
day++;
}
}
int main()
{
int y,m,day;
Date d;
cout<<"请输入年月日:"<while(cin>>y>>m>>day)
{
d.Cinnum(y,m,day);
d.setDate();
if(y<0||m<1||m>12||d.temp==0)
{
cout<<"请重新输入正确的年月日:"<d.temp=1;
cout<<"请输入年月日:"<continue;
}
cout<<"今天是:"<d.PrintNext();
cout<<"请输入年月日:"<}
return 0;
}