Advertisement
serikov

Untitled

Jun 23rd, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. // .a.out 23:25 dms q as demo input
  2.  
  3. #include <boost/regex.hpp>
  4. #include <cstring>
  5. #include <iostream>
  6. using namespace std;
  7.  
  8. class Time {
  9.   public:
  10.  
  11.     bool Parse(string time) {
  12.                cout<<time<<"\n";
  13.                hours = 0;
  14.                minutes = 0;
  15.                boost::regex xRegEx1("([0-2][0-9]):([0-5][0-9])\\s([AP]M)");
  16.                boost::smatch xResults;
  17.                if (boost::regex_match(time, xResults, xRegEx1)) {
  18.                     cout<<"xResults[0] is "<<xResults[0]<<"\n";
  19.                     cout<<"xResults[1] is "<<xResults[1]<<"\n";
  20.                     cout<<"xResults[2] is "<<xResults[2]<<"\n";
  21.                     cout<<"xResults[3] is "<<xResults[3]<<"\n";
  22.                    hours = strtol(xResults[1].str().c_str(), NULL, 10);
  23.                    minutes = strtol(xResults[2].str().c_str(), NULL, 10);
  24.                    if (strcmp(xResults[3].str().c_str(), "PM")==0)  hours = hours + 12;
  25.                    return true;
  26.                } else {
  27.                 cout<<"Incorrect time format or bad regular expression\n";
  28.                 return false;
  29.                 }
  30. }
  31.  
  32.     int GetHours () {
  33.         return hours;
  34.     }  
  35.  
  36.     int GetMinutes() {
  37.         return minutes;
  38.     }  
  39.    
  40.     private:
  41.      int hours;
  42.      int minutes;
  43. };
  44.  
  45.  
  46. class AnalogClock {
  47.    public:
  48.       AnalogClock(Time* atime) {
  49.       t = *atime;
  50.       }
  51.  
  52.       float GetAngle() {
  53.          angle = 0;
  54.          int hours = t.GetHours();
  55.          if (hours>12) hours = hours - 12;
  56.          int minutes = t.GetMinutes();
  57.          if (minutes==0) angle = abs(hours*30-minutes*6);
  58.          else angle = abs((hours+1/minutes)*30-minutes*6);
  59.          if (angle>180) angle=abs(180-angle);
  60.          return angle;
  61.         }
  62.  
  63.     private:
  64.         float angle;
  65.         Time t;
  66. };
  67.  
  68.  
  69. class QuartzClock {
  70.    public:
  71.       QuartzClock(Time* qtime) {
  72.       t = *qtime;
  73.     }
  74.  
  75.       float GetAngle() {
  76.         angle = 0;
  77.         int hours = t.GetHours();
  78.         if (hours>12) hours = hours - 12;
  79.         int minutes = t.GetMinutes();
  80.         angle = abs(hours*30-minutes*6); if (angle>180) angle=abs(180-angle);
  81.         return angle;
  82.         }
  83.  
  84.     private:
  85.         float angle;
  86.         Time t;
  87. };
  88.  
  89.  
  90. string convert_angle(float angle, const char* tformat) {
  91.         char angle1 [50];
  92.         if (strcmp(tformat,"deg")) {
  93.             sprintf (angle1, "%f", angle);
  94.             }
  95.         if (strcmp(tformat,"rad")) {
  96.             angle=angle*(M_PI)/180;
  97.             sprintf (angle1, "%f", angle);
  98.             }
  99.         if (strcmp(tformat,"dms")) {
  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.             sprintf (angle1, "%d.%d'%d\"", hrs, mins, secs);           
  106.         }
  107.     return string(angle1);
  108.     }
  109.  
  110.  
  111. int main(int argc, char *argv[])
  112. {
  113. cout<<"argv[1] is "<<argv[1]<<"\n";
  114. cout<<"argv[2] is "<<argv[2]<<"\n";
  115. cout<<"argv[3] is "<<argv[3]<<"\n";
  116. Time t;
  117. cout<<t.Parse(argv[1])<<"\n";
  118. float angle;
  119. if (strcmp(argv[3],"q")!=0)  {
  120.     QuartzClock c(&t);
  121.     angle = c.GetAngle();
  122.     }
  123. if (strcmp(argv[3],"a")!=0)  {
  124.     AnalogClock c(&t);
  125.     angle = c.GetAngle();
  126.     }
  127. string angle1 = convert_angle(angle, argv[2]);
  128. cout<<"Time is "<<t.GetHours()<<" hours and "<<t.GetMinutes()<<" minutes.\n";
  129. cout<<"Natural angle is: "<<angle<<"\n";
  130. cout<<"Converted angle is: "<<angle1<<"\n";
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement