Answered step by step
Verified Expert Solution
Question
1 Approved Answer
I am writing the implementation for a BigInt class using overloaded addition and subtraction operators. Everything compiles well, but when I run the program, it
I am writing the implementation for a BigInt class using overloaded addition and subtraction operators. Everything compiles well, but when I run the program, it creates an infinite loop. What is causing this and how do I fix it?
My program
#includeBigInt BigInt::operator+(const BigInt& rhs)const { if(this->sign == ZERO) { return rhs; } else if(rhs.sign == ZERO) { return *this; } else if(this->sign == rhs.sign) { // Make copies of both *this and rhs to be manipulated // Also Create the BigInt object 'sum' BigInt augend(*this); BigInt addend(rhs); BigInt sum; if(augend.digits.size() > addend.digits.size()) { addend.digits.resize(augend.digits.size(), '0'); } else if(augend.digits.size() < addend.digits.size()) { augend.digits.resize(addend.digits.size(), '0'); } for(uint i = 0; i < augend.digits.size(); i++) { augend.digits[i] -= '0'; addend.digits[i] -= '0'; } int carry = 0; for(uint i = 0; i < augend.digits.size(); i++) { int digit = augend.digits[i] + addend.digits[i] + carry; carry = 0; if(digit >= 10) { carry = digit / 10; digit = digit % 10; } sum.digits.push_back(digit + '0'); } return sum; } else { BigInt minu; BigInt subtra; BigInt diff; if(*this < rhs) { minu = rhs; subtra = *this; minu.sign = POSITIVE; subtra.sign = POSITIVE; subtra.digits.resize(minu.digits.size(), '0'); diff.sign = rhs.sign; } else { minu = *this; subtra = rhs; minu.sign = POSITIVE; subtra.sign = POSITIVE; subtra.digits.resize(minu.digits.size(), '0'); diff.sign = this->sign; } for (uint i = 0; i < minu.digits.size(); i++) { minu.digits[i] -= '0'; subtra.digits[i] -= '0'; } int borrow = 0; for(uint i = 0; i < minu.digits.size(); i++) { int digit = borrow + minu.digits[i] - subtra.digits[i]; borrow = 0; if(digit < 0) { borrow--; digit = digit + 10; } diff.digits.push_back(digit + '0'); } return diff; } } BigInt BigInt::operator-(const BigInt& rhs)const { BigInt addend(rhs); if(addend.sign == NEGATIVE) { addend.sign = POSITIVE; } else if(addend.sign == POSITIVE) { addend.sign = NEGATIVE; } return *this + addend; }
The header file for the class
#ifndef LAB46_H #define LAB46_H #include#include #include using namespace std; typedef enum {NEGATIVE, ZERO, POSITIVE} Sign; bool isInt(string s); class BigInt { friend ostream& operator<<( ostream& output, const BigInt& ); friend istream& operator>>( istream& input, BigInt& ); public: BigInt(); // constructor; digits = 0 BigInt( int num ); // constructor; digits = num BigInt( const string str ); // constructor; digits = str BigInt( const BigInt& other ); // copy constructor bool operator==( const BigInt& rhs ) const; // Equality bool operator< ( const BigInt& rhs ) const; // Less Than BigInt operator+ ( const BigInt& rhs ) const; // Addition BigInt operator- ( const BigInt& rhs ) const; // Subtraction private: Sign sign; // Sign of # deque<char> digits; // Deque of digits of # }; #endif
The main function to test the program
#includeusing namespace std; int main() { BigInt a, b; while (cin >> a >> b) { cout << a << " + " << b << " = " << a + b << endl; cout << a << " - " << b << " = " << a - b << endl; cout << b << " - " << a << " = " << b - a << endl; } cout << endl; cout << "Fibonacci Sequence" << endl; BigInt i(1), end(51), first(1), second(1); while (i < end) { cout << "Fib(" << i << ") = " << first << endl; second = first + second; first = second - first; i = i + 1; } return EXIT_SUCCESS; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started