Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // сompile string g++ ya.cpp -lboost_regex
- // run example on linux: ./a.out "23:25" dms q as demo input
- // for run unit test please compile with --test key (argv[4])
- #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-1][0-9]):([0-5][0-9])\\s([AP]M)");
- boost::regex xRegEx2("([0-2][0-9]):([0-5][0-9])");
- boost::smatch xResults;
- if ((boost::regex_match(time, xResults, xRegEx1))||(boost::regex_match(time, xResults, xRegEx2))) {
- //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")==0) {
- sprintf (angle1, "%f", angle);
- }
- else if (strcmp(tformat,"rad")==0) {
- angle=angle*(M_PI)/180;
- sprintf (angle1, "%f", angle);
- }
- else if (strcmp(tformat,"dms")==0) {
- int hrs = int(angle+0.5);
- //cout<<"hrs is: "<<hrs<<"\n";
- //cout<<"floor(angle): "<<floor(angle)<<"\n";
- int minst = (angle-floor(angle))*60;
- //cout<<"minst is: "<<minst<<"\n";
- int mins = int(minst+0.5);
- //cout<<"mins is: "<<mins<<"\n";
- int secst = (minst-floor(minst))*60;
- //cout<<"secst is: "<<secst<<"\n";
- int secs = int(secst+0.5);
- //cout<<"secs is: "<<secs<<"\n";
- sprintf (angle1, "%d.%d'%d\"", hrs, mins, secs);
- }
- else {
- cout<<"Angle format is incorrect. Exit\n";
- }
- return string(angle1);
- }
- ////////////////// start of unit test functions
- void TestTimeParse() {
- Time time;
- assert(time.Parse("10:30 AM"));
- assert(time.GetHours() == 10);
- assert(time.GetMinutes() == 30);
- assert(time.Parse("09:45 PM"));
- assert(time.GetHours() == 21);
- assert(time.GetMinutes() == 45);
- assert(time.Parse("23:32"));
- assert(time.GetHours() == 23);
- assert(time.GetMinutes() == 32);
- }
- void AnalogClockTest() {
- Time atime;
- atime.Parse("10:30 AM");
- AnalogClock(Time* atime);
- assert(atime.GetAngle() == 135);
- atime.Parse("22:22");
- AnalogClock(Time* atime);
- assert(atime.GetAngle() == 179);
- }
- void QuartzClockTest() {
- Time atime;
- atime.Parse("10:30 AM");
- QuartzClock(Time* atime);
- assert(atime.GetAngle() == 120);
- atime.Parse("22:22");
- QuartzClock(Time* atime);
- assert(atime.GetAngle() == 168);
- }
- void AngleConvertTest() {
- assert(convert_angle(179, "deg")== 179);
- assert(convert_angle(168., "deg")== 179);
- assert(convert_angle(168, "rad")== 2.9326); // точность?
- assert(convert_angle(168, "dms")== "168.0'0\"");
- }
- ///////////////////////// end of unit test functions
- int main(int argc, char *argv[])
- {
- ///////////////////// unit tests
- if (strcmp(argv[4],"--test")==0) {
- TestTimeParse();
- AnalogClockTest();
- QuartzClockTest();
- AngleConvertTest();
- }
- ///////////////////// end of unit tests
- cout<<"argv[1] is "<<argv[1]<<"\n";
- cout<<"argv[2] is "<<argv[2]<<"\n";
- cout<<"argv[3] is "<<argv[3]<<"\n";
- Time t;
- t.Parse(argv[1]);
- //cout<<t.Parse(argv[1])<<"\n";
- float angle;
- if (!argv[3]) {
- cout<<"You are not specify clock format. By default, it will be quartz\n";
- QuartzClock c(&t);
- angle = c.GetAngle();
- }
- else {
- if (strcmp(argv[3],"q")==0) {
- QuartzClock c(&t);
- angle = c.GetAngle();
- }
- else if (strcmp(argv[3],"a")==0) {
- AnalogClock c(&t);
- angle = c.GetAngle();
- }
- else {
- cout<<"Clock format is incorrect. By default, it will be quartz\n";
- QuartzClock c(&t);
- angle = c.GetAngle();
- }
- }
- cout<<"Natural angle is: "<<angle<<"\n";
- string angle1 = convert_angle(angle, argv[2]);
- //cout<<"Time is "<<t.GetHours()<<" hours and "<<t.GetMinutes()<<" minutes.\n";
- cout<<"Converted angle is: "<<angle1<<"\n";
- }
- // нужна об
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement