?? bignum.cpp
字號:
/**
* Big integer class, optimized for decimal integers.
* Stores and manipulates integers represented as byte arrays,
* where each byte is a decimal digit. If you're looking for
* robust, bug-free, efficient code, keep looking. This is a quick
* and dirty hack. Some day I'll write a templatized BigInt, where
* you will be able to select the base in which to store the
* number. When that day comes, most of this code will be thrown
* away.
*
* BUGS:
* operator-(int) does not work.
*
* BigInt doesn't play nice with long long. Either use int
* or string.
*
* INVARIANTS:
* - capacity is never smaller than 16
* - capacity is not the smallest it can be because every
* modifying member function first grows digits as much as
* it might ever need and then does its job.
* FIELD TESTING:
* - Passed numerous problems on Valladolid, including
* 107, 288, 324, 424, 465, 485, 495, 560, 619, 623, etc.
*
* COMPATIBILITY:
* - This class was written for the g++ compiler and uses some
* of the g++ extensions (like "long double" and the ">?="
* operator). If you want to compile this under Micro$oft's
* "compiler", I don't want to talk to you.
*
* LAST MODIFIED: October 5, 2005
*
* This file is part of my library of algorithms found here:
* http://www.palmcommander.com:8081/tools/
* LICENSE:
* http://www.palmcommander.com:8081/tools/LICENSE.html
* Copyright (c) 2002-2004
* Contact author:
* igor at cs.ubc.ca
**/
#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <sstream>
#include <math.h>
using namespace std;
#ifndef min
#define min(x,y) ((x) < (y) ? (x) : (y))
#endif
#ifndef max
#define max(x,y) ((x) > (y) ? (x) : (y))
#endif
class BigInt
{
private:
char *digits;
int size; // number of used bytes (digits)
int capacity; // size of digits
int sign; // -1, 0 or +1
public:
/** Creates a BigInt with initial value n and initial capacity cap **/
BigInt( int n, int cap );
/** Creates a BigInt with initial value n **/
BigInt( int n );
/** Creates a BigInt with initial value floor( d ) **/
BigInt( long double d );
/** Creates a BigInt with value 0 **/
BigInt();
/** Creates a BigInt by reading the value from a string **/
BigInt( string s );
/** Creates a BigInt by reading the value from a C string **/
BigInt( const char s[] );
/** Copy constructor **/
BigInt( const BigInt &n );
/** Assignment operators **/
const BigInt &operator=( const BigInt &n );
const BigInt &operator=( int n );
/** Cleans up **/
~BigInt();
/** Removes any leading zeros and adjusts the sign **/
void normalize();
/** Returns the sign of n: -1, 0 or 1 **/
static int sig( int n );
/** Returns the sign of n: -1, 0 or 1 **/
static int sig( long double n );
/** Returns the number of decimal digits **/
inline int length() { return size; }
/** Arithmetic **/
BigInt operator++();
BigInt operator++( int );
BigInt operator--();
BigInt operator--( int );
BigInt operator-();
BigInt operator+ ( int n );
BigInt operator+ ( BigInt n );
BigInt&operator+=( int n );
BigInt&operator+=( BigInt n );
BigInt operator- ( int n );
BigInt operator- ( BigInt n );
BigInt&operator-=( int n );
BigInt&operator-=( BigInt n );
BigInt operator* ( int n );
BigInt operator* ( BigInt n );
void operator*=( int n );
void operator*=( BigInt n );
BigInt operator/ ( int n );
BigInt operator/ ( BigInt n );
void operator/=( int n );
void operator/=( BigInt n );
int operator% ( int n );
BigInt operator% ( BigInt n );
void operator%=( int n );
void operator%=( BigInt n );
int divide( int n ); // Divides storing quotient in *this and returning the remainder
BigInt divide( BigInt n ); // Divides storing quotient in *this and returning the remainder
BigInt operator* ( long double n ); // Multiplies by a double and truncates (always under-estimates!)
void operator*=( long double n ); // Multiplies by a double and truncates (always under-estimates!)
/** Bitwise arithmetic **/
BigInt operator<< ( int n );
void operator<<=( int n );
BigInt operator>> ( int n ); // Works differently for negative numbers
void operator>>=( int n ); // Works differently for negative numbers
/*
BigInt operator& ( int n );
BigInt operator& ( BigInt n );
void operator&= ( int n );
void operator&= ( BigInt n );
BigInt operator| ( int n );
BigInt operator| ( BigInt n );
void operator|= ( int n );
void operator|= ( BigInt n );
BigInt operator^ ( int n );
BigInt operator^ ( BigInt n );
void operator^= ( int n );
void operator^= ( BigInt n );
BigInt operator~();
*/
/** Concatenation ;-) **/
BigInt operator,( int n );
BigInt operator,( BigInt n );
/** Casting **/
bool operator!();
operator bool();
//operator int(); //XXX: Don't do this!!! It takes precedence over operator+(int,BigInt)
operator string();
/** Comparison **/
bool operator<( BigInt n );
bool operator>( BigInt n );
bool operator==( BigInt n );
bool operator<=( BigInt n );
bool operator>=( BigInt n );
bool operator<( int n );
bool operator>( int n );
bool operator==( int n );
bool operator<=( int n );
bool operator>=( int n );
int compare( BigInt n );
/** Returns the lowest value as an integer (watch out for overflow) **/
int toInt();
/** Returns the value as a decimal string **/
string toString();
/** Outputs decimal value to stdout **/
void print();
/** Outputs the decimal value, with commas **/
void printWithCommas( ostream &out );
private:
/** Expansion **/
void grow();
/** I/O Friends **/
friend istream &operator>>( istream &in, BigInt &n );
friend ostream &operator<<( ostream &out, BigInt n );
/** Logarithms **/
friend long double log2( BigInt x, long double epsilon );
inline friend long double log( BigInt x, long double epsilon );
inline friend long double log10( BigInt x, long double epsilon );
inline friend long double lg( BigInt x, long double epsilon );
inline friend long double ln( BigInt x, long double epsilon );
};
BigInt operator+( int m, BigInt &n )
{
return n + m;
}
BigInt operator-( int m, BigInt &n )
{
return -n + m;
}
BigInt operator*( int m, BigInt &n )
{
return n * m;
}
BigInt operator/( int m, BigInt &n )
{
return BigInt( m ) / n;
}
BigInt operator%( int m, BigInt &n )
{
return BigInt( m ) % n;
}
/** Misc **/
inline bool isDigit( int c )
{
return( c >= ( int )'0' && c <= ( int )'9' );
}
/** Input/Output **/
istream &operator>>( istream &in, BigInt &n ) // FIXME: see inside
{
n.size = 0;
n.sign = 1;
int sign = 1;
int c;
while( ( c = in.peek() ) >= 0 &&
( c == ' ' || c == '\t' || c == '\r' || c == '\n' ) )
in.get();
if( c < 0 || ( c != ( int )'-' && !isDigit( c ) ) )
{
in >> c; // XXX: force in.fail()
return in;
}
if( c == ( int )'-' ) { sign = -1; in.get(); }
// FIXME: Extremely inefficient! Use a string.
while( ( c = in.peek() ) >= 0 && isDigit( c ) )
{
in.get();
n *= 10;
n += ( c - ( int )'0' );
}
n.sign = sign; //XXX: assign n.sign directly after fixing the FIXME
n.normalize();
return in;
}
ostream &operator<<( ostream &out, BigInt n ) //FIXME: make more efficient
{
return out << n.toString();
}
BigInt::BigInt( int n, int cap )
{
cap = max( cap, ( int )sizeof( n ) * 8 );
capacity = cap;
sign = sig( n );
n *= sign;
digits = new char[cap];
memset( digits, 0, cap );
for( size = 0; n; size++ )
{
digits[size] = n % 10;
n /= 10;
}
}
BigInt::BigInt( int n )
{
capacity = 1024;
sign = sig( n );
n *= sign;
digits = new char[capacity];
memset( digits, 0, capacity );
size = 0;
while( n )
{
digits[size++] = n % 10;
n /= 10;
}
}
BigInt::BigInt( long double d )
{
capacity = 1024;
sign = ( d < 0 ? -1 : d > 0 ? 1 : 0 );
d *= sign;
digits = new char[capacity];
memset( digits, 0, capacity );
size = 0;
d = floor( d );
while( d > 0 )
{
digits[size++] = 0 >? ( int )( ( d - floor( d / 10 ) * 10 ) + 0.5 ) <? 9;
d = floor( d / 10 );
}
}
BigInt::BigInt()
{
capacity = 128;
sign = 0;
digits = new char[capacity];
memset( digits, 0, capacity );
size = 0;
}
BigInt::BigInt( string s )
{
capacity = max( ( int )s.size(), 16 );
sign = 0;
digits = new char[capacity];
memset( digits, 0, capacity );
istringstream in( s );
in >> ( *this );
}
BigInt::BigInt( const char s[] )
{
capacity = max( ( int )strlen( s ), 16 );
sign = 0;
digits = new char[capacity];
memset( digits, 0, capacity );
istringstream in( s );
in >> ( *this );
}
BigInt::BigInt( const BigInt &n )
{
capacity = n.capacity;
sign = n.sign;
size = n.size;
digits = new char[capacity];
memcpy( digits, n.digits, capacity );
}
const BigInt &BigInt::operator=( const BigInt &n )
{
if( &n != this )
{
if( capacity < n.size )
{
capacity = n.capacity;
delete [] digits;
digits = new char[capacity];
}
sign = n.sign;
size = n.size;
memcpy( digits, n.digits, size );
memset( digits + size, 0, capacity - size );
}
return *this;
}
const BigInt &BigInt::operator=( int n )
{
sign = sig( n );
n *= sign;
for( size = 0; n; size++ )
{
digits[size] = n % 10;
n /= 10;
}
return *this;
}
BigInt::~BigInt()
{
delete [] digits;
}
void BigInt::normalize()
{
while( size && !digits[size-1] ) size--;
if( !size ) sign = 0;
}
int BigInt::sig( int n )
{
return( n > 0 ? 1 : ( n == 0 ? 0 : -1 ) );
}
int BigInt::sig( long double n )
{
return( n > 0 ? 1 : ( n == 0 ? 0 : -1 ) );
}
int BigInt::toInt()
{
int result = 0;
for( int i = size - 1; i >= 0; i-- )
{
result *= 10;
result += digits[i];
if( result < 0 ) return sign * 0x7FFFFFFF;
}
return sign * result;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -