Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // .a.out 23:25 dms q as demo input
- #include <boost/regex.hpp>
- #include <cstring>
- #include <iostream>
- using namespace std;
- class Time {
- public:
- Time();
- bool Parse(string time) {
- hours = 0;
- minutes = 0;
- boost::regex xRegEx1("^([0-2][0-9])([0-5][0-9])(\\s[AP]M)");
- boost::smatch xResults;
- if (!boost::regex_match(time, xResults, xRegEx1)) {
- hours = strtol(xResults[1].str().c_str(), NULL, 10);
- minutes = strtol(xResults[2].str().c_str(), NULL, 10);
- if (strcmp(xResults[3].str().c_str(), "PM")==0) hours = hours + 12;
- return true;
- } else return false;
- }
- int GetHours () {
- return hours;
- }
- int GetMinutes() {
- return hours;
- }
- private:
- int hours;
- int minutes;
- };
- class AnalogClock {
- public:
- AnalogClock(Time* atime) {
- t = *atime;
- }
- float GetAngle() {
- angle = 0;
- int hours = t.GetHours();
- if (hours>12) hours = hours - 12;
- int minutes = t.GetMinutes();
- if (minutes==0) angle = abs(hours*30-minutes*6);
- else angle = abs((hours+1/minutes)*30-minutes*6);
- if (angle>180) angle=abs(180-angle);
- return angle;
- }
- private:
- float angle;
- Time t;
- };
- class QuartzClock {
- public:
- QuartzClock(Time* qtime) {
- t = *qtime;
- }
- float GetAngle() {
- angle = 0;
- int hours = t.GetHours();
- if (hours>12) hours = hours - 12;
- int minutes = t.GetMinutes();
- angle = abs(hours*30-minutes*6); if (angle>180) angle=abs(180-angle);
- return angle;
- }
- private:
- float angle;
- Time t;
- };
- char convert_angle(float angle, const char* tformat) {
- char angle1 [50];
- if (strcmp(tformat,"deg")!=0) {
- sprintf (angle1, "%f", angle);
- }
- if (strcmp(tformat,"rad")!=0) {
- angle=angle*(M_PI)/180;
- sprintf (angle1, "%f", angle);
- }
- if (strcmp(tformat,"dms")!=0) {
- int hrs = int(angle+0.5);
- int minst = (angle-floor(angle))*60;
- int mins = int(minst+0.5);
- int secst = (minst-floor(minst))*60;
- int secs = int(secst+0.5);
- sprintf (angle1, "%d.%d'%d\"", hrs, mins, secs);
- }
- return angle1;
- }
- int main(int argc, char *argv[])
- {
- Time t;
- t.Parse(argv[1]);
- float angle;
- if (strcmp(argv[3],"q")!=0) {
- QuartzClock c(&t);
- angle = c.GetAngle();
- }
- if (strcmp(argv[3],"a")!=0) {
- AnalogClock c(&t);
- angle = c.GetAngle();
- }
- char angle1 = convert_angle(angle, argv[2]);
- cout<<"Time is "<<t.GetHours()<<" hours and "<<t.GetMinutes()<<" minutes.\n";
- cout<<"Natural angle is: "<<angle<<"\n";
- cout<<"Converted angle is: "<<angle1<<"\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement