Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Quinton Chapman
- //CS 2336
- //Lab 46
- #include <iostream>
- #include <string>
- #include <deque>
- #include <iostream>
- #include <lab46.h>
- using namespace std;
- BigInt BigInt::operator+(const BigInt& rhs) const{
- BigInt zero(0);
- if(*this==zero){
- return rhs;
- }
- else if(rhs==zero){
- return *this;
- }
- else if(this->sign == rhs.sign){
- BigInt left(*this);
- BigInt right(rhs);
- BigInt ans;
- while (left.digits.size() < right.digits.size()){
- left.digits.push_back('0');
- }
- while (left.digits.size() > right.digits.size()){
- right.digits.push_back('0');
- }
- int carry=0;
- for(uint i=0;i<left.digits.size();i++){
- int sum=carry+((left.digits.back()-'0')+(right.digits.back()-'0'));
- carry=sum/10;
- sum%=10;
- ans.digits.push_back('0'+sum);
- left.digits.pop_front();
- right.digits.pop_front();
- }
- ans.sign=this->sign;
- return ans;
- }//end if they are the same signs
- else{
- BigInt left(*this);
- BigInt right(rhs);
- BigInt ans;
- left.sign=POSITIVE;
- right.sign=POSITIVE;
- ans.sign=this->sign;
- while (left.digits.size() < right.digits.size()){
- left.digits.push_back('0');
- }
- while (left.digits.size() > right.digits.size()){
- right.digits.push_back('0');
- }
- int sub=0;
- if(left<right){
- left.digits.swap(right.digits);
- ans.sign=rhs.sign;
- }
- for(uint i=left.digits.size(); i > 0; i--){
- int sum=(right.digits.back()-'0')-(left.digits.back()-'0')+sub;
- if(sum<0){
- sum+=10;
- sub=-1;
- }
- else
- sub=0;
- ans.digits.push_back(sum+'0');
- left.digits.pop_front();
- right.digits.pop_front();
- }
- while(digits.size()>1 && digits.back()=='0')
- ans.digits.pop_back();
- return ans;
- }//end if they are different signs
- }//end plus
- BigInt BigInt::operator-(const BigInt& rhs) const{
- BigInt sub(rhs);
- if(sub.sign==NEGATIVE)
- sub.sign=POSITIVE;
- else if(sub.sign==POSITIVE)
- sub.sign=NEGATIVE;
- return *this + sub;
- }//end minus
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement