[面向对象程序设计]双计时器

实验要求

题目: 设计一个人工设置的双计时器(高考倒计时器和备考时间累积器),支持起始时间的人工设定和人工加、减时间操作。

基本要求

  1. 设计一个日期类 date。类体内需包含描述年、月、日等信息的数据成员,以及用于设置与读取这些数据成员的成员函数。
  2. 在类体内定义用于初始化对象的构造函数,包含一个重载默认值方式。
  3. 在 date 类中定义成员函数,用于处理月、日的进位的改变问题,特别注意不同月份天数的问题,判断闰年问题。
  4. 在 date 类中重载 + 或 -,++ 或 – 运算符重载,用于实现对日期对象进行加或减 n天,加或减1天操作。
  5. 设计一个双计时器界面,提供人工设定起始日期,人工加、减日期操作,测试 date 类中各成员函数是否能正确运行,并给出测试结果的运行截图。
  6. 对本大作业进行总结(存在的不足、问题、经验等)。

提高要求

  1. 通过继承方式设计出时间类(类名为 DateTime )(包含年、月、日、时、分、秒),给出类DateTime 的定义和实现,描述设计思路。
  2. 给出设完整的日历表类(类名为 CDate)设计,描述设计思路。

其他要求

  1. 运用课堂所学的知识,按题目的要求独立完成大作业及报告。
  2. 被认定为抄袭或被抄袭者,本课程最终成绩作不及格处理。
  3. 在网络教学平台上提交:(最后截止日期为:2020年7月12日)
    (1)大作业报告(word版和pdf版各一份)
    (2)源代码(仅提交 .h和 .cpp 文件)

效果展示

双计时器

在这里插入图片描述

初始化日历

在这里插入图片描述

源码

CDate.cpp

#include "CDate.h"


CDate::CDate(void)
{
    
    
}


CDate::~CDate(void)
{
    
    
}

CDate.h

#pragma once

#include <iostream>

#include "DateTime.h"

class CDate
{
    
    
public:
	CDate();
	~CDate();
	void SetYear(int year)
	{
    
    
		m_dateTime.set_year(year);
	}
	void SetMonth(int month)
	{
    
    
		m_dateTime.set_month(month);
	}
	void SetDay(int day)
	{
    
    
		m_dateTime.set_day(day);
	}
	void SetHour(int hour)
	{
    
    
		m_dateTime.set_hour(hour);
	}
	void SetMinute(int minute)
	{
    
    
		m_dateTime.set_minute(minute);
	}
	void SetSecond(int second)
	{
    
    
		m_dateTime.set_second(second);
	}

	void Display()
	{
    
    
		cout << "当前日历:"
			<< m_dateTime.get_year() << "年" << m_dateTime.get_month() << "月" << m_dateTime.get_day()
			<< "  "
			<< m_dateTime.get_hour() << ":" << m_dateTime.get_minute() << ":" << m_dateTime.get_second() << endl;
	}

	bool IsValid() {
    
     return m_dateTime.isDateTimeValid(); }

	void NextSecond()
	{
    
    
		m_dateTime.add_seconds(1);
		Display();
	}

private:
	DateTime m_dateTime;
};

date.cpp

#include "date.h"
#include <sstream>

using namespace std;

static bool IsLeapYear(int year)
{
    
    
	bool cond1 = (year % 4) == 0 && (year % 100) != 0;
	bool cond2 = (year % 400) == 0;
	if (cond1 || cond2)
	{
    
    
		return true;
	}
	return false;
}

date::~date()
{
    
    
}

date::date(const date& other)
{
    
    
	year_ = other.year_;
	month_ = other.month_;
	day_ = other.day_;
}

void date::Reset()
{
    
    
	year_ = 0;
	month_ = 0;
	day_ = 0;
}

void date::Display()
{
    
    
	cout << "[date] Display()" << endl;
	cout << "year_ :" << year_ << endl;
	cout << "month_ :" << month_ << endl;
	cout << "day_ :" << day_ << endl;
}

void date::change_day(int offset)
{
    
    
	const int LeapMonth[] = {
    
     31,29,31,30,31,30,31,31,30,31,30,31 };
	const int UnLeapMonth[] = {
    
     31,28,31,30,31,30,31,31,30,31,30,31 };

	const int* pMonth = UnLeapMonth;

	day_ = day_ + offset;
	while (true)
	{
    
    
		if (IsLeapYear(year_))
		{
    
    
			pMonth = UnLeapMonth;
		}

		if (month_ > 12)
		{
    
    
			year_ += 1;
			month_ -= 12;
			continue;
		}

		if (month_ < 0)
		{
    
    
			year_ -= 1;
			month_ += 12;
			continue;
		}

		int max_day = pMonth[month_ - 1];
		if (day_ > max_day)
		{
    
    
			month_ += 1;
			day_ -= max_day;
			continue;
		}

		if (day_ <= 0)
		{
    
    
			day_ += max_day;
			month_ -= 1;
			continue;
		}

		break;
	}
}

date date::operator--()//前置
{
    
    
	change_day(-1);
	return *this;
}

date date::operator--(int)//后置
{
    
    
	date tmp(*this);
	change_day(-1);
	return tmp;
}

date date::operator++()//前置
{
    
    
	change_day(1);
	return *this;
}

date date::operator++(int)//后置
{
    
    
	date tmp(*this);
	change_day(1);
	return tmp;
}

date& date::operator=(const date& obj)
{
    
    
	if (&obj != this)
	{
    
    
		year_ = obj.year_;
		month_ = obj.month_;
		day_ = obj.day_;
	}
	return *this;
}

bool date::operator==(const date& obj)
{
    
    
	if (obj.year_ != year_)	return false;
	if (obj.month_ != month_)	return false;
	if (obj.day_ != day_)	return false;
	return true;
}

long operator-(date obj1, date obj2)
{
    
    
	int flag = 1;
	long deleta = 0;
	date* max_date = &obj1;
	date* min_date = &obj2;
	if (obj1 < obj2)
	{
    
    
		max_date = &obj2;
		min_date = &obj1;
		flag = -1;
	}

	while (!(*min_date == *max_date))
	{
    
    
		min_date->change_day(1);
		deleta += 1;
	}

	return deleta * flag;
}

date date::operator+(int offset)
{
    
    
	change_day(offset);
	return *this;
}

date date::operator-(int offset)
{
    
    
	change_day((-1) * offset);
	return *this;
}

bool date::isValid()
{
    
    
	if (year_ < 0 || month_ < 1 || month_ > 12)
	{
    
    
		return false;
	}

	const int LeapMonth[] = {
    
     31,29,31,30,31,30,31,31,30,31,30,31 };
	const int UnLeapMonth[] = {
    
     31,28,31,30,31,30,31,31,30,31,30,31 };

	const int* pMonth = UnLeapMonth;

	int max_day = pMonth[month_ - 1];
	if (day_ < 1 || max_day > max_day)
	{
    
    
		return false;
	}

	return true;
}

bool date::operator>(const date& obj)
{
    
    
	if (year_ != obj.year_) return year_ > obj.year_;
	if (month_ != obj.month_) return month_ > obj.month_;
	if (day_ != obj.day_) return day_ > obj.day_;
	return false;
}

bool date::operator>=(const date& obj)
{
    
    
	if (year_ != obj.year_) return year_ > obj.year_;
	if (month_ != obj.month_) return month_ > obj.month_;
	if (day_ != obj.day_) return day_ > obj.day_;
	return true;
}

bool date::operator<(const date& obj)
{
    
    
	if (year_ != obj.year_) return year_ < obj.year_;
	if (month_ != obj.month_) return month_ < obj.month_;
	if (day_ != obj.day_) return day_ < obj.day_;
	return false;
}

bool date::operator<=(const date& obj)
{
    
    
	if (year_ != obj.year_) return year_ < obj.year_;
	if (month_ != obj.month_) return month_ < obj.month_;
	if (day_ != obj.day_) return day_ < obj.day_;
	return true;
}

date.h

#pragma once

#include <iostream>

using namespace std;

class date
{
    
    
public:
	date() :year_(0), month_(0), day_(0) {
    
    }
	date(int year, int month, int day)
	{
    
    
		year_ = year;
		month_ = month;
		day_ = day;
	}
	date(const date& other);

	~date();

	void change_day(int offset);

public:
	void Reset();
	void Display();

	void set_year(int year) {
    
     year_ = year; }
	void set_month(int month) {
    
     month_ = month; }
	void set_day(int day) {
    
     day_ = day; }

	int get_year() {
    
     return year_; }
	int get_month() {
    
     return month_; }
	int get_day() {
    
     return day_; }

	bool operator == (const date& obj);
	date& operator = (const date& obj);
	date operator++();//前置
	date operator++(int);//后置
	date operator--();//前置
	date operator--(int);//后置
	date operator+(int offset);
	date operator-(int offset);

	friend long operator-(date obj1, date obj);

	bool isValid();

	bool operator > (const date& obj);
	bool operator >= (const date& obj);
	bool operator < (const date& obj);
	bool operator <= (const date& obj);

private:
	int year_;
	int month_;
	int day_;
};

DateTime.cpp

#include "DateTime.h"


DateTime::DateTime(void) :date(), hour_(0), minute_(0), second_(0)
{
    
    
}


DateTime::~DateTime(void)
{
    
    
}

DateTime::DateTime(DateTime& other)
{
    
    
	set_year(other.get_year());
	set_month(other.get_month());
	set_day(other.get_day());
	set_hour(other.get_hour());
	set_minute(other.get_minute());
	set_second(other.get_second());
}

void DateTime::add_year(int nYear)
{
    
    
	set_year(get_year() + nYear);
}

void DateTime::add_month(int nMonth)
{
    
    
	set_month(get_month() + nMonth);
	change_day(0);
}

void DateTime::add_day(int nDay)
{
    
    
	set_day(get_day() + nDay);
	change_day(0);
}

void DateTime::add_hour(int nHour)
{
    
    
	set_hour(get_hour() + 1);
	change_time();
}

void DateTime::add_minute(int nMinute)
{
    
    
	set_minute(get_minute() + 1);
	change_time();
}

void DateTime::add_seconds(int nSecond)
{
    
    
	set_second(get_second() + 1);
	change_time();
}

void DateTime::change_time()
{
    
    
	while (true)
	{
    
    
		if (second_ < 0)
		{
    
    
			second_ += 60;
			minute_ -= 1;
			continue;
		}
		if (second_ >= 60)
		{
    
    
			second_ -= 60;
			minute_ += 1;
			continue;
		}
		if (minute_ >= 60)
		{
    
    
			minute_ -= 60;
			hour_ += 1;
			continue;
		}
		if (minute_ < 0)
		{
    
    
			minute_ += 60;
			hour_ -= 1;
			continue;
		}
		if (hour_ >= 24)
		{
    
    
			hour_ -= 24;
			set_day(get_day() + 1);
			continue;
		}
		if (hour_ < 0)
		{
    
    
			hour_ += 24;
			set_day(get_day() - 1);
			continue;
		}
		break;
	}
	change_day(0);
}

bool DateTime::isDateTimeValid()
{
    
    
	if (isValid() == false)
	{
    
    
		return false;
	}

	if (hour_ < 0 || hour_ >= 24)
	{
    
    
		return false;
	}

	if (minute_ < 0 || minute_ >= 60)
	{
    
    
		return false;
	}
	if (second_ < 0 || minute_ >= 60)
	{
    
    
		return false;
	}
	return true;
}

DateTime.h

#pragma once

#include "date.h"

class DateTime : public date
{
    
    
public:
	DateTime(void);
	DateTime(int year, int month, int day, int hour, int minute, int second)
		: date(year, month, day)
		, hour_(hour)
		, minute_(minute)
		, second_(second)
	{
    
    
	}
	~DateTime(void);
	DateTime(DateTime& other);

	void add_year(int nYear);
	void add_month(int nMonth);
	void add_day(int nDay);
	void add_hour(int nHour);
	void add_minute(int nMinute);
	void add_seconds(int nSecond);

	void set_hour(int h) {
    
     hour_ = h; }
	void set_minute(int m) {
    
     minute_ = m; }
	void set_second(int s) {
    
     second_ = s; }

	int get_hour() {
    
     return hour_; }
	int get_minute() {
    
     return minute_; }
	int get_second() {
    
     return second_; }

	void change_time();
	bool isDateTimeValid();
private:
	int hour_;
	int minute_;
	int second_;
};

main.cpp

// ts_timer.cpp : 定义控制台应用程序的入口点。
//

#include "date.h"
#include "CDate.h"
#include <iostream>
#include <time.h>
#include <Windows.h>
int main(void)
{
    
    
	int choice;

	cout << "\t[1]双计时器\t[2]初始化日历\n" << endl;
	cout << "请输入您的选择:";
	cin >> choice;
	
	if (choice == 1)
	{
    
    
		cout << "请设置当前日期:" << endl;
		int year = 0;
		int month = 0;
		int day = 0;
		cout << "年:"; cin >> year;
		cout << "月:"; cin >> month;
		cout << "日:"; cin >> day;
		date now_date(year, month, day);

		cout << "请设置高考日期:" << endl;
		cout << "年:"; cin >> year;
		cout << "月:"; cin >> month;
		cout << "日:"; cin >> day;
		date collect_date(year, month, day);

		cout << "请设置备考起始日期:" << endl;
		cout << "年:"; cin >> year;
		cout << "月:"; cin >> month;
		cout << "日:"; cin >> day;
		date prepare_data(year, month, day);


		cout << "=========================================================" << endl;
		cout << "1: 高考计时器;" << endl;
		cout << "2: 备考时间累计器;" << endl;
		cout << "=========================================================" << endl;
		int offset = 0;
		while (true)
		{
    
    
			cout << "\n请在当前日期下做偏移天数操作:";
			cin >> offset;
			cout << "偏移后日期信息:" << endl;
			//now_date.change_day(offset);
			now_date = now_date + offset;
			cout << now_date.get_year() << "/" << now_date.get_month() << "/" << now_date.get_day() << endl;
			long collect_off = (collect_date - now_date);
			long prepare_off = (now_date - prepare_data);
			cout << "距离高考日期(天数):" << collect_off << endl;
			cout << "高考备战累计(天数):" << prepare_off << endl;
		}

	}
	else if (choice == 2)
	{
    
    
		CDate now;
		while (true)
		{
    
    
			cout << "请初始化日历:";

			int year, month, day, hour, minute, second;
			cout << "当前年:";	cin >> year;	now.SetYear(year);
			cout << "当前月:";	cin >> month;	now.SetMonth(month);
			cout << "当前日:";	cin >> day;		now.SetDay(day);
			cout << "当前时:";	cin >> hour;		now.SetHour(hour);
			cout << "当前分:";	cin >> minute;		now.SetMinute(minute);
			cout << "当前秒:";	cin >> second;		now.SetSecond(second);
			if (now.IsValid())
			{
    
    
				break;
			}
			cout << "设定的日期无效,请重新设置!!" << endl;
		}

		cout << "开始启动日历运行:" << endl;
		while (true)
		{
    
    
			Sleep(1000);
			now.NextSecond();
		}
	}
	else
	{
    
    
		cout << "\t[1]双计时器\t[2]初始化日历\n" << endl;
		cout << "请输入您的选择:";
		cin >> choice;
	}

	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_51008866/article/details/116278310
今日推荐