SHOW:
|
|
- or go back to the newest paste.
1 | // .a.out 23:25 dms q as demo input | |
2 | ||
3 | - | Time(); |
3 | + | #include <boost/regex.hpp> |
4 | - | int hours = 0; |
4 | + | #include <cstring> |
5 | - | int minutes = 0; |
5 | + | #include <iostream> |
6 | using namespace std; | |
7 | ||
8 | class Time { | |
9 | public: | |
10 | ||
11 | Time(); | |
12 | ||
13 | bool Parse(string time) { | |
14 | hours = 0; | |
15 | minutes = 0; | |
16 | boost::regex xRegEx1("^([0-2][0-9])([0-5][0-9])(\\s[AP]M)"); | |
17 | boost::smatch xResults; | |
18 | if (!boost::regex_match(time, xResults, xRegEx1)) { | |
19 | hours = strtol(xResults[1].str().c_str(), NULL, 10); | |
20 | minutes = strtol(xResults[2].str().c_str(), NULL, 10); | |
21 | if (strcmp(xResults[3].str().c_str(), "PM")==0) hours = hours + 12; | |
22 | return true; | |
23 | } else return false; | |
24 | } | |
25 | ||
26 | int GetHours () { | |
27 | return hours; // нет декларации в этой области видимости - ? | |
28 | } | |
29 | ||
30 | int GetMinutes() { | |
31 | return hours; // нет декларации в этой области видимости - ? | |
32 | } | |
33 | ||
34 | private: | |
35 | int hours; | |
36 | int minutes; | |
37 | }; | |
38 | ||
39 | ||
40 | class AnalogClock { | |
41 | public: | |
42 | AnalogClock(Time* t) { // передаем указатель на экземпляр класса время | |
43 | time = t; | |
44 | } | |
45 | ||
46 | float GetAngle() { | |
47 | angle = 0; | |
48 | int hours = time->GetHours(); | |
49 | if (hours>12) hours = hours - 12; | |
50 | int minutes = time->GetMinutes(); | |
51 | if (minutes==0) angle = abs(hours*30-minutes*6); | |
52 | else angle = abs((hours+1/minutes)*30-minutes*6); | |
53 | if (angle>180) angle=abs(180-angle); | |
54 | return angle; | |
55 | } | |
56 | ||
57 | private: | |
58 | float angle; | |
59 | }; | |
60 | ||
61 | ||
62 | class QuartzClock { | |
63 | public: | |
64 | QuartzClock(Time* t) { | |
65 | time = t; | |
66 | float angle = 0; | |
67 | } | |
68 | ||
69 | float GetAngle() { | |
70 | int hours = time->GetHours(); | |
71 | if (hours>12) hours = hours - 12; | |
72 | int minutes = time->GetMinutes(); | |
73 | angle = abs(hours*30-minutes*6); if (angle>180) angle=abs(180-angle); | |
74 | return angle; | |
75 | } | |
76 | }; | |
77 | ||
78 | ||
79 | class Angle { | |
80 | public: | |
81 | Angle(const char* param3) { | |
82 | if ((strcmp(param3,"q")!=0) Angle(QuartzClock *c); | |
83 | if ((strcmp(param3,"a")!=0) Angle(AnalogClock *c); | |
84 | clock = c; | |
85 | float angle = clock->GetAngle(); | |
86 | string angle1; | |
87 | } | |
88 | ||
89 | char ConvertedAngle(const char* tformat) { | |
90 | if ((strcmp(tformat,"deg")!=0) { | |
91 | angle1=FloatToStr(angle); | |
92 | return angle1; | |
93 | } | |
94 | if ((strcmp(tformat,"rad")!=0) { | |
95 | angle=angle*(M_PI)/180; | |
96 | angle1=FloatToStr(angle); | |
97 | return angle1; | |
98 | } | |
99 | if ((strcmp(tformat,"dms")!=0) { | |
100 | int hrs = int(angle+0.5); | |
101 | int minst = (angle-floor(angle))*60; | |
102 | int mins = int(minst+0.5); | |
103 | int secst = (minst-floor(minst))*60; | |
104 | int secs = int(secst+0.5); | |
105 | string an; | |
106 | sprintf (an, "%s.%s'%s", hrs, mins, secs); | |
107 | return an; | |
108 | } | |
109 | } | |
110 | }; | |
111 | ||
112 | ||
113 | ||
114 | int main(int argc, char *argv[]) | |
115 | { | |
116 | Time t; | |
117 | Angle a(&argv[3]); | |
118 | t.Parse(argv[1]); | |
119 | cout<<"Time is "<<t.GetHours()<<" hours and "<<t.GetMinutes();<<" minutes.\n"; | |
120 | cout<<"Angle is: "<<a.ConvertedAngle(argv[2]);<<"\n"; | |
121 | } |