Experimental requirements
Title: design a manually set double timer (college entrance examination countdown timer and test preparation time accumulator) to support manual setting of starting time and manual addition and subtraction of time.
Basic Requirements
- Design a date class date. The class body needs to contain data members describing year, month, day and other information, as well as member functions for setting and reading these data members.
- Define the constructor used to initialize the object in the class body, including an overloaded default value method.
- A member function is defined in the date class to handle the change of the carry of the month and day, pay special attention to the number of days in different months, and judge the leap year.
- Overload the + or -, + + or – operators in the date class, which is used to add or subtract n days and 1 day from the date object.
- A dual timer interface is designed to provide manual setting of start date, manual addition and subtraction of date, test whether each member function in date class can run correctly, and give a screenshot of the test results.
- Summarize the major operation (existing deficiencies, problems, experience, etc.).
Improve requirements
- Through inheritance, a time class (named DateTime) (including year, month, day, hour, minute and second) is designed, the definition and implementation of the class DateTime are given, and the design idea is described.
- The design of a complete calendar table class (class name CDate) is given, and the design idea is described.
Other requirements
- Use the knowledge learned in class and complete the homework and report independently according to the requirements of the topic.
- Those identified as plagiarism or being plagiarized will be treated as failing in the final score of this course.
- Submitted on the online teaching platform: (deadline: July 12, 2020)
(1) Large assignment report (one in word and one in pdf)
(2) Source code (submit only. h and. cpp files)
Effect display
Twin timer
Initialize calendar
Source code
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 << "Current calendar:" << m_dateTime.get_year() << "year" << m_dateTime.get_month() << "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--()//Front { change_day(-1); return *this; } date date::operator--(int)//Post { date tmp(*this); change_day(-1); return tmp; } date date::operator++()//Front { change_day(1); return *this; } date date::operator++(int)//Post { 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++();//Front date operator++(int);//Post date operator--();//Front date operator--(int);//Post 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: defines the entry point for the console application. // #include "date.h" #include "CDate.h" #include <iostream> #include <time.h> #include <Windows.h> int main(void) { int choice; cout << "\t[1]Twin timer \t[2]Initialize calendar\n" << endl; cout << "Please enter your choice:"; cin >> choice; if (choice == 1) { cout << "Please set the current date:" << endl; int year = 0; int month = 0; int day = 0; cout << "Year:"; cin >> year; cout << "Month:"; cin >> month; cout << "Date:"; cin >> day; date now_date(year, month, day); cout << "Please set the date of college entrance examination:" << endl; cout << "Year:"; cin >> year; cout << "Month:"; cin >> month; cout << "Date:"; cin >> day; date collect_date(year, month, day); cout << "Please set the test preparation start date:" << endl; cout << "Year:"; cin >> year; cout << "Month:"; cin >> month; cout << "Date:"; cin >> day; date prepare_data(year, month, day); cout << "=========================================================" << endl; cout << "1: College entrance examination timer;" << endl; cout << "2: Test preparation time accumulator;" << endl; cout << "=========================================================" << endl; int offset = 0; while (true) { cout << "\n Please offset the number of days under the current date:"; cin >> offset; cout << "Date information after offset:" << 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 << "Distance from college entrance examination date (days):" << collect_off << endl; cout << "Cumulative preparation for college entrance examination (days):" << prepare_off << endl; } } else if (choice == 2) { CDate now; while (true) { cout << "Please initialize the calendar:"; int year, month, day, hour, minute, second; cout << "Current year:"; cin >> year; now.SetYear(year); cout << "Current month:"; cin >> month; now.SetMonth(month); cout << "Current date:"; cin >> day; now.SetDay(day); cout << "Current time:"; cin >> hour; now.SetHour(hour); cout << "Current score:"; cin >> minute; now.SetMinute(minute); cout << "Current seconds:"; cin >> second; now.SetSecond(second); if (now.IsValid()) { break; } cout << "The set date is invalid, please reset!!" << endl; } cout << "Start calendar run:" << endl; while (true) { Sleep(1000); now.NextSecond(); } } else { cout << "\t[1]Twin timer \t[2]Initialize calendar\n" << endl; cout << "Please enter your choice:"; cin >> choice; } return 0; }