Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cctype>
- #include <math.h>
- #include <string.h>
- using namespace std;
- struct Time
- {
- int h, m, s;
- void init()
- {
- h = m = s = 0;
- }
- void init(int ah, int am, int as)
- {
- h = ah;
- m = am;
- s = as;
- }
- void init(long as)
- {
- h += as / 3600;
- as -= (as / 3600) * 3600;
- m += as / 60;
- as -= (as / 60) * 60;
- s += as;
- }
- void init(char *str)
- {
- char tm[] = "xx";
- int k = 0, j = 0, i = 0, len = strlen(str), time[3];
- while (str[i] != '\0')
- {
- if (isdigit(str[i]) && j < 2) tm[j++] = str[i];
- else if (str[i] == ':') { time[k++] = atoi(tm); j = 0; }
- i++;
- }
- time[k] = atoi(tm);
- h = time[0];
- m = time[1];
- s = time[2];
- }
- void input()
- {
- cout << "Input h m s: ";
- cin >> h >> m >> s;
- }
- int check()
- {
- return (h >= 0 && h <= 23 && m >= 0 && m <= 59) ? 1 : 0;
- }
- char *toPchar()
- {
- char str[9];
- strcpy(str, itos(h, 2));
- str[2] = ':';
- strcpy(str + 3, itos(m, 2));
- str[5] = ':';
- strcpy(str + 6, itos(s, 2));
- str[8] = '\0';
- cout << str << '\n';
- return str;
- }
- char *itos(int n, int len)
- {
- char *s = new char[len + 1];
- s[len] = '\0';
- for (int i = 1; i <= len; i++)
- {
- if (n != 0)
- {
- s[len - i] = n % 10 + 48;
- n /= 10;
- }
- else s[len - i] = '0';
- }
- return s;
- }
- void output()
- {
- cout << h << ':' << m << ':' << s << endl;
- }
- };
- class TTime
- {
- Time t;
- public:
- void init()
- {
- t.init();
- }
- void init(int h, int m, int s)
- {
- t.init(h, m, s);
- }
- void init(long s)
- {
- t.init(s);
- }
- void init(char *s)
- {
- t.init(s);
- }
- void input()
- {
- t.input();
- }
- void output()
- {
- t.output();
- }
- char *toPChar()
- {
- return t.toPchar();
- }
- void setStruct(int h, int m, int s)
- {
- t.init(h, m, s);
- }
- Time getStruct()
- {
- return t;
- }
- long toSec()
- {
- return t.h * 3600 + t.m * 60 + t.s;
- }
- long toMin()
- {
- return t.h * 60 + t.m;
- }
- long sub(TTime b)
- {
- long as = toSec(), bs = b.toSec();
- return as - bs;
- }
- void add(long s)
- {
- t.h += s / 3600;
- s -= (s / 3600) * 3600;
- t.m += s / 60;
- s -= (s / 60) * 60;
- t.s += s;
- }
- void sub(long s)
- {
- long as = toSec();
- as -= s;
- t.init();
- add(as);
- }
- int comp(TTime b)
- {
- long as = toSec(), bs = b.toSec();
- if (as == bs) return 0;
- return (as > bs) ? 1 : -1;
- }
- };
- long toSec(Time t)
- {
- return t.h * 3600 + t.m * 60 + t.s;
- }
- long toMin(Time t)
- {
- return t.h * 60 + t.m;
- }
- long sub(Time a, Time b)
- {
- long as = toSec(a), bs = toSec(b);
- return as - bs;
- }
- Time add(Time t, long s)
- {
- t.h += s / 3600;
- s -= (s / 3600) * 3600;
- t.m += s / 60;
- s -= (s / 60) * 60;
- t.s += s;
- return t;
- }
- Time sub(Time a, long s)
- {
- long as = toSec(a);
- Time rez;
- rez.init();
- as -= s;
- rez = add(rez, as);
- return rez;
- }
- int comp(Time a, Time b)
- {
- long as = toSec(a), bs = toSec(b);
- if (as == bs) return 0;
- return (as > bs) ? 1 : -1;
- }
- int main()
- {
- Time t1, t2;
- TTime t3, t4;
- char s[9];
- scanf("%s", s);
- t3.init(s);
- t3.toPChar();
- /*t1.input();
- t2.input();
- //test
- t1.toPchar();
- cout << comp(t1, t2) << endl;
- sub(t1, 3200).toPchar();
- add(t2, 3200).toPchar();
- cout << sub(t1, t2) << endl;
- t3.input();
- t4.input();
- t3.toPChar();
- cout << t3.comp(t4) << endl;
- t3.sub(3200);
- t3.toPChar();
- t3.add(3200);
- t3.toPChar();
- cout << t3.sub(t4) << endl;
- cin.ignore();*/
- cin.ignore();
- cin.get();
- }
- /*
- Визначити тип TTime для роботи з часом у форматі "година:хвилина:секунда".
- Тип повинен включати в себе не менше чотирьох функцій ініціалізації: числами,
- рядком (наприклад, "23:59:59"), секундами та часом. Обов’язковими операціями є:
- обчислення різниці між двома моментами часу в секундах, додавання часу й заданої
- кількості секунд, віднімання від часу заданої кількості секунд, порівняння моментів
- часу, переведення у секунди, переведення у хвилини (з округленням до цілої хвилини).
- */
Advertisement
Add Comment
Please, Sign In to add comment