Time operation (implemented in C + +)

  • Experimental principle

Rules and application of operator overloading

  • Experimental content

Title: design and implement a time class and its related operations.
Requirement:

  1. Time class includes hour, minute and second.
  2. Time, minute and second can be read and set.
  3. Format time HH: MM: SS can be output.
  4. Implement time comparison operation, greater than (>), less than (<), and equal to (= =).
  5. Realize time arithmetic operation, add (+), subtract (-), auto increment for one second (+ + before and after), auto decrement for one second (– before and after), increase n seconds (+ =), and decrease n seconds (- =).
  6. Each operator tests three sets of data.
  7. Pay attention to the robustness of the program.
  • Experimental results (including source code)

#include <iostream>
using namespace std;

class Time {
public:
	//Constructor
	Time(int h=0,int m=0,int s=0):hour(h),minute(m),second(s){}
	//Setup time
	void SetTime() {
		int h, m, s;
		cin >> h >> m >> s;
		if (h >= 0 && m >= 0 && s >= 0) {	//Judge the validity of input value
			hour = h, minute = m, second = s;
		}
		else
			return;
	}
	int GetHour() { return hour;}
	int GetMinute() { return minute;}
	int GetSecond() { return second;}
	//Read time
	void display() {
		cout << hour << ": " << minute << ": " << second << endl;
	}
	
	//Time comparison >
	bool operator> (Time &time2) {
		if (hour > time2.hour) { return true; }
		else if (hour == time2.hour && minute > time2.minute) { return true; }
		else if (hour == time2.hour && minute == time2.minute && second > time2.second) { return true; }
		else { return false; }
	}
	//Time comparison operation<
	bool operator< (Time &time2) {
		if (hour < time2.hour) { return true; }
		else if (hour == time2.hour && minute < time2.minute) { return true; }
		else if (hour == time2.hour && minute == time2.minute && second < time2.second) { return true; }
		else { return false; }
	}
	//Time comparison operation=
	bool operator== (Time &time2) {
		if (hour == time2.hour && minute == time2.minute && second == time2.second) { return true; }
		else { return false; }
	}
	
	//Time addition+
	Time operator+ (const Time &time2) {
		hour = hour + time2.hour;
		minute = minute + time2.minute;
		second = second + time2.second;
		//Add, min, SEC full 60 carry
		if (second >= 60) {
			minute++; second -= 60;
		}
		if (minute >= 60) {
			hour++; minute -= 60;
		}
		return Time(hour, minute, second);
	}
	//Time subtraction-
	Time operator- (const Time &time2) {
		hour = hour - time2.hour; 
		minute = minute - time2.minute; 
		second = second - time2.second;
		//Subtractive loan
		if (hour > 0) {
			if (second < 0) {
				minute--; second += 60;
			}
			if (minute < 0) {
				hour--; minute += 60;
			}
		}
		else {
			if (second > 0) {
				minute--; second -= 60;
			}
			if (minute > 0) {
				hour--; minute -= 60;
			}
		}
		return Time(hour, minute, second);
	}
	
	//Time auto increment one second (post)
	Time operator++ () {
		second++;
		if (second == 60) {
			minute++; second = 0;
			if (minute == 60) {
				hour++; minute = 0;
			}
		}
		return Time(hour , minute , second );
	}
	//Time is increased by one second (before)
	const Time operator++ (int) {
		Time temp = *this;
		++(*this);
		return temp;
	}
	
	//Time auto minus one second (post)
	Time operator-- () {
		second--;
		if (hour == 0 && second == 0) { ; }
		else {
			if (second == -1) {
				minute--; second += 60;
				if (minute == -1) {
					hour--; minute += 60;
				}
			}
		}
		return Time(hour, minute, second);
	}
	//Time auto minus one second (front)
	const Time operator-- (int) {
		Time temp = *this;
		--(*this);
		return temp;
	}
	
	//Increase n seconds
	Time operator+=(int n) {
		second += n;
		if (second >= 60) {
			minute++; second -= 60;
			if (minute == 60) {
				hour++; minute -= 60;
			}
		}
		return Time(hour, minute, second);
	}
	//Reduce n seconds
	Time operator-= (int n) {
		second -= n;
		if (hour == 0 && second == 0) { ; }
		else {
			if (second < 0) {
				minute--; second += 60;
				if (minute < 0) {
					hour--; minute += 60;
				}
			}
		}
		return Time(hour, minute, second);
	}

private:
	int hour, minute, second;
};
int main()
{
	Time T1, T2, T3, T4;
	cout << "Please enter the time T1:   ";
	T1.SetTime(); cout << "T1 = "; T1.display();
	cout << "Please enter the time T2:   ";
	T2.SetTime(); cout << "T2 = "; T2.display();
	cout << endl;

	//Compare the size of two times
	if (T1 > T2) {
		cout << "T1 > T2" << endl;
	}	
	else if (T1 < T2) {
		cout << "T1 < T2" << endl;
	}
	else {
		cout << "T1 = T2" << endl;
	}
	cout << endl;

	//Add and subtract two times
	T3 = T1 - T2; cout << "T3 = T1 - T2 = "; T3.display();
	T1 = T3 + T2; cout << "T1 = T3 + T2 = "; T3.display();
	cout << endl;

	//Auto increment (front)
	cout << "T3 = ++T1 " << endl; T3= ++T1;
	cout << "T3 = "; T3.display();
	cout << "T1 = "; T1.display();
	cout << endl;

	//Auto increment (post)
	cout << "T3 = T1++" << endl; T3 = T1++;
	cout << "T3 = "; T3.display();
	cout << "T1 = "; T1.display();
	cout << endl;

	//Self reduction (front)
	cout << "T3 = --T2 " << endl; T3 = --T2;
	cout << "T3 = "; T3.display();
	cout << "T2 = "; T2.display();
	cout << endl;

	//Self reduction (post)
	cout << "T3 = T2--" << endl; T3 = T2--;
	cout << "T3 = "; T3.display();
	cout << "T2 = "; T2.display();
	cout << endl;

	//Add n seconds
	T1 += 4; cout << "T1 + 4s = "; T1.display();
	T2 -= 3; cout << "T2 - 3s = "; T2.display();

	system("pause");
	return 0;
}

Test result I

Test result 2

Test result 3

Published 9 original articles, praised 0, visited 17
Private letter follow

Tags: less

Posted on Fri, 14 Feb 2020 07:36:15 -0800 by wakenbake