C++小白,求问个日期类的问题

题目::

给定日期类的声明

class Date

{

public:

Date(int d = 0, int m = 0, int y = 0); //构造函数

int get_day() const; // 返回day

int get_month() const; //返回month

int get_year() const; // 返回year

static void set_default(int, int, int); //设置default_date

static int get_default_day(); //返回缺省day

static int get_default_month(); //返回缺省month

static int get_default_year(); //返回缺省year

Date & add_year(int n); //加n年

Date & add_month(int n); //加n月,考虑超过12月

Date & add_day(int n); //加n天,考虑进位月和年,考虑闰年

private:

int day, month, year;

static Date default_date; //初始化为 1901年1月1日

};

实现这个类并用以下的main函数测试它。

int main()

{

char type[110];

int day,mon,year;

int addday,addmon,addyear;

while(cin>>type)

{

if(strcmp(type,"Date") == 0)

{

cin>>day>>mon>>year;

Date mydate(day,mon,year);

cin>>addday>>addmon>>addyear;

mydate.add_day(addday).add_month(addmon).add_year(addyear);

cout << mydate.get_day() << " " << mydate.get_month() << " " << mydate.get_year() << endl;

}

else if(strcmp(type,"defaultDate") == 0)

{

cout << Date::get_default_day() << " " << Date::get_default_month() << " " << Date::get_default_year() << endl;

}

else if(strcmp(type,"setdefaultDate") == 0)

{

cin>>day>>mon>>year;

Date::set_default(day,mon,year);

cout << Date::get_default_day() << " " << Date::get_default_month() << " " << Date::get_default_year() << endl;

}

}

return 0;

}

输入

 

多组输入,每一组输入以一个 type来判断输入类型,如果是'Date',那么就输入当前设置的日期以及要加的天数;如果是'defaultDate',那么就是直接输出默认日期;如果是'setdefaultDate',那么就输入要设置的默认日期。

输出

 

输出计算后得到的日期。

输入样例 1 

Date 18 3 1981 2 1 1
defaultDate
setdefaultDate 1 1 1900

输出样例 1

20 4 1982
1 1 1901
1 1 1900

下面是我的代码和解题的思路

自己测试感觉ok了,一进OJ就WA,不知道哪里还有逻辑错误,还请大神指教

