hozer

[OOP]Lab3

Sep 30th, 2014
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.36 KB | None | 0 0
  1. #include <iostream>
  2. #include <cctype>
  3. #include <math.h>
  4. #include <string.h>
  5.  
  6. using namespace std;
  7.  
  8. struct Time
  9. {
  10.     int h, m, s;
  11.  
  12.     void init()
  13.     {
  14.         h = m = s = 0;
  15.     }
  16.  
  17.     void init(int ah, int am, int as)
  18.     {
  19.         h = ah;
  20.         m = am;
  21.         s = as;
  22.     }
  23.  
  24.     void init(long as)
  25.     {
  26.         h += as / 3600;
  27.         as -= (as / 3600) * 3600;
  28.         m += as / 60;
  29.         as -= (as / 60) * 60;
  30.         s += as;
  31.     }
  32.  
  33.     void init(char *str)
  34.     {
  35.         char tm[] = "xx";
  36.         int k = 0, j = 0, i = 0, len = strlen(str), time[3];
  37.  
  38.         while (str[i] != '\0')
  39.         {
  40.             if (isdigit(str[i]) && j < 2) tm[j++] = str[i];
  41.             else if (str[i] == ':') { time[k++] = atoi(tm); j = 0; }
  42.             i++;
  43.         }
  44.         time[k] = atoi(tm);
  45.  
  46.         h = time[0];
  47.         m = time[1];
  48.         s = time[2];
  49.     }
  50.  
  51.     void input()
  52.     {
  53.         cout << "Input h m s: ";
  54.         cin >> h >> m >> s;
  55.     }
  56.  
  57.     int check()
  58.     {
  59.         return (h >= 0 && h <= 23 && m >= 0 && m <= 59) ? 1 : 0;
  60.     }
  61.  
  62.     char *toPchar()
  63.     {
  64.         char str[9];
  65.  
  66.         strcpy(str, itos(h, 2));
  67.         str[2] = ':';
  68.         strcpy(str + 3, itos(m, 2));
  69.         str[5] = ':';
  70.         strcpy(str + 6, itos(s, 2));
  71.         str[8] = '\0';
  72.         cout << str << '\n';
  73.         return str;
  74.     }
  75.  
  76.     char *itos(int n, int len)
  77.     {
  78.         char *s = new char[len + 1];
  79.         s[len] = '\0';
  80.         for (int i = 1; i <= len; i++)
  81.         {
  82.             if (n != 0)
  83.             {
  84.                 s[len - i] = n % 10 + 48;
  85.                 n /= 10;
  86.             }
  87.             else s[len - i] = '0';
  88.         }
  89.  
  90.         return s;
  91.     }
  92.  
  93.     void output()
  94.     {
  95.         cout << h << ':' << m << ':' << s << endl;
  96.     }
  97. };
  98.  
  99. class TTime
  100. {
  101.     Time t;
  102.  
  103. public:
  104.     void init()
  105.     {
  106.         t.init();
  107.     }
  108.  
  109.     void init(int h, int m, int s)
  110.     {
  111.         t.init(h, m, s);
  112.     }
  113.  
  114.     void init(long s)
  115.     {
  116.         t.init(s);
  117.     }
  118.  
  119.     void init(char *s)
  120.     {
  121.         t.init(s);
  122.     }
  123.  
  124.     void input()
  125.     {
  126.         t.input();
  127.     }
  128.  
  129.     void output()
  130.     {
  131.         t.output();
  132.     }
  133.  
  134.     char *toPChar()
  135.     {
  136.         return t.toPchar();
  137.     }
  138.  
  139.     void setStruct(int h, int m, int s)
  140.     {
  141.         t.init(h, m, s);
  142.     }
  143.  
  144.     Time getStruct()
  145.     {
  146.         return t;
  147.     }
  148.  
  149.     long toSec()
  150.     {
  151.         return t.h * 3600 + t.m * 60 + t.s;
  152.     }
  153.     long toMin()
  154.     {
  155.         return t.h * 60 + t.m;
  156.     }
  157.     long sub(TTime b)
  158.     {
  159.         long as = toSec(), bs = b.toSec();
  160.         return as - bs;
  161.     }
  162.     void add(long s)
  163.     {
  164.         t.h += s / 3600;
  165.         s -= (s / 3600) * 3600;
  166.         t.m += s / 60;
  167.         s -= (s / 60) * 60;
  168.         t.s += s;
  169.     }
  170.     void sub(long s)
  171.     {
  172.         long as = toSec();
  173.         as -= s;
  174.         t.init();
  175.         add(as);
  176.     }
  177.  
  178.     int comp(TTime b)
  179.     {
  180.         long as = toSec(), bs = b.toSec();
  181.  
  182.         if (as == bs) return 0;
  183.         return (as > bs) ? 1 : -1;
  184.     }
  185.  
  186.  
  187. };
  188.  
  189. long toSec(Time t)
  190. {
  191.     return t.h * 3600 + t.m * 60 + t.s;
  192. }
  193.  
  194. long toMin(Time t)
  195. {
  196.     return t.h * 60 + t.m;
  197. }
  198.  
  199. long sub(Time a, Time b)
  200. {
  201.     long as = toSec(a), bs = toSec(b);
  202.     return as - bs;
  203. }
  204.  
  205. Time add(Time t, long s)
  206. {
  207.     t.h += s / 3600;
  208.     s -= (s / 3600) * 3600;
  209.     t.m += s / 60;
  210.     s -= (s / 60) * 60;
  211.     t.s += s;
  212.  
  213.     return t;
  214. }
  215.  
  216. Time sub(Time a, long s)
  217. {
  218.     long as = toSec(a);
  219.     Time rez;
  220.     rez.init();
  221.     as -= s;
  222.     rez = add(rez, as);
  223.  
  224.     return rez;
  225. }
  226.  
  227. int comp(Time a, Time b)
  228. {
  229.     long as = toSec(a), bs = toSec(b);
  230.  
  231.     if (as == bs) return 0;
  232.     return (as > bs) ? 1 : -1;
  233. }
  234.  
  235.  
  236. int main()
  237. {
  238.     Time t1, t2;
  239.     TTime t3, t4;
  240.     char s[9];
  241.     scanf("%s", s);
  242.     t3.init(s);
  243.     t3.toPChar();
  244.     /*t1.input();
  245.     t2.input();
  246.     //test
  247.     t1.toPchar();
  248.     cout << comp(t1, t2) << endl;
  249.     sub(t1, 3200).toPchar();
  250.     add(t2, 3200).toPchar();
  251.     cout << sub(t1, t2) << endl;
  252.  
  253.     t3.input();
  254.     t4.input();
  255.  
  256.     t3.toPChar();
  257.     cout << t3.comp(t4) << endl;
  258.     t3.sub(3200);
  259.     t3.toPChar();
  260.     t3.add(3200);
  261.     t3.toPChar();
  262.     cout <<  t3.sub(t4) << endl;
  263.  
  264.  
  265.     cin.ignore();*/
  266.     cin.ignore();
  267.     cin.get();
  268. }
  269.  
  270. /*
  271. Визначити тип TTime для роботи з часом у форматі "година:хвилина:секунда".
  272. Тип повинен включати в себе не менше чотирьох функцій ініціалізації: числами,
  273. рядком (наприклад, "23:59:59"), секундами та часом. Обов’язковими операціями є:
  274. обчислення різниці між двома моментами часу в секундах, додавання часу й заданої
  275. кількості секунд, віднімання від часу заданої кількості секунд, порівняння моментів
  276. часу, переведення у секунди, переведення у хвилини (з округленням до цілої хвилини).
  277. */
Advertisement
Add Comment
Please, Sign In to add comment