Overloaded operators - calculated with high accuracy + - *

#include <cstdio> 
#include < String > 
#include <the iostream> 
#include <algorithm> 
#include <CString>
 #define max (A, B) ((A)> (B)? (A) :( B))
 the using  namespace STD;
 struct the Data 
{ 
    int a [ 100001 ], len; // each digit storage array length and 
    
    the Data () // initial value 
    { 
        Memset (a, 0 , the sizeof (a)); 
        len = . 1 ; 
    } 
    
    void the read () // read function
    {
         Char D [ 100001 ]; 
        Scanf ( " % S " , D);
         int L = strlen (D);
         for ( int I = . 1 ; I <= L; I ++ ) 
            A [I] = D [Li] - ' 0 ' ; // reverse storage, easy carry 
        len = L; // record length 
    } 
    
    void Write () // output function 
    {
         for ( int I = len; I> = . 1 ; i-- ) 
            the printf ( "D% " , A [I]); // reverse output 
    } 
    
    void Clean () // remove leading zeros 
    {
         the while (len> . 1 && A [len]) len--! ; 
    } 
    
    Friend BOOL  operator <( const the Data & A , const the Data & B) // define the outer, outside do not add friend; friend without internal, not two parameters 
    { // const prevent you change values, do not forget to take the address & 
        IF (A.len> B.len) return  0 ; // first length ratio 
        IF (A.len <B.len) return  . 1 ;
         for (int I = . 1 ; I <= A.len; I ++) // then determined from the most significant bit 
        {
          IF (Aa [I]> of Ba [I]) return  0 ;
          IF (Aa [I] <of Ba [I]) return  . 1 ; 
        } 
        return  0 ; // if the same return to zero 
    } 
    
    Friend BOOL  operator <= ( const the Data A &, const the Data & B) 
    { 
        IF (A.len> B.len) return  0 ; 
         IF (A.len < B.len) return  . 1 ;
        for(int i=1;i<=A.len;i++) 
        {
         if(A.a[i]>B.a[i]) return 0;
         if(A.a[i]<B.a[i]) return 1;
        }
        return 1; //如果一样返回 1
    }
    
    friend bool operator > (const Data & A,const Data & B)
    {
        if(A.len>B.len) return 1; 
        if(A.len<B.len) return 0;
        for(int i=1;i<=A.len;i++) 
        {
         if(A.a[i]>B.a[i]) return 1;
         if(A.a[i]<B.a[i]) return 0;
        }
        return 0;
    }
    
    friend bool operator >= (const Data & A,const Data &B)
    {
        if(A.len>B.len) return 1; 
        if(A.len<B.len) return 0;
        for(int i=1;i<=A.len;i++) 
        {
         if(A.a[i]>B.a[i]) return 1;
         if(A.a[i]<B.a[i]) return 0;
        }
        return 1;
    }
    friend bool operator == (const Data & A,const Data &B)
    {
        if(A.len>B.len) return 0; 
        if(A.len<B.len) return 0;
        for(int i=1; I <= A.len; I ++ ) 
        { 
         IF (Aa [I]> of Ba [I]) return  0 ;
          IF (Aa [I] <of Ba [I]) return  0 ; 
        } 
        return  . 1 ; // equal length, is equal to the corresponding bits are equal 
    } 

    Friend the Data operator + ( const the Data A &, const the Data & B) 
    { 
        the Data C; // as A, B can not change the value, the introduction of C, C 1 length, the value 0 
        C. max = len (B.len, A.len); // select a, B is the maximum length 
        for ( int I = . 1 ; I <= C.len; I ++)// boundary Note 
        { 
            of Ca [I] + = Aa [I] of Ba + [I]; // corresponding bit adding 
            IF (of Ca [I]> = 10 ) // per decade a 
            { 
                of Ca [I] - = 10 ; // Since no more than 10, so with - instead of 10 = 10% higher efficiency 
                of Ca [I + . 1 ] ++; // carry 
            } 
        } 
        iF (of Ca [C.len + . 1 ]) // consider whether proceeds to the next B.len 
            C.len ++ ; 
        C.clean (); // to 0, it does not matter even if the length of the large 
        return C; 
    }
    
    the Data Friend operator - ( const the Data A &, const the Data & B) 
    { 
        the Data C; // as A, B can not change the value, the introduction of C, C 1 length, the value 0 
        IF (A> B) 
        { 
            C. len = A.len;
             for ( int I = . 1 ; I <= A.len; I ++ ) 
                {      
                    of Ca [I] + = Aa [I] - of Ba [I];
                     IF (of Ca [I] < 0 ) // borrow 
                    {of Ca [I + . 1 ] -; of Ca [I] = + 10;} 
                } 
            IF (of Ca [C.len + . 1 ]) C.len ++ ; 
            C.clean (); 
            return C; 
        } 
        the else  IF (A == B) // prevent the occurrence of 1-1 = -0 
        {
             return C ; // C is the initial length of 1, a value of 0 
        }
         the else  IF (a < B) 
        { 
            COUT << ' - ' ; 
            C.len = B.len;
             for ( int I = 1 ; I <= B.len ; i ++ ) 
            {
                C.a[i]+=B.a[i]-A.a[i];
                if(C.a[i]<0) 
                {C.a[i+1]--;C.a[i]+=10;} 
            }
            if(C.a[C.len+1]) C.len++;
            C.clean();
            return C;
        }
    }
    
    friend Data operator * (const Data &A ,const Data &B)
    {
        Data C;
        C.len=A.len+B.len;//The maximum length C of A.len + B.len, sometimes B.len-+. 1 A.len
        for ( int I = . 1 ; I <= A.len; I ++ )
             for ( int J = . 1 ; J <= B. len; J ++ ) 
                { 
                    of Ca [I + J- . 1 ] = Aa + [I] of Ba * [J]; // multiplication result is applied to the J-+. 1 I 
                    of Ca [I + J] of Ca + = [I + J- . 1 ] / 10 ; 
                    of Ca [I + J- . 1 ]% = 10 ; 
                } 
        IF (of Ca [C.len + . 1 ]) 
           C.len ++ ;
        C.clean();
        return C;  
    }
    
};

Data A,B,C;
int main()
{
    A.read();
    B.read();
    C = A+B;
    //C=A-B;
    //C=A*B;
    C.write();
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/wzy1744315462/p/12482948.html