#include <iostream>
using namespace std;

//실습) 현재 시각을 나타내는 Time 클래스를 정의한다.
//- 시는 0~23, 분은 0~59, 초는 0~59  입력
// + , -연산자를 중복 정의하라.
//- 멤버 함수로 정의
//- 초 로 변환해서 연산
//- / , % 사용해서 시 / 분 / 초 로 변환
//- +연산시 24시가 넘을 경우, -연산시 0시 이전이 될 경우
// <, >, == 연산자를 중복 정의하라.
//- 멤버 함수로 정의
//- 초 로 변환해서 연산
// >> , << 연산자를 중복 정의하라
//- friend 함수로 정의



// 초를 시간으로 바꾸기



class cTime
{
private:
	int hours;		// 시간
	int minute;		// 분
	int second;		// 초

public:

	cTime(int hours = 0, int minute = 0, int second = 0) : hours(hours), minute(minute), second(second) {} 	// 생성자



	int totsum() { // 자기 값 초단위로 넣기
		return ((((this->hours * 60) + this->minute) * 60) + this->second);
	}

	int totsum(const cTime& temp) { // 비교 할 값 초단위로 넣기
		return ((((temp.hours * 60) + temp.minute) * 60) + temp.second);
	}


	cTime operator+(int d) { 
		int tot = totsum() + d;

		while (tot > 86400)
			tot -= 86400;

		hours = (tot / 3600) % 24;
		minute = (tot / 60) % 60;
		second = tot % 60;

		return cTime(hours, minute, second); }  // 덧셈 연산자

	cTime operator-(int d) { 
		int tot = totsum() - d;

		while (tot < 0)
			tot += 86400;

		hours = (tot / 3600) % 24;
		minute = (tot / 60) % 60;
		second = tot % 60;
				
		return cTime(hours, minute, second); }  // 뺄셈 연산자

	cTime operator++(int) {  //클래스 후처리 ++ 증감연산자
		cTime a(*this);

		int tot = totsum() + 1;

		while (tot < 0)
			tot += 86400;

		hours = (tot / 3600) % 24;
		minute = (tot / 60) % 60;
		second = tot % 60;

		return a;
	}

	cTime operator--(int) { // 클래스 후처리 -- 증감연산자
		cTime a(*this);
		int tot = totsum() - 1;

		while (tot < 0)
			tot += 86400;

		hours = (tot / 3600) % 24;
		minute = (tot / 60) % 60;
		second = tot % 60;

		return a;
	}



	bool operator<(const cTime& o) { //클래스 비교연산자(<)
		if (totsum() < totsum(o))
			return 1;
		else
			return 0;
	}


	bool operator>(const cTime& o) { //클래스 비교연산자(>)
		if (totsum() > totsum(o))
			return 1;
		else
			return 0;
	}

	bool operator==(const cTime& o) { //클래스 비교연산자(==)
		return totsum() == totsum(o);
	}

	friend ostream& operator<<(ostream& cout, const cTime& temp);

	friend istream& operator>>(istream& cin, cTime& temp);

};


ostream& operator<<(ostream& cout, const cTime& temp) { // 클래스 출력 함수 (시:분:초)

	cout << temp.hours << ":" << temp.minute << ":" << temp.second <<endl;
	return cout;
}

istream& operator>>(istream& cin, cTime& temp) {  // 클래스 입력 함수 (시 분 초)
	cin >> temp.hours >> temp.minute >> temp.second;

	int total = temp.totsum();

	while (total < 0)
		total += 86400;

	temp.hours = (total / 3600) % 24;
	temp.minute = (total / 60) % 60;
	temp.second = total % 60;
	
	

	return cin;
}



int main() {

	cTime start(1); // 1:0:0 입력
	cTime end; // 0:0:0 입력

	end = start+1000;
	cout << "end = start + 1000 : " << end << endl;

	end = end-500;
	cout << "end = end - 500 : " << end << endl;

	cout << "start < end : " << (start < end) << endl;
	cout << "start > end : " << (start > end) << endl;

	cout << "cin>>";
	cin >> start;
	cout << "cout<<"<< start <<endl;

	
	cout << "start++ = " << start++ << endl;
	cout << "start = " << start << endl;

	cout << "start-- = " << start-- << endl;
	cout << "start = " << start << endl;


}

Posted by pi92

블로그 이미지
pi92

공지사항

Yesterday
Today
Total

달력

 « |  » 2025.9
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30

최근에 올라온 글

최근에 달린 댓글

글 보관함