/*
 *  Bigint.cc
 *  (C) Porkolab Zoltan, ELTE, Budapest, Hungary
 *  (C) 1998
 */

#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include "bigint"

Bigint::Bigint( const char *s)
{
    size = strlen( s );
    init( s );
}
Bigint::Bigint( const Bigint &rhs)
{
    size = rhs.size;
    init( rhs.v );
}
Bigint::~Bigint() 
{
}
Bigint &Bigint::operator=( const Bigint &rhs)
{
    if ( &rhs != this )
    {
        delete [] v;
        size = rhs.size;
        init( rhs.v );
    }
    return *this;
}
Bigint &Bigint::operator++()
{
    for ( int i = size-1; i >= 0; --i )
    {
        ++v[i];
        if ( v[i] == '9'+1 )
            v[i] = '0';
        else
            break;
    }
    if ( v[0] == '0' )
    {
        char *p = new char[size+1];
        p[0] = '1';
        memcpy( p+1, v, size); 
        ++size;
        delete [] v;
        v = p;
    }
    return *this;
}
int Bigint::get_digit( int i) const
{
    assert( i > 0  &&  i <= size );

    return v[size-i]-'0';
}
int Bigint::operator[]( int i) const
{
    return get_digit( i );
}
double Bigint::get_value() const
{
    assert( size < 10 );

    double val = v[0]-'0';
    for ( int i = 1; i < size ; ++i )
    {
        val *= 10;
        val += v[i]-'0'; 
    }
    return val;
}
Bigint::operator double() const
{
    return get_value();
}
int Bigint::mark_five( int arg )
{
    return 1;
}
void Bigint::init( const char *s)
{
    v = new char[size];
    for ( int i = 0; i < size; ++i )
        v[i] = s[i];
}
ostream& operator<<( ostream& os, const Bigint &bi)
{
    for ( int i = 0; i < bi.size; ++i )
        os << bi.v[i];
    return os;
}