#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
//关键问题是解决进位之后的日期问题 决定最后再计算日的进位(有四种情况:进位前是二月/不是二月 &&进位后是二月/不是二月)
//先处理日期(相加后不管) 需要一个动态的计算方法(从二月加过去)(先对月做进位运算)
/*
*可以整一个常量数组来作为月份日期的计算
*/
int m[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
class Date
{
public:
	Date(int d = 0, int m =0, int y =0)
	{
		day = d;
		month = m;
		year = y;
	};//构造
	int get_day()const;//返回day
	int get_month()const;//返回month
	int get_year()const;//返回year
	static void set_default(int, int, int);//设置default_date
	static int get_default_day();//返回缺省day
	static int get_default_month();//返回缺省month
	static int get_default_year();//返回缺省year
	Date& add_year(int n) 
	{
		year += n;
		while (day > m[month])
		{
			if ((year % 100 != 0 && year % 4 == 0) || (year % 400 == 0))
			{
				m[2] = 29;
			}
			else
			{
				m[2] = 28;
			}
			day -= m[month];
			month += 1;
			if (month > 12)
			{
				month -= 12;
				year += 1;
			}
		}
		return *this;
	};//加n年
	Date& add_month(int n)
	{
		month += n;
		while (month > 12)
		{
			year += 1;
			month -= 12;
		}
		return *this;
		
	};//加n月,考虑超过12月
	Date& add_day(int n)
	{
		day += n;
		return *this;
	};//加n天,考虑进位月,年,闰年
private:
	int day, month, year;
	static Date default_date;//初始化为1901.1.1
};
Date Date::default_date(1,1,1901);
int Date::get_day()const
{
	return day;
}
int Date::get_month()const
{
	return month;
}
int Date::get_year()const
{
	return year;
}
void Date::set_default(int d = 0, int m = 0, int y = 0)
{
	default_date.day = d;
	default_date.month = m;
	default_date.year = y;
}
int Date::get_default_day()
{
	return default_date.day;
}
int Date::get_default_month()
{
	return default_date.month;
}
int Date::get_default_year()
{
	return default_date.year;
}


int main()
{
	char type[110];
	int day, mon, year;
	int addday, addmon, addyear;
	while (cin >> type)
	{
		if (strcmp(type, "Date") == 0)
		{
			cin >> day >> mon >> year;
			Date mydate(day, mon, year);
			cin >> addday >> addmon >> addyear;
			mydate.add_day(addday).add_month(addmon).add_year(addyear);

			cout << mydate.get_day() << " " << mydate.get_month() << " " << mydate.get_year() << endl;
		}
		else if (strcmp(type, "defaultDate") == 0)
		{
			cout << Date::get_default_day() << " " << Date::get_default_month() << " " << Date::get_default_year() << endl;
		}
		else if (strcmp(type, "setdefaultDate") == 0)
		{
			cin >> day >> mon >> year;
			Date::set_default(day, mon, year);
			cout << Date::get_default_day() << " " << Date::get_default_month() << " " << Date::get_default_year() << endl;
		}
	}
	return 0;
}

 

#pragma once //头文件

class Date{

private:
	int _year;
	int _month;
	int _day;

public:
	Date(int year=1990,int month=1,int day=1);
	Date(const Date& tmp);
    ~Date();
	int operator-(const Date& tmp);//日期相减
	Date operator+(int day);//日期与天数相加
	Date& operator++();//日期与天数相加
	Date& operator+=(int day);//日期与天数相加

	Date operator-(int day);//日期与天数相减
	bool operator>(const Date& tmp);//日期比较
	bool operator==(const Date& tmp);//日期是否相等


	void PrintDate();//打印日期
	bool IsleapYear(const Date& tmp);//是否闰年
	bool IsOverDay(const Date& tmp);//是否超过1个月的天数
	bool IsOverMonth(const Date& tmp);//是否超过1年的月数
};

#include<iostream>//函数文件
#include "date.h"

using namespace std;

Date::Date(int year,int month,int day)
{
    _year=year;
	_month=month;
	_day=day;
	if(year<1990||month<1||IsOverMonth(*this)||day<1||IsOverDay(*this))
	{	
		cout<<"无效的日期"<<endl;
		exit(-1);	
	}
}

Date::Date(const Date& tmp)
{
	_year=tmp._year;
	_month=tmp._month;
	_day=tmp._day;
}

Date::~Date()
{

}
int Date::operator-(const Date& tmp)//基本实现日期相减 (复杂逻辑)
{
	Date dateTmp;
	int count=0 ;
	int tmpCount=0;
	if(tmp._year-_year==0)
	{
		dateTmp._month=(_month<tmp._month)? _month:tmp._month;
		dateTmp._day=0;
		int maxDay=_month>tmp._month?_day:tmp._day;
		while(dateTmp._month!=(_month>tmp._month? _month:tmp._month)||
			dateTmp._day!=maxDay)
		{
			
			if(dateTmp._month!=(_month>tmp._month? _month:tmp._month))
			{
				if(dateTmp._month==1||dateTmp._month==3||dateTmp._month==5
					||dateTmp._month==7
				||dateTmp._month==8||dateTmp._month==10||dateTmp._month==12)
				{	
					count+=31;
					++dateTmp._month;
				}
				else if(dateTmp._month==2)
				{
					if(IsleapYear(tmp))
					{
						count+=29;
						++dateTmp._month;
					}
					else
					{
						count+=28;
						++dateTmp._month;
					}
				}
				else 
				{	
					count+=30;
					++dateTmp._month;
				}
			}
			else
			{
				++dateTmp._day;
				++count ;
			}
		}
		return count-(_month>tmp._month?_day:tmp._day);
	}
	else
	{
		dateTmp._year=(_year<dateTmp._year)? _year:tmp._year;
		dateTmp._month=1;
		dateTmp._day=0;
		int maxDay=(_year>tmp._year?_day:tmp._day);
		int tmpCount=0;
		while((dateTmp._year!=(_year>tmp._year? _year:tmp._year))||
			(dateTmp._month!=(_year>tmp._year? _month:tmp._month))||
			(dateTmp._day!=maxDay))
		{
			if(dateTmp._year!=(_year>tmp._year? _year:tmp._year))
			{
				if(IsleapYear(dateTmp))
				{
					count+=366;
					++dateTmp._year;
				}
				else
				{
					count+=365;
					++dateTmp._year;
				}
			}

			else if(dateTmp._month!=(_year>tmp._year? _month:tmp._month))
			{
				if(dateTmp._month==1||dateTmp._month==3||dateTmp._month==5
					||dateTmp._month==7
					||dateTmp._month==8||dateTmp._month==10||dateTmp._month==12)
				{	
					count+=31;
					++dateTmp._month;
				}
				else if(dateTmp._month==2)
				{
					if(IsleapYear(tmp))
					{
						count+=29;
						++dateTmp._month;
					}
					else
					{
						count+=28;
						++dateTmp._month;
					}
				}
				else 
				{	
					count+=30;
					++dateTmp._month;
				}
			}
			else
			{
				++count ;
				++dateTmp._day;		
			}
		}
		dateTmp._month=1;
		dateTmp._day=0;
		maxDay=_year<tmp._year?_day:tmp._day;
		while(dateTmp._month!=(_year<tmp._year? _month:tmp._month)||
			dateTmp._day!=maxDay)
		{
			
			if(dateTmp._month!=(_year<tmp._year? _month:tmp._month))
			{
				if(dateTmp._month==1||dateTmp._month==3||dateTmp._month==5
					||dateTmp._month==7
					||dateTmp._month==8||dateTmp._month==10||dateTmp._month==12)
				{	
					tmpCount+=31;
					++dateTmp._month;
				}
				else if(dateTmp._month==2)
				{
					if(IsleapYear(dateTmp))
					{
						tmpCount+=29;
						++dateTmp._month;
					}
					else
					{
						tmpCount+=28;
						++dateTmp._month;
					}
				}
				else 
				{	
					tmpCount+=30;
					++dateTmp._month;
				}
			}
			else
			{
				++dateTmp._day;
				++tmpCount ;
			}
		}
		return count-tmpCount;
	}
}
int Date::operator-(const Date& tmp)//日期相减  (与上个函数体 重复)
{
//利用其它 类的成员函数  简单实现日期相减
	int count=0;
	if(*this>tmp)
	{
		Date dateTmp(tmp);
		while(1)
		{
			if (dateTmp == *this)
				return count;
			if ((++dateTmp) == *this)
			{
				count++;
				return count;
			}
			count++;
		}
	}
	else
	{
		Date dateTmp(*this);
		while(1)
		{
			if (dateTmp == *this)
				return count;
			if ((++dateTmp) == *this)
			{
				count++;
				return count;
			}
			count++;
		}

	}
  
}

Date Date::operator+(int day)//日期与天数相加
{
	Date tmp(*this);
	int count=0;
	while(count!=day)
	{
		++tmp._day;
		if(IsOverDay(tmp))
		{
			++tmp._month;
			if(IsOverMonth(tmp))
			{
				++tmp._year;	
				tmp._month=1;
			}
			tmp._day=1;
		}
		count++;
	}
	return tmp;
}
Date Date::operator-(int day)//日期与天数相减
{
	Date tmp(*this);
	int count=0;
	while(count!=day)
	{
		--tmp._day;
		if(tmp._day==0)
		{
			--tmp._month;
			if(tmp._month==0)
			{
				--tmp._year;	
				tmp._month=12;
			}
			if(tmp._month==1||tmp._month==3||tmp._month==5||tmp._month==7
				||tmp._month==8||tmp._month==10||tmp._month==12)
			{	

				tmp._day=31;
			}
			else if(tmp._month==2)
			{
				if(IsleapYear(tmp))
					tmp._day=29;
				else
					tmp._day=28;
			}
			else 
				tmp._day=30;

		}
		count++;
	}
	return tmp;

}


bool Date::IsleapYear(const Date& tmp)//是否闰年
{
	return ((tmp._year%4==0&&tmp._year%100!=0)||(tmp._year%400==0));
}

bool Date::IsOverDay(const Date& tmp)//是否超过1个月的天数
{
	if(tmp._month==1||tmp._month==3||tmp._month==5||tmp._month==7
		||tmp._month==8||tmp._month==10||tmp._month==12)
	{	
		
			return tmp._day>31;
	}
	else if(tmp._month==2)
	{
		 if(IsleapYear(tmp))
			 return tmp._day>29;
		 else
			 return tmp._day>28;
	}
	else 
		return tmp._day>30;

}

bool Date::IsOverMonth(const Date& tmp)//是否超过12个月的
{
	return tmp._month>12;
}
void Date::PrintDate()
{
	cout<<_year<<"年"<<_month<<"月"<<_day<<"日"<<endl;
}

bool Date::operator>(const Date& tmp)//日期比较
{
	return (_year>=tmp._year)&&(_month)>=(tmp._month)&&(_day>=tmp._day);
}

bool Date::operator==(const Date& tmp)
{
	return ((_year==tmp._year)&&(_month==tmp._month)&&(_day==tmp._day));

}
Date& Date::operator++()//日期与天数相加  前置++
{
	int count = 0;
	while (count != 1)
	{
		++(this->_day);
		if (IsOverDay(*this))
		{
			++(this->_month);
			if (IsOverMonth(*this))
			{
				++(this->_year);
				this->_month = 1;
			}
			this->_day = 1;
		}
		count++;
	}
	return *this;
}

Date& Date::operator+=(int day)//日期与天数相加
{

	int count = 0;
	while (count != day)
	{
		++(this->_day);
		if (IsOverDay(*this))
		{
			++(this->_month);
			if (IsOverMonth(*this))
			{
				++(this->_year);
				this->_month = 1;
			}
			this->_day = 1;
		}
		count++;
	}
	return *this;
}


//主函数测试文件

#include<iostream>
#include "date.h"
using namespace std;

void test()
{
	Date d1(2010,1,1);
	Date d2(2010,1,1);
	
	//(d2-35).PrintDate();
	//(d2 +1).PrintDate();
	//(++d2).PrintDate();
	//(d1 += 100).PrintDate();
	//cout<<(d2>d1)<<endl;
	cout<<d1-d2<<endl;
}

int main()
{
	test();

	return 0;
}

 

现在具体是什么问题呢

 

你逻辑反了。

应该是先算day,然后month,然后再算year。在算day时,需要考虑当前的年份是不是闰年,如果是闰年,就把2月的天数设为28天,否则就是29天,这样逐渐累加上去就是了

您的问题已经有小伙伴解答了,请点击【采纳】按钮,采纳帮您提供解决思路的答案,给回答的人一些鼓励哦~~

ps:开通问答VIP,享受5次/月 有问必答服务,了解详情↓↓↓

【电脑端】戳>>>  https://vip.csdn.net/askvip?utm_source=1146287632
【APP 】  戳>>>  https://mall.csdn.net/item/52471?utm_source=1146287632