大整数运算

前两天做了北航上的一道大整数数,顺便复习一下大整数的运算~

http://www.bianchengla.com/oj/1/practise/problem?id=1001

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <string>
#include <algorithm>

using namespace std ;

int f ;

class node
{
private:
    static int COMPARE( string a , string b )
    {
        int i , j ;
        int len1 = a.size() ;
        int len2 = b.size() ;

        if ( a.size() == 0 ) a = "0" ;
        if ( b.size() == 0 ) b = "0" ;

        i = 0 ;
        j = 0 ;
        for ( i = 0 ; i < len1 ; i++ )
        {
            if ( a[i] == '0' ) ++j ;
            else break;
        }
        a = a.substr( j ) ;

        i= 0 ;
        j = 0 ;
        for ( i = 0 ; i < len2 ; i++ )
        {
            if ( b[i] == '0' ) ++j ;
            else break;
        }
        b = b.substr( j ) ;

        len1 = a.size() ;
        len2 = b.size() ;

        if ( len1 < len2 )
        return -1 ;
        else if ( len1 > len2 )
        return 1 ;
        else if ( len1 == len2 )
        {
            if ( a.compare ( b ) > 0 )
            return 1 ;
            else if ( a.compare ( b) == 0 )
            return 0 ;
            else
            return -1;
        }
        return 0 ;
    }
public:
    static string PLUS( string a , string b )
    {
            int i ;
            int len1 = a.size();
            int len2 = b.size();
            string result = "" ;

            reverse ( a.begin() , a.end());
            reverse ( b.begin() , b.end());

            for ( i = 0 ; i < len1 && i < len2 ; i++ )
            {
                char tem = ( char )( ( a[i] + b[i] ) - '0' );
                result += tem ;
            }
            while ( i < len1 )
            {
                result += ( char )a[i] ;
                i++ ;
            }
            while ( i < len2 )
            {
                result += ( char )b[i] ;
                i++ ;
            }

            int value = 0 ;
            for ( i = 0 ; i < ( int )result.size() ; i++ )
            {
                value = value + ( result[i] - '0' ) ;
                result[i] = ( char )( ( value % 10 ) + '0' ) ;
                value = value / 10 ;
            }
            if ( value != 0 )
            result += ( char )( value + '0' ) ;

            for ( i = result.size() - 1 ; i >= 0 ; i-- )
            {
                if ( result[i] != '0' )
                break;
            }
            result = result.substr( 0 , i + 1 );
            reverse ( result.begin() , result.end()) ;
            if ( result.length() == 0 ) return "0" ;
            return result ;
    }

    static string MINUS( string a , string b )
    {
        int i ;
        int len1 = a.size() ;
        int len2 = b.size() ;
        string result = "" ;

        if ( COMPARE( a , b ) < 0 )
        {
            return "-" + MINUS( b , a ) ;
        }

        reverse ( a.begin() , a.end()) ;
        reverse ( b.begin() , b.end()) ;

        for ( i = 0 ; i < len1 && i < len2 ; i++ )
        {
            char tem = ( char )( ( a[i] - b[i] ) + '0' );
            result += tem ;
        }
        while ( i < len1 )
        {
            result += ( char )a[i] ;
            i++ ;
        }

        int value = 0 ;
        int tem = 0 ;
        for ( i = 0 ; i < ( int )result.size() ; i++ )
        {
            value = ( result[i] - '0' ) + tem ;
            if ( value < 0 )
            {
                value += 10 ;
                tem = -1 ;
            }
            else
            tem = 0 ;
            result[i] = ( char )( value + '0' ) ;
        }

        for ( i = result.size() -1 ; i >= 0 ; i-- )
        {
            if ( result[i] != '0' ) break;
        }
        result = result.substr ( 0 , i + 1 );
        reverse ( result.begin() , result.end());
        if ( result.length() == 0 ) return "0" ;
        return result ;
    }

    static string MULTIPLY( string a , string b )
    {
        int i , j ;
        int len1 = a.size() ;
        int len2 = b.size() ;
        string result = "" ;

        reverse ( a.begin() , a.end()) ;
        reverse ( b.begin() , b.end()) ;

        int *it = ( int *)malloc( sizeof( int ) * ( len1 + len2 + 1)) ;
        memset( it , 0 , sizeof( int ) * ( len1 + len2 + 1 ));

        for ( i = 0 ; i < len1 ; i++ )
        {
            for ( j = 0 ; j < len2 ; j++ )
            {
                it[i+j] += ( a[i] - '0' ) * ( b[j] - '0' );
            }
        }
        int value = 0 ;
        for ( i = 0 ; i < len1 + len2 ; i++ )
        {
            value += it[i] ;
            it[i] = value % 10 ;
            value /= 10 ;
        }
        for ( i = len1 + len2 -1 ; i >= 0 ; i-- )
        {
            if ( it[i] != 0 ) break;
        }
        for ( ; i >= 0 ; i-- )
        {
            result += ( char )( it[i] + '0' ) ;
        }

        free ( it ) ;

        if ( result == "" ) return "0" ;
        return result ;
    }

    static string DIVIDE( string a , string b )
    {
        int i ;
        int len1 = a.size() ;
        int len2 = b.size() ;
        string result = "" ;

        if ( COMPARE( a , b ) < 0 && f == 0 )
        {
            return "0" ;
        }

        string tem =  a.substr ( 0 , len2 ) ;
        int pos = len2 - 1 ;

        while ( pos < len1 )
        {
            int res = 0 ;
            while ( COMPARE( tem , b ) >= 0 )
            {
                res++ ;
                tem = MINUS( tem , b ) ;
            }

            result += ( char )( res + '0' ) ;

            pos++ ;

            if ( pos < len1 )
            {
                tem += a[pos] ;
            }
        }

        if ( f > 0 )
        {
           result += "." ;
           string st = "1" ;

           for ( i = 1 ; i <= f ; i++ )
           {
               st += "0" ;
               if ( COMPARE( MULTIPLY( MINUS( a ,  MULTIPLY( DIVIDE( a, b ) , b )) , st ) , b ) < 0 )
               {
                   result += "0" ;
               }
           }

           string tem =  DIVIDE( MULTIPLY( MINUS( a , MULTIPLY( DIVIDE( a , b ) , b ) ) , st ) , b ) ;
           if ( tem[0] != '0' )
           result += tem ;
        }

        int len = result.size() ;
        for ( i = 0 ; i < len ; i++ )
        if ( result[i] != '0' )
        break ;

        result = result.substr ( i , len ) ;
        return result ;
    }

    static string MOD( string a , string b )
    {
        if ( COMPARE ( a , b ) < 0 )
        return a ;
        else if (COMPARE( a , b ) == 0 )
        return "0" ;
        else
        return MINUS( a , MULTIPLY( DIVIDE( a , b ) , b ));
    }
};

int main()
{
    string a , b , result1 , result2  ;

    while ( cin>>a>>b )
    {
        if ( a[0] == '0' && b[0] == '0' && a.size() == 1 && b.size() == 1 )
        break;
        result1 = node::MULTIPLY( a , b );
        while ( b.size() != 1 || b[0] != '0')
        {
            result2 = node::MOD( a , b );
            a = b.substr( 0 , b.size());
            b = result2.substr(0 , result2.size());
        }
        result1 = node::DIVIDE( result1 , a );
        cout<<result1<<endl;
    }
    return 0;
}
posted @ 2012-07-14 10:58  Misty_1  阅读(165)  评论(0编辑  收藏  举报