Questions: clock writing class

topic

To facilitate the later understanding, this code is here to make up the foregoing apart libraries and macro definitions:

#include<cstdio>
#include<iostream>
#include<string>
#include<map>
using namespace std;
#define f(a,b,c,d) for(register int a=b,c=d;a<=c;a++)
#define g(a,b,c,d) for(register int a=b,c=d;a>=c;a--)
typedef int i32;
typedef unsigned int u32;
typedef long long int i64;
typedef unsigned long long int u64;

beginning

Consider only two objects: clock (CLOCK) and the world (WORLD)

Clock properties include: time (Hour), points (Mint), second (SecD)
clock method comprising: increasing (add), reset (set), view (show), is cleared (reset), update (Update)

World properties comprising: (Order) Clock (x), the instruction
method comprising the world: an error is thrown (error_solve), command recognition (take_in)

analysis

Respectively, to achieve clock now with the world:

clock

First, for a clock object, which requires two methods, a string-to-digital, a digital to String

We construct two new ways: to-digital (to_Num) and turn string (to_s)

The method is preferably implemented to digital, the input string is stored in the string class, reads the optimum manner similar to calculate a single number. Just remember plus Laid sentence: a plurality of negative beginning can occur, but not an intermediate; and other determination can not be converted into a digital special judgment, a simple implementation can be:

inline bool to_Num(string s,i32 &d_Num){
    d_Num=0;
    bool b_Neg=0,b_Start=0;
    f(i,0,I,s.size()-1){
        if(s[i]=='-'&&b_Start==0) b_Neg^=1;
        else if(s[i]>='0'&&s[i]<='9'){
            d_Num=d_Num*10+s[i]-48;
            b_Start=1;
        }
        else return 0;
    }
    if(b_Neg) d_Num=-d_Num;
    return 1;
}

The method of taking into account a certain string rotation is converted into a two-digit string, so it is relatively easy to write:

inline string to_s(i32 d_Num){
    string s="";
    if(d_Num/10) s+=d_Num/10+'0';
    s+=d_Num%10+'0';
    return s;
}

Then one by one to achieve the above five methods:

A method of increasing

The input character string converted into digital transferred directly into seconds and added to the original time, considering the characteristics of the clock, the need for the number of seconds, minutes, hours successively carry processing. Taking into account the users will enter the "minute and second" or "hour: minute: second" format, then turn increases the number of seconds (to_Sec) method, converted into the class where the number of seconds remaining the same treatment. I remember all the extraordinary circumstances outside the normal special judge failed to increase.

inline void add(i64 d_Time){
    d_Secd+=d_Time%60;
    if(d_Secd>=60) d_Secd-=60,d_Mint++;
    d_Time/=60;

    d_Mint+=d_Time%60;
    if(d_Mint>=60) d_Mint-=60,d_Hour++;
    d_Time/=60;

    d_Hour+=d_Time;
    if(d_Hour>=24) d_Hour-=24;
}
inline bool add(string s){
    i32 d_Time;
    if(s.find(" ")!=string::npos) { if( !to_Sec(s,d_Time) ) return 0; }
    else { if( !to_Num(s,d_Time) ) return 0; }
    d_Time%=86400;
    if(d_Time<0) d_Time+=86400;
    add(d_Time);
    return 1;
}

The method preferably also the rotation number of seconds to achieve, when sequentially determining whether configuration, minutes, seconds three digits, then the number of seconds into a return feed rate can be:

inline bool to_Sec(string ss,i32 &d_Time){
    f(i,0,I,ss.size()-1) if(ss[i]==':') ss[i]=' ';

    i32 h,m,s;
    string tmp;
    if(ss.find(" ")==string::npos) return 0;
    tmp=ss.substr( ss.find(" ")+1 );
    ss=ss.substr(0, ss.find(" ") );
    if( !to_Num(ss,h) ) return 0;
    
    ss=tmp;
    if(ss.find(" ")==string::npos) return 0;
    tmp=ss.substr( ss.find(" ")+1 );
    ss=ss.substr(0, ss.find(" ") );
    if( !to_Num(ss,m) ) return 0;

    ss=tmp;
    if(ss.find(" ")!=string::npos) return 0;
    if( !to_Num(ss,s) ) return 0;

    d_Time+=3600*h+60*m+s;
    return 1;
}

Reset Method

Reads "minutes and seconds" or "hours: minutes: seconds" in the form of time, if the hours, minutes and seconds to any of the non-compliant, you can judge wrong. Of course, the transfer can not be directly wrong ruling.

inline void set(i32 h,i32 m,i32 s) { *this=CLOCK(h,m,s); }
inline bool set(string ss){
    f(i,0,I,ss.size()-1) if(ss[i]==':') ss[i]=' ';
    
    i32 h,m,s;
    string tmp;
    if(ss.find(" ")==string::npos) return 0;
    tmp=ss.substr( ss.find(" ")+1 );
    ss=ss.substr(0, ss.find(" ") );
    if( !to_Num(ss,h) ) return 0;
    
    ss=tmp;
    if(ss.find(" ")==string::npos) return 0;
    tmp=ss.substr( ss.find(" ")+1 );
    ss=ss.substr(0, ss.find(" ") );
    if( !to_Num(ss,m) ) return 0;

    ss=tmp;
    if(ss.find(" ")!=string::npos) return 0;
    if( !to_Num(ss,s) ) return 0;

    if(h<0||h>=24||m<0||m>=60||s<0||s>=60) return 0;
    set(h,m,s);
    return 1;
}

Check method

Direct call transfer method of the foregoing character string, returning "hour: minute: second" format string means

inline string show(){ return to_s(d_Hour)+":"+to_s(d_Mint)+":"+to_s(d_Secd); }

Clear method

Equivalent reset to "0: 0: 0"

inline void reset() { set(0,0,0); }

Update method

Representing an increase of 1 second

inline void update() { add(1); }

world

Method throws an error

Direct output to

inline void error_solve() { cout<<"Input error!"<<endl; }

Instruction execution method

First determine whether spaces

If there are spaces, can only increase, reset method, or input format is wrong; these two methods for determining whether a corresponding method is successful, unsuccessful needs to throw

If there is no space, can only be viewed, cleared, update method, does not need to determine whether they were successful, can be directly executed

inline void take_in(string order){
    if(order.find(" ")!=string::npos){
        tmp=order.substr( order.find(" ")+1 );
        order=order.substr(0, order.find(" ") );
        if(order=="add") { if( !x.add(tmp) ) error_solve(); }
        else if(order=="set") { if( !x.set(tmp) ) error_solve();}
        else error_solve();
    }
    else{
        if(order=="show") cout<<x.show()<<endl;
        else if(order=="reset") x.reset();
        else if(order=="update") x.update();
        else error_solve();
    }
}

achieve

#include<cstdio>
#include<iostream>
#include<string>
#include<map>
using namespace std;
#define f(a,b,c,d) for(register int a=b,c=d;a<=c;a++)
#define g(a,b,c,d) for(register int a=b,c=d;a>=c;a--)
typedef int i32;
typedef unsigned int u32;
typedef long long int i64;
typedef unsigned long long int u64;

