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* t) { // передаем указатель на экземпляр класса время
- time = t;
- }
- float GetAngle() {
- angle = 0;
- int hours = time->GetHours();
- if (hours>12) hours = hours - 12;
- int minutes = time->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;
- };
- class QuartzClock {
- public:
- QuartzClock(Time* t) {
- time = t;
- float angle = 0;
- }
- float GetAngle() {
- int hours = time->GetHours();
- if (hours>12) hours = hours - 12;
- int minutes = time->GetMinutes();
- angle = abs(hours*30-minutes*6); if (angle>180) angle=abs(180-angle);
- return angle;
- }
- };
- class Angle {
- public:
- Angle(const char* param3) {
- if ((strcmp(param3,"q")!=0) Angle(QuartzClock *c);
- if ((strcmp(param3,"a")!=0) Angle(AnalogClock *c);
- clock = c;
- float angle = clock->GetAngle();
- string angle1;
- }
- char ConvertedAngle(const char* tformat) {
- if ((strcmp(tformat,"deg")!=0) {
- angle1=FloatToStr(angle);
- return angle1;
- }
- if ((strcmp(tformat,"rad")!=0) {
- angle=angle*(M_PI)/180;
- angle1=FloatToStr(angle);
- return angle1;
- }
- 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);
- string an;
- sprintf (an, "%s.%s'%s", hrs, mins, secs);
- return an;
- }
- }
- };
- int main(int argc, char *argv[])
- {
- Time t;
- Angle a(&argv[3]);
- t.Parse(argv[1]);
- cout<<"Time is "<<t.GetHours()<<" hours and "<<t.GetMinutes();<<" minutes.\n";
- cout<<"Angle is: "<<a.ConvertedAngle(argv[2]);<<"\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement