Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. //Quinton Chapman
  2. //CS 2336
  3. //Lab 46
  4.  
  5. #include <iostream>
  6. #include <string>
  7. #include <deque>
  8. #include <iostream>
  9. #include <lab46.h>
  10.  
  11. using namespace std;
  12.  
  13. BigInt BigInt::operator+(const BigInt& rhs) const{
  14.     BigInt zero(0);
  15.     if(*this==zero){
  16.         return rhs;
  17.     }
  18.     else if(rhs==zero){
  19.         return *this;
  20.     }
  21.     else if(this->sign == rhs.sign){
  22.         BigInt left(*this);
  23.         BigInt right(rhs);
  24.         BigInt ans;
  25.  
  26.         while (left.digits.size() < right.digits.size()){
  27.             left.digits.push_back('0');
  28.         }
  29.         while (left.digits.size() > right.digits.size()){
  30.             right.digits.push_back('0');
  31.         }
  32.  
  33.         int carry=0;
  34.         for(uint i=0;i<left.digits.size();i++){
  35.             int sum=carry+((left.digits.back()-'0')+(right.digits.back()-'0'));
  36.             carry=sum/10;
  37.             sum%=10;
  38.             ans.digits.push_back('0'+sum);
  39.             left.digits.pop_front();
  40.             right.digits.pop_front();
  41.         }
  42.    
  43.         ans.sign=this->sign;
  44.         return ans;
  45.     }//end if they are the same signs
  46.     else{
  47.         BigInt left(*this);
  48.         BigInt right(rhs);
  49.         BigInt ans;
  50.         left.sign=POSITIVE;
  51.         right.sign=POSITIVE;
  52.         ans.sign=this->sign;
  53.        
  54.         while (left.digits.size() < right.digits.size()){
  55.             left.digits.push_back('0');
  56.         }
  57.         while (left.digits.size() > right.digits.size()){
  58.             right.digits.push_back('0');
  59.         }
  60.        
  61.         int sub=0;
  62.         if(left<right){
  63.             left.digits.swap(right.digits);
  64.             ans.sign=rhs.sign;
  65.         }
  66.  
  67.             for(uint i=left.digits.size(); i > 0; i--){
  68.                 int sum=(right.digits.back()-'0')-(left.digits.back()-'0')+sub;
  69.                 if(sum<0){
  70.                     sum+=10;
  71.                     sub=-1;
  72.                 }
  73.                 else
  74.                     sub=0;
  75.                 ans.digits.push_back(sum+'0');
  76.                 left.digits.pop_front();
  77.                 right.digits.pop_front();
  78.             }
  79.            
  80.             while(digits.size()>1 && digits.back()=='0')
  81.                 ans.digits.pop_back();
  82.  
  83.         return ans;
  84.     }//end if they are different signs
  85. }//end plus
  86.  
  87. BigInt BigInt::operator-(const BigInt& rhs) const{
  88.     BigInt sub(rhs);
  89.     if(sub.sign==NEGATIVE)
  90.         sub.sign=POSITIVE;
  91.     else if(sub.sign==POSITIVE)
  92.         sub.sign=NEGATIVE;
  93.     return *this + sub;
  94. }//end minus
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement