Esempio n. 1
0
//------------------------------------------------------------------------------
BigNumber BigNumber::mult (int small_number) {
        int carry=0;
        int temp_result;
        BigNumber result;
        TNode *temp;
        temp=last;
        for (int i=0; i<Count(); i++) {
                temp_result=temp->data * small_number + carry;
                carry=(int)(temp_result/1000);
                result.AddBefore(result.root, temp_result%1000);
                temp=temp->prev;
        }
        if (carry)
                result.AddBefore(result.root,carry);
        return result;
}
Esempio n. 2
0
//------------------------------------------------------------------------------
BigNumber operator+ (BigNumber x1, BigNumber x2) {
        int carry=0;
        int result;
        BigNumber temp;
        FIFO fifo;
        if (x1.Count()<x2.Count()) {
                temp=x1;
                x1=x2;
                x2=temp;
        }
        //temp.DeleteList(); //????????????????????????????????
        TNode *temp_x1=x1.last;
        TNode *temp_x2=x2.last;
        TNode *temp_result=temp.last;//???????????????????????
        for (int i=0;i<x2.Count();i++) { //Ѕлок протестирован
                result=temp_x1->data+temp_x2->data+carry;
                carry=(int)(result/1000);
                fifo.Push(result%1000);
                temp_x1=temp_x1->prev;
                temp_x2=temp_x2->prev;
        }
        if ( (x2.Count()==x1.Count() ) && (carry!=0) )
                fifo.Push(carry);
        else {
                for (int i=x2.Count();i<x1.Count();i++) {
                        result=temp_x1->data+carry;
                        carry=(int)(result/1000);
                        fifo.Push(result%1000);
                        temp_x1=temp_x1->prev;
                }
                if (carry!=0)
                        fifo.Push(carry);
        }

        int count=fifo.Count();
        for (int i=0;i<count;i++) {
                if (!fifo.Pop(result)) {
                        cout <<"Error\n";
                        exit(-1);
                }
                temp.AddBefore(temp.root,result);
        }
        return temp;

}