Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "INTL.h"
- using namespace intl::operators;
- /*operators**********************************************************/
- void intl::operators::operator << ( const ostream &, INTL &z )
- {
- z.output();
- }
- /********************************************************************/
- void intl::operators::operator >> ( const istream &, INTL &z )
- {
- z.input();
- }
- /********************************************************************/
- intl::INTL intl::operators::operator +( const intl::INTL &a, const intl::INTL &b )
- {
- intl::INTL rslt;
- int z = 0;
- rslt.size = max( a.size, b.size );
- for( int i = 0; i < rslt.size | z; i++ )
- {
- rslt.digits[i] = a.digits[i] + b.digits[i] + z;
- if( rslt.digits[i] >= rslt.osn )
- {
- rslt.digits[i] -= rslt.osn;
- z = 1;
- }
- else z = 0;
- }
- if( rslt.digits[rslt.size] )
- rslt.size++;
- return rslt;
- }
- /********************************************************************/
- void intl::operators::operator ++( intl::INTL &a )
- {
- a = a + intl::int2intl(1);
- }
- /********************************************************************/
- void intl::operators::operator +=( intl::INTL &a, const intl::INTL &b )
- {
- a = a + b;
- }
- /********************************************************************/
- void intl::operators::operator --( intl::INTL &a )
- {
- a = a - intl::int2intl(1);
- }
- /********************************************************************/
- void intl::operators::operator -=( intl::INTL &a, const intl::INTL &b )
- {
- a = a - b;
- }
- /********************************************************************/
- intl::INTL sub( const intl::INTL &a, const intl::INTL &b )
- {
- intl::INTL rslt = a;
- int z = 0;
- for( int i = 0; i < rslt.size; i++ )
- {
- rslt.digits[i] -= b.digits[i] + z;
- if( rslt.digits[i] < 0 )
- {
- rslt.digits[i] += rslt.osn;
- rslt.digits[i+1]--;
- }
- }
- int pos = rslt.size;
- while( pos && !rslt.digits[pos] )
- pos--;
- rslt.size = pos + 1;
- return rslt;
- }
- /********************************************************************/
- intl::INTL intl::operators::operator - ( const intl::INTL &a, const intl::INTL &b )
- {
- intl::INTL c;
- if( a < b )
- {
- c = sub( b, a );
- c.negative = true;
- }
- else
- {
- c = sub( a, b );
- c.negative = false;
- }
- return c;
- }
- /********************************************************************/
- intl::INTL intl::operators::operator * ( const intl::INTL &a, const intl::INTL &b )
- {
- intl::INTL rslt;
- for( int i = 0; i < a.size; i++ )
- {
- int z = 0;
- for( int j = 0; j < b.size | z; j++ )
- {
- rslt.digits[i+j] += a.digits[i] * b.digits[j] + z;
- z = rslt.digits[i+j] / rslt.osn;
- rslt.digits[i+j] -= z * rslt.osn;
- }
- }
- int pos = a.size + b.size;
- while( pos > 0 && !rslt.digits[pos] )
- pos--;
- rslt.size = pos + 1;
- return rslt;
- }
- /********************************************************************/
- void intl::operators::operator *= ( intl::INTL &a, const intl::INTL &b )
- {
- a = a * b;
- }
- /********************************************************************/
- void next( intl::INTL &z )
- {
- for( int i = z.size; i >= 1; i-- )
- z.digits[i] = z.digits[i-1];
- if( z.digits[z.size] )
- z.size++;
- }
- /********************************************************************/
- intl::INTL intl::operators::operator / (const intl::INTL &a, const intl::INTL &b)
- {
- intl::INTL rslt;
- intl::INTL tmp;
- for( int i = a.size - 1; i >= 0; i-- )
- {
- next( tmp );
- tmp.digits[0] = a.digits[i];
- int x = 0;
- int y = 0, z = a.osn;
- while (y <= z)
- {
- int m = (y + z) >> 1;
- intl::INTL cur = b * m;
- if( cur <= tmp )
- {
- x = m;
- y = m + 1;
- }
- else
- z = m - 1;
- }
- rslt.digits[i] = x;
- tmp = tmp - b * x;
- }
- int pos = a.size;
- while( pos >= 0 && !rslt.digits[pos] )
- pos--;
- rslt.size = pos + 1;
- return rslt;
- }
- /********************************************************************/
- void intl::operators::operator /= ( intl::INTL &a, const intl::INTL &b )
- {
- a = a / b;
- }
- /********************************************************************/
- intl::INTL intl::operators::operator % ( const intl::INTL &a, const intl::INTL &b )
- {
- intl::INTL rslt;
- intl::INTL temp;
- for( int i = a.size - 1; i >= 0; i-- )
- {
- next( temp );
- temp.digits[0] = a.digits[i];
- int x = 0;
- int y = 0, z = a.osn;
- while( y <= z )
- {
- int m = (y + z) >> 1;
- intl::INTL cur = b * m;
- if( cur <= temp )
- {
- x = m;
- y = m + 1;
- }
- else
- z = m - 1;
- }
- rslt.digits[i] = x;
- temp = temp - b * x;
- }
- return temp;
- }
- /********************************************************************/
- void intl::operators::operator %= ( intl::INTL &a, const intl::INTL &b )
- {
- a = a % b;
- }
- /********************************************************************/
- bool intl::operators::operator < ( const intl::INTL &a, const intl::INTL &b )
- {
- if( a.size != b.size )
- return a.size < b.size;
- for( int i = a.size - 1; i >= 0; i-- )
- {
- if( a.digits[i] != b.digits[i] )
- return a.digits[i] < b.digits[i];
- }
- return false;
- }
- /********************************************************************/
- bool intl::operators::operator > ( const intl::INTL &a, const intl::INTL &b )
- {
- if( a.size != b.size )
- return a.size > b.size;
- for( int i = a.size - 1; i >= 0; i-- )
- {
- if( a.digits[i] != b.digits[i] )
- return a.digits[i] > b.digits[i];
- }
- return false;
- }
- /********************************************************************/
- bool intl::operators::operator <= ( const intl::INTL &a, const intl::INTL &b )
- {
- if( a.size != b.size )
- return a.size <= b.size;
- for( int i = a.size - 1; i >= 0; i-- )
- if( a.digits[i] != b.digits[i] )
- return a.digits[i] <= b.digits[i];
- return true;
- }
- /********************************************************************/
- bool intl::operators::operator >= ( const intl::INTL &a, const intl::INTL &b )
- {
- if( a.size != b.size )
- return a.size >= b.size;
- for( int i = a.size - 1; i >= 0; i-- )
- if( a.digits[i] != b.digits[i] )
- return a.digits[i] >= b.digits[i];
- return true;
- }
- /********************************************************************/
- bool intl::operators::operator == ( const intl::INTL &a, const intl::INTL &b )
- {
- if( a.size != b.size )
- return false;
- for( int i = 0; i < a.size; i++ )
- {
- if( a.digits[i] != b.digits[i] )
- return false;
- }
- return true;
- }
- /********************************************************************/
- intl::INTL intl::pow( const intl::INTL &a, const int &N )
- {
- intl::INTL rslt( intl::int2str(1) );
- intl::INTL tmp = a;
- int z = N;
- while( z )
- {
- if( z & 1 )
- rslt = rslt * tmp;
- tmp = tmp * tmp;
- z >>= 1;
- }
- return rslt;
- }
- /********************************************************************/
- intl::INTL intl::sqrt( intl::INTL rslt )
- {
- }
- /********************************************************************/
- intl::INTL intl::factorial( intl::INTL n )
- {
- intl::INTL rslt(1);
- try
- {
- for( intl::INTL i(2); i <= n; ++i )
- rslt = rslt * i;
- }
- catch( exception &e )
- {
- rslt.setmemory( e.what() );
- }
- return rslt;
- }
- /********************************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement