Time Arithmetic by Tyler F

#include <iostream>\x0d
#include <iomanip>\x0d
\x0d
using std::cin;\x0d
using std::cout;\x0d
using std::setw;\x0d
\x0d
struct time\x0d
{\x0d
\x09int hour, min, sec;\x0d
\x09time(int h, int m, int s) { hour = h; min = m; sec = s; }\x0d
\x09time() { hour = 0; min = 0; sec = 0; }\x0d
\x09friend std::ostream& operator << (std::ostream& out, const time& thetime)\x0d
\x09{\x0d
\x09\x09char old = out.fill();\x0d
\x09\x09out.fill('0');\x0d
\x09\x09out << setw(2) << thetime.hour << ':' << setw(2) << thetime.min << ':' << setw(2) << thetime.sec;\x0d
\x09\x09out.fill(old);\x0d
\x09\x09return out;\x0d
\x09}\x0d
};\x0d
\x0d
typedef struct time t;\x0d
\x0d
t getTime();\x0d
t addTime(const t,const t);\x0d
void printTime(const t);\x0d
\x0d
int main()\x0d
{\x0d
\x09t first, second, result;\x0d
\x09char trash;\x0d
\x09for(;;)\x0d
\x09{\x0d
\x09\x09first = getTime();\x0d
\x09\x09cin >> trash;\x0d
\x09\x09if (cin.fail())\x0d
\x09\x09\x09return 0;\x0d
\x09\x09second = getTime();\x0d
\x09\x09result = addTime(first, second);\x0d
\x09\x09std::cout << first << '+' << second << '=' << result << '\n';\x0d
\x09}\x0d
}\x0d
\x0d
t getTime()\x0d
{\x0d
\x09//##:##:##\x0d
\x09t out;\x0d
\x09char junk;\x0d
\x09cin >> out.hour >> junk >> out.min >> junk >> out.sec;\x0d
\x09return out;\x0d
}\x0d
\x0d
t addTime(const t first,const t second)\x0d
{\x0d
\x09t result;\x0d
\x09result.sec = first.sec + second.sec;\x0d
\x09result.min = first.min + second.min;\x0d
\x09result.hour = first.hour + second.hour;\x0d
\x09if (result.sec > 59)\x0d
\x09{\x0d
\x09\x09result.min++;\x0d
\x09\x09result.sec -= 60;\x0d
\x09}\x0d
\x09if (result.min > 59)\x0d
\x09{\x0d
\x09\x09result.hour++;\x0d
\x09\x09result.min -= 60;\x0d
\x09}\x0d
\x09if (result.hour > 23)\x0d
\x09\x09result.hour -= 24;\x0d
\x09return result;\x0d
}\x0d
\x0d
void printTime(const t thetime)\x0d
{\x0d
\x09cout.fill('0');\x0d
\x09cout << setw(2) << thetime.hour << ':' << setw(2) << thetime.min << ':' << thetime.sec;\x0d
}

Note that non-ascii characters in the above source code will be escaped (such as \x9f).

download

return to the top page