模拟实现->日期类

1. 日期类的实现

class Date
{
public:
	// 构造函数
	Date(int year = 0, int month = 1, int day = 1);
	// 打印函数
	void Print() const;
	// 日期+=天数
	Date& operator+=(int day);
	// 日期+天数
	Date operator+(int day) const;
	// 日期-=天数
	Date& operator-=(int day);
	// 日期-天数
	Date operator-(int day) const;
	// 前置++
	Date& operator++();
	// 后置++
	Date operator++(int);
	// 前置--
	Date& operator--();
	// 后置--
	Date operator--(int);
	// 日期的大小关系比较
	bool operator>(const Date& d) const;
	bool operator>=(const Date& d) const;
	bool operator<(const Date& d) const;
	bool operator<=(const Date& d) const;
	bool operator==(const Date& d) const;
	bool operator!=(const Date& d) const;
	// 日期-日期
	int operator-(const Date& d) const;

	// 析构,拷贝构造,赋值重载可以不写,使用默认生成的即可

private:
	int _year;
	int _month;
	int _day;
};

1.1 构造函数

// 获取某年某月的天数
inline int GetMonthDay(int year, int month)
{
	// 数组存储平年每个月的天数
	static int dayArray[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
	int day = dayArray[month];
	if (month == 2 && ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)))
	{
		//闰年2月的天数
		day = 29;
	}
	return day;
}
// 构造函数
Date::Date(int year, int month, int day)
{
	// 检查日期的合法性
	if (year >= 0
	&& month >= 1 && month <= 12
	&& day >= 1 && day <= GetMonthDay(year, month))
	{
		_year = year;
		_month = month;
		_day = day;
	}
	else
	{
		// 严格来说抛异常更好
		cout << "非法日期" << endl;
		cout << year << "年" << month << "月" << day << "日" << endl;
	}
}
  •  GetMonthDay函数会被多次调用,所以最好设置成内联函数
  • 且该函数中的月天数用static修饰,避免每次调用该函数都需要重新开辟数组。
  • 当函数声明和定义分开时,在声明时注明缺省参数定义时不标出缺省参数

1.2 打印函数

// 打印函数
void Date::Print() const
{
	cout << _year << "年" << _month << "月" << _day << "日" << endl;
}

1.3 日期 += 天数

// 日期+=天数
Date& Date::operator+=(int day)
{
	if (day<0)
	{
		// 复用operator-=
		*this -= -day;
	}
	else
	{
		_day += day;
		// 日期不合法,通过不断调整,直到最后日期合法为止
		while (_day > GetMonthDay(_year, _month))
		{
			_day -= GetMonthDay(_year, _month);
			_month++;
			if (_month > 12)
			{
				_year++;
				_month = 1;
			}
		}
	}
	return *this;
}
  1. 首先判断日期是否合法 

  2. 若日已满,则日减去当前月的天数,月加一

  3. 若月已满,则将年加一,月置为1

1.4 日期 + 天数

// 日期+天数
Date Date::operator+(int day) const
{
	Date tmp(*this);// 拷贝构造tmp,用于返回
	// 复用operator+=
	tmp += day;

	return tmp;
}
  •  复用代码

1.5 日期 -= 天数

// 日期-=天数
Date& Date::operator-=(int day)
{
	if (day < 0)
	{
		// 复用operator+=
		*this += -day;
	}
	else
	{
		_day -= day;
		// 日期不合法,通过不断调整,直到最后日期合法为止
		while (_day <= 0)
		{
			_month--;
			if (_month == 0)
			{
				_year--;
				_month = 12;
			}
			_day += GetMonthDay(_year, _month);
		}
	}
	return *this;
}
  • 首先判断日期是否合法 

  • 若日为负数,则月减一

  • 若月为0,则年减一,月置为12
  • 日加上当前月的天数

1.6 日期 - 天数 

// 日期-天数
Date Date::operator-(int day) const
{
	Date tmp(*this);// 拷贝构造tmp,用于返回
	// 复用operator-=
	tmp -= day;

	return tmp;
}
  •   复用代码

1.8 自增自减运算符的实现

前置++

// 前置++
Date& Date::operator++()
{
	// 复用operator+=
	*this += 1;
	return *this;
}
  •  复用代码 

后置 ++

// 后置++
Date Date::operator++(int)
{
	Date tmp(*this);// 拷贝构造tmp,用于返回
	// 复用operator+=
	*this += 1;
	return tmp;
}
  • 注意: 后置++需要多给一个参数 
  •  复用代码

前置 - -

// 前置--
Date& Date::operator--()
{
	// 复用operator-=
	*this -= 1;
	return *this;
}
  •  复用代码

后置 - - 

// 后置--
Date Date::operator--(int)
{
	Date tmp(*this);// 拷贝构造tmp,用于返回
	// 复用operator-=
	*this -= 1;
	return tmp;
}
  • 注意: 后置++需要多给一个参数 
  •  复用代码

1.9 比较运算符的实现

>运算符的重载

bool Date::operator>(const Date& d) const
{
	if (_year > d._year)
	{
		return true;
	}
	else if (_year == d._year)
	{
		if (_month > d._month)
		{
			return true;
		}
		else if (_month == d._month)
		{
			if (_day > d._day)
			{
				return true;
			}
		}
	}
	return false;
}

==运算符的重载

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

 >=运算符的重载

bool Date::operator>=(const Date& d) const
{
	return *this > d || *this == d;
}

<运算符的重载

bool Date::operator<(const Date& d) const
{
	return !(*this >= d);
}

<=运算符的重载

bool Date::operator<=(const Date& d) const
{
	return !(*this > d);
}

 !=运算符的重载

bool Date::operator!=(const Date& d) const
{
	return !(*this == d);
}

 1.10  日期 - 日期

// 日期-日期
int Date::operator-(const Date& d) const
{
	Date max = *this;// 假设第一个日期较大
	Date min = d;// 假设第二个日期较小
	int flag = 1;// 此时结果应该为正值
	if (*this < d)
	{
		// 假设错误,更正
		max = d;
		min = *this;
		flag = -1;// 此时结果应该为负值
	}
	int n = 0;// 记录所加的总天数
	while (min != max)
	{
		min++;// 较小的日期++
		n++;// 总天数++
	}
	return n*flag;
}
  • 较小的日期的天数一直加一,直到最后和较大的日期相等即可
  • 代码中使用flag变量标记返回值的正负
    flag为1代表返回的是正值,flag为-1代表返回的是值,
    最后返回总天数与flag相乘之后的值即可。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的模拟智能养花系统的QT代码,实现了上述功能: ```cpp #include "mainwindow.h" #include "ui_mainwindow.h" #include <QDateTime> #include <QDebug> MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); // 初始化温度、湿度、阳光情况、时间和日期 m_temperature = 25.0; m_humidity = 60.0; m_sunshine = false; m_currentTime = QDateTime::currentDateTime(); // 定时器更新时间和日期 QTimer *timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &MainWindow::updateDateTime); timer->start(1000); // 每秒钟更新一次 // 定时器检测温度是否达到预设值 m_alarmTimer = new QTimer(this); connect(m_alarmTimer, &QTimer::timeout, this, &MainWindow::checkTemperature); m_alarmTimer->start(5000); // 每5秒检测一次 } MainWindow::~MainWindow() { delete ui; } void MainWindow::updateDateTime() { m_currentTime = QDateTime::currentDateTime(); ui->timeLabel->setText(m_currentTime.time().toString("hh:mm:ss")); ui->dateLabel->setText(m_currentTime.date().toString("yyyy-MM-dd")); } void MainWindow::checkTemperature() { // 当温度达到预设值时触发报警 if (m_temperature >= ui->tempSpinBox->value()) { qDebug() << "Temperature alarm!"; ui->tempAlarmLabel->setText("ALARM"); } else { ui->tempAlarmLabel->setText(""); } } void MainWindow::on_pumpButton_toggled(bool checked) { // 控制水泵 if (checked) { ui->pumpLabel->setText("ON"); // 修改温度、湿度 m_temperature += 0.5; m_humidity += 10.0; } else { ui->pumpLabel->setText("OFF"); } // 更新温度、湿度显示 ui->tempLabel->setText(QString("%1°C").arg(m_temperature, 0, 'f', 1)); ui->humidityLabel->setText(QString("%1%").arg(m_humidity, 0, 'f', 0)); } void MainWindow::on_lightButton_toggled(bool checked) { // 控制日照灯 if (checked) { ui->lightLabel->setText("ON"); // 修改阳光情况 m_sunshine = true; } else { ui->lightLabel->setText("OFF"); // 修改阳光情况 m_sunshine = false; } // 更新阳光情况显示 ui->sunshineLabel->setText(m_sunshine ? "SUNNY" : "CLOUDY"); } void MainWindow::on_humidifierButton_toggled(bool checked) { // 控制加湿机 if (checked) { ui->humidifierLabel->setText("ON"); // 修改温度、湿度 m_temperature += 0.2; m_humidity += 20.0; } else { ui->humidifierLabel->setText("OFF"); } // 更新温度、湿度显示 ui->tempLabel->setText(QString("%1°C").arg(m_temperature, 0, 'f', 1)); ui->humidityLabel->setText(QString("%1%").arg(m_humidity, 0, 'f', 0)); } ``` 该程序使用了定时器来更新时间和日期,以及检测温度是否达到预设值;同时,使用了按钮来模拟控制水泵、日照灯和加湿机。当用户按下按钮时,程序会相应地修改温度、湿度、阳光情况对应的值,并更新显示窗口的数据。如果温度达到预设值,程序会触发报警并在窗口上显示警告信息。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值