class CLOCK{
    private:
        inline bool to_Num(string s,i32 &d_Num){
            d_Num=0;
            bool b_Neg=0,b_Start=0;
            f(i,0,I,s.size()-1){
                if(s[i]=='-'&&b_Start==0) b_Neg^=1;
                else if(s[i]>='0'&&s[i]<='9'){
                    d_Num=d_Num*10+s[i]-48;
                    b_Start=1;
                }
                else return 0;
            }
            if(b_Neg) d_Num=-d_Num;
            return 1;
        }
        inline string to_s(i32 d_Num){
            string s="";
            if(d_Num/10) s+=d_Num/10+'0';
            s+=d_Num%10+'0';
            return s;
        }
        int d_Hour,d_Mint,d_Secd;
        inline void add(i64 d_Time){
            d_Secd+=d_Time%60;
            if(d_Secd>=60) d_Secd-=60,d_Mint++;
            d_Time/=60;

            d_Mint+=d_Time%60;
            if(d_Mint>=60) d_Mint-=60,d_Hour++;
            d_Time/=60;

            d_Hour+=d_Time;
            if(d_Hour>=24) d_Hour-=24;
        }
        inline void set(i32 h,i32 m,i32 s) { *this=CLOCK(h,m,s); }
        inline bool to_Sec(string ss,i32 &d_Time){
            f(i,0,I,ss.size()-1) if(ss[i]==':') ss[i]=' ';

            i32 h,m,s;
            string tmp;
            if(ss.find(" ")==string::npos) return 0;
            tmp=ss.substr( ss.find(" ")+1 );
            ss=ss.substr(0, ss.find(" ") );
            if( !to_Num(ss,h) ) return 0;
            
            ss=tmp;
            if(ss.find(" ")==string::npos) return 0;
            tmp=ss.substr( ss.find(" ")+1 );
            ss=ss.substr(0, ss.find(" ") );
            if( !to_Num(ss,m) ) return 0;

            ss=tmp;
            if(ss.find(" ")!=string::npos) return 0;
            if( !to_Num(ss,s) ) return 0;

            d_Time+=3600*h+60*m+s;
            return 1;
        }
    public:
        CLOCK(i32 d_Hour_=0,i32 d_Mint_=0,i32 d_Secd_=0):d_Hour(d_Hour_),d_Mint(d_Mint_),d_Secd(d_Secd_) {}
        ~CLOCK() {}
        inline bool add(string s){
            i32 d_Time;
            if(s.find(" ")!=string::npos) { if( !to_Sec(s,d_Time) ) return 0; }
            else { if( !to_Num(s,d_Time) ) return 0; }
            d_Time%=86400;
            if(d_Time<0) d_Time+=86400;
            add(d_Time);
            return 1;
        }
        inline bool set(string ss){
            f(i,0,I,ss.size()-1) if(ss[i]==':') ss[i]=' ';
            
            i32 h,m,s;
            string tmp;
            if(ss.find(" ")==string::npos) return 0;
            tmp=ss.substr( ss.find(" ")+1 );
            ss=ss.substr(0, ss.find(" ") );
            if( !to_Num(ss,h) ) return 0;
            
            ss=tmp;
            if(ss.find(" ")==string::npos) return 0;
            tmp=ss.substr( ss.find(" ")+1 );
            ss=ss.substr(0, ss.find(" ") );
            if( !to_Num(ss,m) ) return 0;

            ss=tmp;
            if(ss.find(" ")!=string::npos) return 0;
            if( !to_Num(ss,s) ) return 0;

            if(h<0||h>=24||m<0||m>=60||s<0||s>=60) return 0;
            set(h,m,s);
            return 1;
        }
        inline string show(){ return to_s(d_Hour)+":"+to_s(d_Mint)+":"+to_s(d_Secd); }
        inline void reset() { set(0,0,0); }
        inline void update() { add(1); }
};

class WORLD{
    private:
        CLOCK x;
        string tmp;
        inline void error_solve() { cout<<"Input error!"<<endl; }
    public:
        WORLD() {}
        ~WORLD() {}
        inline void take_in(string order){
            if(order.find(" ")!=string::npos){
                tmp=order.substr( order.find(" ")+1 );
                order=order.substr(0, order.find(" ") );
                if(order=="add") { if( !x.add(tmp) ) error_solve(); }
                else if(order=="set") { if( !x.set(tmp) ) error_solve();}
                else error_solve();
            }
            else{
                if(order=="show") cout<<x.show()<<endl;
                else if(order=="reset") x.reset();
                else if(order=="update") x.update();
                else error_solve();
            }
        }
}w;

int main(){
    string order;
    while( getline(cin,order) ) w.take_in(order);
    return 0;
}

Guess you like

Origin www.cnblogs.com/JustinRochester/p/12236169.html