Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
3.0 kB
2
Indexable
Never
HugeInteger::HugeInteger():CARRY(10000000000000000000U),CARRY_LENGTH(19U){
    _length=max_length;
    data= new uint64_t[_length];
    _sign=0;
    for(int i=0;i<_length;i++) data[i]=0;

    // TODO 1
}

HugeInteger::HugeInteger(const HugeInteger& h):CARRY(10000000000000000000U),CARRY_LENGTH(19U){
_sign=h._sign;
_length=h._length;
data=new uint64_t[_length];
for(int i=0;i<_length;i++) data[i]=h.data[i];
    //TODO 2
}
HugeInteger::HugeInteger(string& s):CARRY(10000000000000000000U),CARRY_LENGTH(19U){
    _length = max_length;
    data = new uint64_t[_length];
    _sign = false;
    for(int i=0;i<_length;i++) data[i]=0;
    input(s);
}

HugeInteger::~HugeInteger(){
    delete [] data;
    // TODO[] 3
}

void HugeInteger::resize(){
    if(max_length == _length) return;
    uint64_t* temp = data;
    data = new uint64_t[max_length];
    for(int i = 0 ; i < max_length-_length; ++i)
        data[i] = 0;
    for(int i = max_length-_length ; i < max_length; ++i)
        data[i] = temp[i+_length-max_length];
    delete [] temp;
    _length = max_length;
    return;
}

void HugeInteger::input(string& s){
    _sign=0;
    while( s.length() > max_length*CARRY_LENGTH ){
        max_length += 1;}
    _length = max_length;
    delete [] data;
    data=new uint64_t[_length];
    for(int i=0;i<_length;i++) data[i]=0;
    uint64_t p = 1, array_count = _length-1;
   // i 從 s.length()-1 開始 ; 如果 i >= 0 且 s[i] 不是 '-' 就繼續迴圈 ; i減一:
   
    for(int i=s.length()-1;i>=0;i--)
    {
        if(s[i]=='-'){
            _sign=1;break;
        }
        data[array_count]+=((s[i]-'0') * p);
        //data[array_count] 加上 ((s[i]-'0') 乘以 p);
        p*=10;
        
        if((s.length()-i)%CARRY_LENGTH==0){
            //cout<<array_count;
            array_count--;

            p=1;
        }
        //如果 (s.length()-i) 可以被 CARRY_LENGTH 整除:
        //    array_count 減一 且 p 重設為 1 (這表示 array 的某一格存滿了,要往前進一格繼續存)

    }
    // TODO 4

    return;
}
void HugeInteger::print(){
    // TODO 5
    // hint: you will need setw(), setfill('0'), and controll cout carefully.
   // cout<<_length<<"\n";
   if(_length==1&&data[0]==0){cout<<0;_sign=0;}
else{
   int re=0;//re=1有數字
    if(_sign==1)cout<<"-";
    for(int i=0;i<_length;i++) {
        if(data[i]==0){
            if(re==1)
             for(uint64_t k=0;k<CARRY_LENGTH;k++)cout<<0;}
        else {
        if(re)cout<<setfill('0')<<setw(19)<<data[i];
        else cout<<data[i];re=1;}}
}

    return;
}
bool HugeInteger::isGreaterThan(HugeInteger& n){
    this->resize(); n.resize();
    
    if(this->_sign==1&&n._sign==0) return 0;
    else if(this->_sign==0&&n._sign==1) return 1;
    else {
        for(int i=0;i<max_length;i++){
            
                if(this->data[i]>n.data[i]){  if(_sign) return 0; else return 1;}
        else if(this->data[i]<n.data[i]){ if(_sign) return 1; else return 0;}
        else continue;
            }
    }
      return 0;
    // TODO 6
}
Leave a Comment