-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
114 lines (106 loc) · 2.31 KB
/
main.cpp
File metadata and controls
114 lines (106 loc) · 2.31 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <boost/format.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
template<class Rap>
class Time
{
private:
Rap hour, minute, second;
public:
Time(Rap _hour, Rap _minute, Rap _second)
{
second = std::div(_second, 60).rem;
minute = std::div(_minute + std::div(_second, 60).quot, 60).rem;
hour = _hour + std::div(_minute + std::div(_second, 60).quot, 60).quot;
}
template<class Real>
Real get_as_second(void) const
{
return static_cast<Real>(60 * 60 * hour + 60 * minute + second);
}
template<class Real>
Real get_as_minute(void) const
{
return static_cast<Real>(60 * hour + minute + second / 60);
}
template<class Real>
Real get_as_hour(void) const
{
return static_cast<Real>(hour + minute / 60 + second / (60 * 60));
}
Rap get_second(void) const { return second; }
Rap get_minute(void) const { return minute; }
Rap get_hour(void) const { return hour; }
template<class Integer>
Time operator*(Integer _m)
{
Time temp(_m * this->hour, _m * this->minute, _m * this->second);
return temp;
}
template<class Integer>
Time& operator*=(Integer _m)
{
*this = *this * _m;
return *this;
}
};
using ITime = Time<int>;
namespace AST
{
struct Time
{
int h, m, s;
};
}
BOOST_FUSION_ADAPT_STRUCT
(
AST::Time,
(int, h)
(int, m)
(int, s)
)
void input(ITime& _t)
{
using namespace boost::spirit;
std::string input;
std::cout << "ATTENTION: Please input as following form." << std::endl;
std::cout << "XXXhYYYmZZZs" << std::endl;
std::getline(std::cin, input);
AST::Time origin;
auto result = qi::phrase_parse(input.begin(), input.end(), qi::int_ >> 'h' >> qi::int_ >> 'm' >> qi::int_, qi::ascii::space, origin);
if (result)
{
ITime temp(origin.h, origin.m, origin.s);
_t = temp;
}
else
{
throw std::runtime_error("Invalid form of expression.");
}
}
void output(const ITime& _t)
{
std::cout << boost::format("%1%ŽžŠÔ%2%•ª%3%•b") %_t.get_hour() %_t.get_minute() %_t.get_second() << std::endl;
}
int main()
{
try
{
ITime time(0, 0, 0);
input(time);
std::cout << "Original" << std::endl;
output(time);
time *= 2;
std::cout << "Doubled" << std::endl;
output(time);
}
catch(const std::runtime_error& _e)
{
std::cerr << _e.what() << std::endl;
}
std::system("pause");
return 0;
}