Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ConsoleApplication1.cpp : Этот файл содержит функцию "main". Здесь начинается и заканчивается выполнение программы.
- //
- #include <iostream>
- using namespace std;
- class Time {
- public:
- int h, m, s;
- Time() {
- h = 0;
- m = 0;
- s = 0;
- }
- Time(int h, int m, int s) {
- if (h < 0 || m < 0 || s < 0) throw 1;
- this->h = (h + (m + s / 60) / 60) % 24;
- this->m = (m + s / 60) % 60;
- this->s = s % 60;
- }
- Time(const Time& T) {
- this->h = T.h;
- this->m = T.m;
- this->s = T.s;
- }
- ~Time() {}
- Time& operator--() {
- s -= 5;
- if (s < 0) {
- s = 60 + s;
- m--;
- if (m < 0) {
- m = 60 + m;
- h--;
- if (h < 0)
- h = 24 + h;
- }
- }
- return *this;
- }
- Time operator--(int) {
- Time t(*this);
- s -= 5;
- if (s < 0) {
- s = 60 + s;
- m--;
- if (m < 0) {
- m = 60 + m;
- h--;
- if (h < 0)
- h = 24 + h;
- }
- }
- return t;
- }
- void operator+(Time T) {
- s = (s + T.s);
- m = (m + T.m + s / 60);
- h = (h + T.h + m / 60) % 24;
- m %= 60;
- s %= 60;
- }
- friend Time& operator++(Time&);
- friend Time operator++(Time&, int);
- friend bool operator!=(Time, Time);
- friend ostream& operator<<(ostream&, Time);
- friend istream& operator>>(istream&, Time&);
- };
- Time& operator++(Time &T) {
- T.s += 5;
- T.m += T.s / 60;
- T.h += T.m / 60;
- T.h %= 24;
- T.m %= 60;
- T.s %= 60;
- return T;
- }
- Time operator++(Time &T, int) {
- Time t(T);
- T.s += 5;
- T.m += T.s / 60;
- T.h += T.m / 60;
- T.h %= 24;
- T.m %= 60;
- T.s %= 60;
- return t;
- }
- bool operator!=(Time T1, Time T2) {
- return (T1.h != T2.h || T1.m != T2.m || T1.s != T2.s);
- }
- ostream& operator<<(ostream& out, Time T) {
- out << T.h << ":" << T.m << ":" << T.s;
- return out;
- }
- istream& operator>>(istream& in, Time& T) {
- int h, m, s;
- in >> h >> m >> s;
- T = Time(h, m, s);
- return in;
- }
- int main() {
- Time t(0, 0, 3), v(23, 59, 59);
- try {
- Time e(-1, 20, 20);
- }
- catch (int) {
- cout << "Errror!\n";
- }
- cout << t++ << endl;
- cout << t << endl;
- t + v;
- cout << t << endl;
- cout << (t != v) << endl;
- cout << t << endl;
- cout << --t << endl;
- cout << t << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment