Question

C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve...

C++ NEED AS SOON AS POSSIBLE!

BigInt class is used for the mathematical operations that involve very big integer calculations that are outside the limit of all available primitive data types. For example, factorial of 100 contains 158 digits in it so we can’t store it in any primitive data type available. We can store as large Integer as we want in it.
Your goal is to overload the operators for a generic “BigInt” class. You will need to write two files (BigInt.h and BigInt.cpp). Your implemented class must fully provide the definitions of following class (interface) functions .
class BigInt
{
//think about the private data members
public:
BigInt(int val = 0);
BigInt(const string& text);
BigInt(const BigInt& copy); // copy constructor
// Binary Operators
// Arithmetic Operators
BigInt operator+(const BigInt& val) const;
BigInt operator+(int val) const;
BigInt operator-(const BigInt& val) const;
BigInt operator-(int val) const;
BigInt operator*(const BigInt& val) const;
// Compound Assignment Operators
BigInt operator+=(const BigInt& rhs);
BigInt operator-=(const BigInt& rhs);
BigInt operator*=(const BigInt& rhs);
// Logical Operators
bool operator==(const BigInt& val) const;
bool operator!=(const BigInt& val) const;
bool operator<(const BigInt& val) const;
bool operator<=(const BigInt& val) const;
bool operator>(const BigInt& val) const;
bool operator>=(const BigInt& val) const

// Unary Operators
BigInt& operator++(); // Pre-increment Operator
BigInt operator++(int); // Post-increment Operator
BigInt& operator--(); // Pre-decrement Operator
BigInt operator--( int ); // Post-decrement Operator
//Conversion Operator
operator string(); // return value of the BigInt as string
~BigInt(); // destructor
};
ostream& operator<<(ostream& output, const BigInt& val); // outputs the BigInt
istream& operator>>(istream& input, BigInt& val); // inputs the BigInt

Your goal is to overload the operators for a generic “Polynomial” class. A polynomial will be represented via its coefficients. Here is a third degree polynomial 4x3 + 3x + 2; where we have four coefficients and the coefficient corresponding to 2nd power is zero. You are required to write two files (Polynomial.h and Polynomial.cpp). Your implemented class must fully provide the definitions of following class (interface) functions.
class Polynomial {
// think about the private data members (coefficient values can be of type int)
public:
//include all the necessary checks before performing the operations in the functions
Polynomial(); // a default constructor
Polynomial(int); // a parameterized constructor, received the highest degree of polynomial
Polynomial(const Polynomial &); // a copy constructor
// Binary Operators
// Assignment Operator
Polynomial operator=(const Polynomial& rhs); //assigns (copies) the rhs Polynomial to "this" Polynomial
// Arithmetic Operators
Polynomial operator+(const Polynomial &); // adds two Polynomials and returns the result
Polynomial operator-(const Polynomial &); // subtracts two Polynomials and returns the result
// Compound Assignment Operators
void operator+=(const Polynomial&); // adds two Polynomials
void operator-=(const Polynomial&); // subtracts two Polynomials
// Logical Operator
bool operator==(const Polynomial &); // compares and returns true if equal
// Conversion Operator
operator string() const; // returns the value of the Polynomial as a string like “4x^3 + 3x + 2”
~Polynomial(); // destructor
};

ostream& operator<<(ostream& output, const Polynomial&); // outputs the Polynomial istream& operator>>(istream& input, Polynomial&); // inputs the Polynomial

0 0
Add a comment Improve this question Transcribed image text
Answer #1

THIS IS THE QUESTION YOU ASKED FOR ITS WORKING PERFECTLY FINE

Comment down for any queries
Please give a thumbs up if you are satisfied with the answer


#ifndef BIGINT_H_
#define BIGINT_H_
#include <string>
using namespace std;
class BigInt
{
   static const int digitsPerSlot = 8;
static const int valuePerSlot = 100000000;
void copy(const BigInt & that);
void constructPointers();
slot * start;
slot * end;
int numberOfSlots;
void clear();
void put(int value);
void push(int value);
void add(const BigInt & that);
void subtract(const BigInt & that);
void removeLeadingZeros();
//think about the private data members
public:
BigInt(int val = 0);
BigInt(const string& text);
BigInt(const BigInt& copy); // copy constructor
// Binary Operators
// Arithmetic Operators
BigInt operator+(const BigInt& val) const;
BigInt operator+(int val) const;
BigInt operator-(const BigInt& val) const;
BigInt operator-(int val) const;
BigInt operator*(const BigInt& val) const;
// Compound Assignment Operators
BigInt operator+=(const BigInt& rhs);
BigInt operator-=(const BigInt& rhs);
BigInt operator*=(const BigInt& rhs);
// Logical Operators
bool operator==(const BigInt& val) const;
bool operator!=(const BigInt& val) const;
bool operator<(const BigInt& val) const;
bool operator<=(const BigInt& val) const;
bool operator>(const BigInt& val) const;
bool operator>=(const BigInt& val) const;
// Unary Operators
BigInt& operator++(); // Pre-increment Operator
BigInt operator++(int); // Post-increment Operator
BigInt& operator--(); // Pre-decrement Operator
BigInt operator--( int ); // Post-decrement Operator
//Conversion Operator
operator string();
// return value of the BigInt as string
~BigInt(); // destructor
};
ostream& operator<<(ostream& output, const BigInt& val); // outputs the BigInt
istream& operator>>(istream& input, BigInt& val); // inputs the BigInt
#endif /* BIGINT_H_ */

//CPP FILE OF BIG INT

#include<cstdlib>
#include "BigInt.h"
#include<iostream>
#include<string>
using namespace std;
BigInt::BigInt()
{
constructPointers();
}
BigInt::BigInt(int val)
{
constructPointers();
if (val < 0)
{
isPositive = false;
val *= -1;
}

if (val >= valuePerSlot)
{
put(val / valuePerSlot);
val %= valuePerSlot;
}
put(val);  
}
BigInt::BigInt(const string& text)
{
constructPointers();
int lengthOfString = 0;
while (string[lengthOfString] != '\0')
{
lengthOfString++;
}
int value = 0;
int index = 0;
if (string[0] == '-')
{
isPositive = false;
lengthOfString--;
index++;
}
while (lengthOfString)
{
if (!(lengthOfString % digitsPerSlot))
{
put(value);
value = 0;
}
value = value * 10 + (string[index] - '0');
lengthOfString--;
index++;
}
put(value);
}
BigInt::BigInt(const BigInt& copy) // copy constructor
{
constructPointers();
copy(copy);
}
// Binary Operators
// Arithmetic Operators

// Compound Assignment Operators
BigInt BigInt::operator+=(const BigInt& rhs)
{
if (isPositive && rhs.isPositive)
{
add(that);
}
else if (isPositive && !rhs.isPositive)
{
BigInt placeholder(rhs);
placeholder.isPositive = true;
subtract(placeholder);
}
else if (!isPositive && rhs.isPositive)
{
BigInt placeholder(rhs);
placeholder.subtract(*this);
copy(placeholder);
}
else
{
add(that);
isPositive = false;
}
return *this;
}
BigInt BigInt::operator+(const BigInt& val) const
{
   return *this+val;
}
BigInt BigInt::operator+(int val) const
{
   return *this+val;
}
BigInt BigInt::operator-(const BigInt& val) const
{
   return *this-val;
}
BigInt BigInt::operator-(int val) const
{
   return *this-val;
}
BigInt BigInt::operator*(const BigInt& val) const
{
   return *this*val;
}
BigInt BigInt::operator-=(const BigInt& rhs)
{
BigInt placeholder(rhs);
placeholder.isPositive = !placeholder.isPositive;
return *this += placeholder;
}
BigInt BigInt::operator*=(const BigInt& rhs)
{
   if (isPositive && that.isPositive)
{
add(that);
}
else if (isPositive && !that.isPositive)
{
BigInt placeholder(that);
placeholder.isPositive = true;
subtract(placeholder);
}
else if (!isPositive && that.isPositive)
{
BigInt placeholder(that);
placeholder.subtract(*this);
copy(placeholder);
}
else
{
add(that);
isPositive = false;
}
return *this;

}
// Logical Operators
bool BigInt::operator==(const BigInt& val) const
{
   if (this->numberOfSlots != val.numberOfSlots)
{
return false;
}
if (isPositive != val.isPositive)
{
return false;
}
slot * currentSlotThis = end;
slot * currentSlotThat = val.end;

while (currentSlotThis != nullptr)
{
if (currentSlotThis->value != currentSlotThat->value)
{
return false;
}
currentSlotThat = currentSlotThat->previous;
currentSlotThis = currentSlotThis->previous;
}
return true;
}
bool BigInt::operator!=(const BigInt& val) const
{
   return !(*this == val);
}
bool BigInt::operator<(const BigInt& val) const
{
   if (this->numberOfSlots != val.numberOfSlots || this->isPositive != val.isPositive)
{
return this->numberOfSlots * (this->isPositive - 0.5) < val.numberOfSlots * (that.isPositive - 0.5);
}

slot * currentSlotThis = start;
slot * currentSlotThat = val.start;
while (currentSlotThis->next != nullptr && currentSlotThis->value == currentSlotThat->value)
{
currentSlotThat = currentSlotThat->next;
currentSlotThis = currentSlotThis->next;
}
return currentSlotThis->value < currentSlotThat->value;
}
bool BigInt::operator<=(const BigInt& val) const
{
return !(val < *this);
}
bool BigInt::operator>(const BigInt& val) const
{
   return val < *this;
}
bool BigInt::operator>=(const BigInt& val) const
{
return !(*this < that);
}
// Unary Operators
BigInt& BigInt::operator++() // Pre-increment Operator
{
return *this += BigInt(1);
}
BigInt BigInt::operator++(int) // Post-increment Operator
{
   BigInt temp(*this);
operator++();
return temp;
}
BigInt& BigInt::operator--() // Pre-decrement Operator
{
   return *this -= BigInt(1);
}
BigInt BigInt::operator--( int ) // Post-decrement Operator
{
   BigInt temp(*this);
operator--();
return temp;  
}
//Conversion Operator
operator BigInt::string()
{
   return string;
}
// return value of the BigInt as string
BigInt::~BigInt()// destructor
{
clear();
}
ostream& operator<<(ostream& output, const BigInt& val)
{
   if (!val.isPositive)
{
output.put('-');
}

slot * currentSlot = val.start;
while (currentSlot != nullptr)
{
int value = currentSlot->value;
int fullValue = value;
int numberOfPaddingZeros = 1;
int digit = 10000000;
if (currentSlot == val.start && (fullValue == 0))
{
output.put('0');
}
else
{
while (digit)
{
// Pad the value in the slot with leading zeros to fill the entire slot, except when it is the first slot.
if (currentSlot != val.start || (fullValue >= digit))
{
output.put((value / digit) + '0');
}

value %= digit;
digit /= 10;
}
}
currentSlot = currentSlot->next;
}
return output;

} // outputs the BigInt

//POLYNOMIAL HEADER

#ifndef POLYNOMIAL_H_
#define POLYNOMIAL_H_

#include <string>
using namespace std;

class Polynomial
{
private:
   int d;
   int *ptr;

public:

Polynomial(); // a default constructor
Polynomial(int degree); // a parameterized constructor, received the highest degree of polynomial
Polynomial(const Polynomial &obj); // a copy constructor

int getDegree() const;
void addData(int data,int index);

void printData() const;

// Binary Operators
// Assignment Operator
Polynomial operator=(const Polynomial& rhs); //assigns (copies) the rhs Polynomial to "this" Polynomial
// Arithmetic Operators
Polynomial operator+(const Polynomial &rhs); // adds two Polynomials and returns the result
Polynomial operator-(const Polynomial &rhs); // subtracts two Polynomials and returns the result
// Compound Assignment Operators
void operator+=(const Polynomial& rhs); // adds two Polynomials
void operator-=(const Polynomial& rhs); // subtracts two Polynomials
// Logical Operator
bool operator==(const Polynomial &rhs); // compares and returns true if equal

// Conversion Operator
operator string() const; // returns the value of the Polynomial as a string like “4x^3 + 3x + 2”
~Polynomial(); // destructor
};

ostream& operator<<(ostream& output, const Polynomial& rhs); // outputs the Polynomial
istream& operator>>(istream& input, Polynomial& rhs); // inputs the Polynomial

#endif /* POLYNOMIAL_H_ */

//POLYNOMIAL CPP


#include<cstdlib>
#include "Polynomial.h"
#include<iostream>
#include<string>
using namespace std;

Polynomial::Polynomial() // a default constructor
{
   d=3;
   ptr=new int[d+1];
}
Polynomial::Polynomial(int degree) // a parameterized constructor, received the highest degree of polynomial
{
   d=degree;
   ptr=new int[d+1];
}
Polynomial::Polynomial(const Polynomial &obj) // a copy constructor
{
   this->d=obj.d;
   this->ptr=new int[d+1];
   for(int i=0;i<=d;i++)
   {
       this->ptr[i]=obj.ptr[i];
   }
}

int Polynomial::getDegree() const
{
   return d;
}

void Polynomial::addData(int data,int index)
{
   if(index>=0 && index<=d)
   {
       ptr[index]=data;
   }


}

void Polynomial::printData()const
{
   string poly;
       for(int i=d;i>=0;i--)
       {
           if(ptr[i]!=0)
           {
               poly= to_string(ptr[i])+"x^" + to_string(i);

               cout<<poly;
               if(i>0)
               {
                   cout<<"+";
               }

           }

       }
       cout<<endl;


}
// Binary Operators
// Assignment Operator
Polynomial Polynomial::operator=(const Polynomial& rhs) //assigns (copies) the rhs Polynomial to "this" Polynomial
{
   if(this==&rhs)
   {
       return *this;
   }
   if(ptr!=NULL)
   {
       delete [] ptr;
   }

       d=rhs.d;
       this->ptr=new int[d];

       for(int i=0;i<=d;i++)
       {
           this->ptr[i]=rhs.ptr[i];
       }

       return *this;


}
// Arithmetic Operators
Polynomial Polynomial::operator+(const Polynomial &rhs) // adds two Polynomials and returns the result
{

   if(d != rhs.d)
   {
       return *this;
   }

   Polynomial toReturn(d);

       for(int i=0;i<=d;i++)
       {
           toReturn.ptr[i]=ptr[i] + rhs.ptr[i];
       }

       return toReturn;

}
Polynomial Polynomial::operator-(const Polynomial &rhs) // subtracts two Polynomials and returns the result
{
   if(d != rhs.d)
   {
       return *this;
   }

   Polynomial toReturn(d);

       for(int i=0;i<=d;i++)
       {
           toReturn.ptr[i]=ptr[i] - rhs.ptr[i];
       }

       return toReturn;
}
// Compound Assignment Operators
void Polynomial::operator+=(const Polynomial& rhs) // adds two Polynomials
{
   *this=*this+rhs;
}
void Polynomial::operator-=(const Polynomial& rhs) // subtracts two Polynomials
{
   *this=*this- rhs;

}
// Logical Operator
bool Polynomial::operator==(const Polynomial &rhs) // compares and returns true if equal
{
   if(d==rhs.d)
   {
       for(int i=0;i<=d;i++)
       {
           if(ptr[i] != rhs.ptr[i])
           {
               return false;
           }
       }

       return true;
   }

   else
   {
       return false;
   }
}

// Conversion Operator
Polynomial::operator string() const // returns the value of the Polynomial as a string like “4x^3 + 3x + 2”
{
   string poly;
   string final;
   for(int i=d;i>=0;i--)
   {
       if(ptr[i]!=0)
       {
           poly= to_string(ptr[i])+"x^" + to_string(d);
       }

       final+=poly;

   }
   return final;

}
Polynomial::~Polynomial() // destructor
{
   delete [] ptr;
}


ostream& operator<<(ostream& output, const Polynomial& rhs) // outputs the Polynomial
{


   rhs.printData();


   return output;

}

istream& operator>>(istream& input, Polynomial& rhs) // inputs the Polynomial
{
   int data;
   int degree;
   cout<<"Enter number of the heighst degree #";
   cin>>degree;
   cout<<endl;
   for(int i=0;i<=degree;i++)
   {
       cout<<"Enter coffecient of "<<i<<endl;
       cin>>data;
       rhs.addData(data,i);

   }

   return input;

}

Comment down for any queries
Please give a thumbs up if you are satisfied with the answer

Add a comment
Know the answer?
Add Answer to:
C++ NEED AS SOON AS POSSIBLE! BigInt class is used for the mathematical operations that involve...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp,...

    Please do it carefully Using the header file ( MyArray.h ) Type the implementation file MyArray.cpp, and a test file to test the functionality of the class. Hint: read all the explanations in the header with attention. MyArray.h : #ifndef MYARRAY_H #define MYARRAY_H #include <iostream> using namespace std; class MyArray { friend ostream& operator<<( ostream & output, const MyArray & rhs); // to output the values of the array friend istream& operator>>( istream & input, MyArray & rhs); // to...

  • Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: --------------------...

    Task: Tasks to complete: ------------------------------------------------------------------------------------------------------------------------------------------ given code: ------------------------------------------------------------------------------------------------------------------------------------------ main.cpp #include #include "rectangleType.h" using namespace std; // part e int main() { rectangleType rectangle1(10, 5); rectangleType rectangle2(8, 7); rectangleType rectangle3; rectangleType rectangle4; cout << "rectangle1: " << rectangle1 << endl; cout << "rectangle2: " << rectangle2 << endl; rectangle3 = rectangle1 + rectangle2;    cout << "rectangle3: " << rectangle3 << endl; rectangle4 = rectangle1 * rectangle2;    cout << "rectangle4: " << rectangle4 << endl; if (rectangle1 > rectangle2) cout << "Area...

  • C++ When running my tests for my char constructor the assertion is coming back false and...

    C++ When running my tests for my char constructor the assertion is coming back false and when printing the string garbage is printing that is different everytime but i dont know where it is wrong Requirements: You CANNOT use the C++ standard string or any other libraries for this assignment, except where specified. You must use your ADT string for the later parts of the assignment. using namespace std; is stricly forbiden. As are any global using statements. Name the...

  • Please show me how to overload the operators << and >> #ifndef LINK_LIST #define LINK_LIST #include...

    Please show me how to overload the operators << and >> #ifndef LINK_LIST #define LINK_LIST #include <iostream> using namespace std; template <typename T> struct Int_Node {    T value;    Int_Node<T> *pre, *next; }; template <typename T> class Link_List {    template <typename U>    friend ostream &operator<<(ostream &, const Link_List<U> &);// print all integers in the list    template <typename U>    friend istream &operator>>(istream &, Link_List<U> &);// input a value at the back of the list, like insert_node(val);...

  • C++ problem: Make a program that the user enter the degree and coefficients of a polynomial....

    C++ problem: Make a program that the user enter the degree and coefficients of a polynomial. Use the overload operators mentioned in the class below: #include <iostream> #include <stdlib.h> using namespace std; class Polynomial{ public:    Polynomial(); //Default constructor: it creates an Polynomial of degree 99 Polynomial(int dg); //Special constructor: it creates an Polynomial of degree dg Polynomial(const Polynomial &Original); //Copy Constructor ~Polynomial(); //Destructor: deallocate memory void readPolynomial(); //readPolynomial Method: Reads all positions of the Polynomial void printPolynomial(); //printPolynomial Method:...

  • How do i Overload the & operator to concatenate two arrays?, so elements from both arrays...

    How do i Overload the & operator to concatenate two arrays?, so elements from both arrays will be seen Below is code i have so far. everything works except the operator overload of &. The & operator only works when both array are equal, first array smaller then second, fails when first array is the largest. class smartArray{ public: int *elements; // dynamic array, memory is allocated in the constructor. Use *this to access size member. int length(); // returns...

  • Please give a answer to both tasks with a screenshot of the running file. Thanks Consider...

    Please give a answer to both tasks with a screenshot of the running file. Thanks Consider that individual nodes in an unsorted linked list have the following definition class Vector public: Vector(int s = 0)( // makes Size //allocates s space, // makes all entries s, kes all entries e Vector (const Vector & rhs) // copy constructor // makes self a deep copy of rhs Vector operator (const Vector & rhs)(// makes self a deep copy of rhs nVector...

  • C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType va...

    C++ LinkedList I need the code for copy constructor and assignment operator #include <iostream> #include <string> using namespace std; typedef string ItemType; struct Node {    ItemType value;    Node *next; }; class LinkedList { private:    Node *head;    // You may add whatever private data members or private member functions you want to this class.    void printReverseRecursiveHelper(Node *temp) const; public:    // default constructor    LinkedList() : head(nullptr) { }    // copy constructor    LinkedList(const LinkedList& rhs);    // Destroys all the dynamically allocated memory    //...

  • // thanks for helping // C++ homework // The homework is to complete below in the...

    // thanks for helping // C++ homework // The homework is to complete below in the stack.h : // 1. the copy constructor // 2. the assignment operator // 3. the destructor // 4. Write a test program (mytest.cpp) to test copy and assignment // 5. Verify destructor by running the test program in Valgrind // This is the main.cpp #include <iostream> #include "stack.h" using namespace std; int main() { Stack<int> intStack; cout << "\nPush integers on stack and dump...

  • Write a MyString class that stores a (null-terminated) char* and a length and implements all of...

    Write a MyString class that stores a (null-terminated) char* and a length and implements all of the member functions below. Default constructor: empty string const char* constructor: initializes data members appropriately Copy constructor: prints "Copy constructor" and endl in addition to making a copy Move constructor: prints "Move constructor" and endl in addition to moving data Copy assignment operator: prints "Copy assignment" and endl in addition to making a copy Move assignment operator: prints "Move assignment" and endl in addition...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT