Question: C++ Dynamic Arrays Implement the code below with these tasks 1. Overload *, / operators to multiply and divide bigPosInt numbers. ( + and -
C++ Dynamic Arrays
Implement the code below with these tasks
1. Overload *, / operators to multiply and divide bigPosInt numbers. ( + and - operators have been implemented in the code below)
2. overload >> and << for easy io.
3. implement appropriate constructors.
4. main.cpp shall support the following statements:
bigPosInt num1(1234567890123456789012345); bigPosInt num2(11223344556677889900112233"); ; cin >> num1;
cout << num1: << num1 << endl; cout << num2: << num2 << endl;
numTest = num1 * num2; cout << numTest: << numTest << endl;
numTest = num1 / num2; cout << numTest: << numTest << endl;
C++ Source code
#include
#includeusing namespace std; //Calls definition class bigPosInt { string number; public: bigPosInt(string s) { number = s; }//End of constructor //Default constructor to initialize data member bigPosInt() { number = ""; }//End of constructor //Method prototype friend ostream & operator << (ostream &out, const bigPosInt &c); friend istream & operator >> (istream &in, bigPosInt &c); bigPosInt operator + (bigPosInt); bigPosInt operator - (bigPosInt); }; //Overloads the + operator bigPosInt bigPosInt::operator + (bigPosInt obj) { //Declares a temporary object to store the addition result bigPosInt res; //Calculates the length of first number int lenF = number.length(); //Calculates the length of second number int lenS = obj.number.length(); //To store the character of a digit char c; //Initialize the val to zero for storing carry int val = 0; //Loops till end of the number for(int x = lenF-1; x >= 0; x--) { //Converts the character at 'x' position and adds it val = val + (number[x] - 48) + (obj.number[x] - 48); //Checks if the result is two digit if(val > 9) { //Extracts the unit digit and converts it to character c = (val % 10) + '0'; //Set the val to one for carry val = 1; }//End of if //If the val is single digit else { //Convert to character c = '0' + val; //Set the val to zero for no carry val = 0; }//End of else //Concatenate the character at the beginning of the string res.number = c + res.number; }//End of loop //Return the result object return res; }//End of method //Overloads the - operator bigPosInt bigPosInt::operator - (bigPosInt obj) { //Declares the temporary object to store the result bigPosInt res; //Calculates the length of first number int lenF = number.length(); //Calculates the length of second number int lenS = obj.number.length(); //To store the character of a digit char c; //Initialize the val to zero for storing borrow int val = 0; //Loops till the length of the number for(int x = lenF-1; x >= 0; x--) { //Checks if the first number digit is less then the second number digit if((number[x] - 48) < (obj.number[x] - 48)) { //Subtract the second from the first val = val + (obj.number[x] - 48) - (number[x] - 48); //Convert the result to character c = val + '0'; //Set the val to -1 for borrow val = -1; }//End of if //Checks if the first number digit is not less then the second number digit else { //Subtract the first from the second val = val + (number[x] - 48) - (obj.number[x] - 48); //Convert the result to character c = '0' + val; //Set the val to 0 for borrow val = 0; }//End of else //Concatenates the character at the beginning of the result res.number = c + res.number; }//End of loop //Returns the result object return res; }//End of method //Overloads << operator to display the object data ostream & operator << (ostream &out, const bigPosInt &c) { out << c.number< return out; }//End of method //Overloads >> operator to accept the object data istream & operator >> (istream &in, bigPosInt &c) { cout << "Enter the number: "; in >> c.number; return in; }//End of method //Main method definition int main() { //Creates two objects using parameterized constructor bigPosInt num1("256"); bigPosInt num2("209"); //Displays the numbers cout <<"The First Number: "< "The Second Number: "< //Adds two objects and stores the result in num3 object bigPosInt num3 = num1 + num2; //Displays the result cout<<" Result: "< //Subtracts two objects and stores the result in num4 object bigPosInt num4 = num1 - num2; //Displays the result cout<<" Result: "< return 0; }//End of main
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
