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:
- bool Parse(string time) {
- cout<<time<<"\n";
- 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)) {
- cout<<"xResults[0] is "<<xResults[0]<<"\n";
- cout<<"xResults[1] is "<<xResults[1]<<"\n";
- cout<<"xResults[2] is "<<xResults[2]<<"\n";
- cout<<"xResults[3] is "<<xResults[3]<<"\n";
- 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 {
- cout<<"Incorrect time format or bad regular expression\n";
- return false;
- }
- }
- int GetHours () {
- return hours;
- }
- int GetMinutes() {
- return minutes;
- }
- 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;
- };
- string convert_angle(float angle, const char* tformat) {
- char angle1 [50];
- if (strcmp(tformat,"deg")) {
- sprintf (angle1, "%f", angle);
- }
- if (strcmp(tformat,"rad")) {
- angle=angle*(M_PI)/180;
- sprintf (angle1, "%f", angle);
- }
- if (strcmp(tformat,"dms")) {
- 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 string(angle1);
- }
- int main(int argc, char *argv[])
- {
- cout<<"argv[1] is "<<argv[1]<<"\n";
- cout<<"argv[2] is "<<argv[2]<<"\n";
- cout<<"argv[3] is "<<argv[3]<<"\n";
- Time t;
- cout<<t.Parse(argv[1])<<"\n";
- 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();
- }
- string 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