【C++】实现data类

//#include<stdio.h>
#include<iostream>
#include<assert.h>
using namespace std;
class Date
{
public:
	Date(int year = 1900, int month = 1, int day = 1)//全缺省参数构造函数
		:_year(year)
		, _month(month)
		, _day(day)
	{
	}

	Date(const Date& d){//拷贝构造函数
		_year = d._year;
		_month = d._month;
		_day = d._day;
	}
	Date& operator=(const Date& d){//赋值构造函数
		if (this != &d){
			_year = d._year;
			_month = d._month;
			_day = d._day;
		}
		return *this;
	}
	~Date(){}//析构函数
	bool isLeapYear(int year){//判断是否为闰年
		if (year < 1990){
			return false;
		}
		else{
			if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
				return true;
			}
		}
		return false;
	}
	int GetMonthDay(int year, int month){//获取每个月的天数
		if (year < 1990 || month<0 || month>12){
			return 0;
		}
		int days[] = { 0, 31, 28, 31, 30, 31, 30, 31, 30, 31, 30, 31, 30 };
		int day = days[month];
		if (isLeapYear(year)||month==2){
			day++;
		}
		return day;
	}
	void Show(){//输出函数
		cout << _year << "-" << _month << "-" << _day << endl;
	}
	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 || _day != d._day){
			return true;
		}
		else{
			return false;
		}*/
		if (!operator==(d)){
			return true;
		}
		else{
			return false;
		}
	}
	//	// d1 < d2 
	bool operator<(const Date& d){//判断d1是否小于d2
		if (_year < d._year){
			return true;
		}
		else if (_month < d._month){
			return true;
		}
		else if (_day < d._day){
			return true;
		}
		else{
			return false;
		}
	}
	bool operator>=(const Date& d){//判断d1是否大于等于d2
		if (!operator<(d)){
			return true;
		}
		else{
			return false;
		}
	}
	bool operator<=(const Date& d){//判断d1是否小于等于d2

		if (operator<(d) || operator==(d)){
			return true;
		}
		else{
			return false;
		}
	}
	bool operator>(const Date& d){//判断d1是否大于d2

		if (!operator<=(d)){
			return true;
		}
		else{
			return false;
		}
	}
	 
	//	// d1 + 10 
	Date operator+(int day){
		Date ret(*this);
		ret._day += day;
		while (ret._day > GetMonthDay(ret._year, ret._month)){
			ret._day -= GetMonthDay(ret._year, ret._month);
			++ret._month;
			if (ret._month == 13){
				ret._month = 1;
				++ret._year;
			}
		}
		return ret;
	}
	Date& operator+=(const int day){
		_day += day;
		while (_day > GetMonthDay(_year, _month)){
			_day -= GetMonthDay(_year, _month);
			++_month;
			if (_month == 13){
				_month = 1;
				++_year;
			}
		}
		return *this;
	}
	Date operator-(int day){
		Date ret(*this);
		ret._day -= day;
		while (ret._day <1){
			--ret._month;
			if (ret._month < 1){
				ret._month = 12;
				--ret._year;
			}
			ret._day += GetMonthDay(ret._year, ret._month); 
		}
		return ret;
	}
	Date& operator-=(int day){
		_day -= day;
		while (_day < 1){
			--_month;
			if (_month < 1){
				_month = 12;
				--_year;
			}
			_day += GetMonthDay(_year, _month);
		}
		return *this;
	}
	int operator-(const Date& d){
		Date max = 0;
		Date min = 0;
		if (operator>(d)){
			max = *this;
			min = d;
		}else if(operator==(d)){
			return 0;
		}
		else{
			max = d;
			min = *this;
		}
		int count = 0;
		while (min.operator!=(max)){
			min.operator++(1);
			count++;
		}
		return count;
	}

	++d1 
	Date& operator++() // 前置 
	{
		return *this += 1;
	}

	//	//d1++  
	Date operator++(int) // 后置
	{
		Date ret(*this);
		ret += 1;
		*this = ret;
		return ret;
	}
	//	Date operator--();
	//Date operator--(int);
private: 
	int _year;
	int _month;
	int _day;
};

int main()
{
	Date d1(2012,7,8); 
//	d1.operator-(60).Show();
	/*d1.Show();*/

	Date d2(2012, 7, 27);
	//d2.operator-=(20).Show();
	cout << d1.operator-(d2) << endl;
	//d1 .operator+=(1000);
	//cout << (d1 != d2) << endl;
	//cout << (d1<=d2) << endl;
	//d1.Show();
	//Date d2(2011, 2, 12);
	//d2.operator+(1).Show();
	//d2.Show();
	//++d1; 
	//d1.operator++().Show(); 
	//d1++;
//	d1.operator++(1).Show(); 
	system("pause");
	return 0;
}











  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 请创建一个抽象DataStructure,该包括下面的成员变量和成员函数: 1) 一个成员变量len,表示里面的元素个数最大值 2) 构造函数DataStructure(int l),将len初始化为0 3) 虚析构函数~DataStructure() 4) 纯虚函数Output(),输出DataStructure中的数据 5) 纯虚函数Size(),返回DataStructure中的元素个数 2. 请创建DataStructure的一个派生MyString,该包括下面的成员变量和成员函数: 1) 一个成员变量char* data,表示里面的数据 2) 构造函数MyString(int max_size),将MyString初始化为空串,最大元素个数为max_size 3) 析构函数~MyString(),释放相应的数据 4) Input()函数,往MyString输入数据 5) 重载operator+=()函数,实现两个字符串的连接 6) 重定义Output()和Size()函数 3. 请创建DataStructure的一个派生MyStack,该包括下面的成员变量和成员函数: 1) 一个成员变量int* data,用于里面的数据 2) 一个成员变量int top,表示最上面的元素下标 3) 构造函数MyStack(int max_size),将MyStack初始化为空栈,最大元素个数为max_size 4) 析构函数~MyStack(),释放相应的数据 5) Push_back(int e)函数,往栈里面压入一个数据e 6) 重定义Output()和Size()函数 4. 请编写main函数,测试上面程序的正确性 1) 创建两个MyString的对象str1和str2,分别调用Input函数输入str1和str2,然后分别调用operator+=函数将str2连接到str1的末尾 2) 创建一个MyStack的对象stack,调用Push_back函数输入往stack中输入m(m < max_size)个数据 3) 创建一个长度为3的DataStructure*型的数组,将其3个元素分别指向str1, str2, stack,然后编写for循环调用Size()和Output()函数输出每个元素的大小和内容。 5. 输入输出样例: 1) 输入样例 A promising techni que for checking reachability 4 12 23 34 45 2) 输出样例 47 A promising technique for checking reachability 29 que for checking reachability 4 12 23 34 45

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值