Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In general, computer systems support single (and mostly double) precision for ca

ID: 3878804 • Letter: I

Question

In general, computer systems support single (and mostly double) precision for calculations. Overflows are common when results exceed the precision sizes. Arbitrary-precision or multi-precision arithmetic partition the arbitrary numbers into digit arrays and perform the arithmetic calculations on the digit arrays

. Create a bigPosInt class using dynamic arrays. overload +, - and * operators to add, subtract and multiply bigPosInt numbers. overload >> and << for easy io. implement appropriate constructors. 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; numTest = num1 - num2; cout << “numTest: ” << numTest << endl;

Explanation / Answer

#include<iostream>
#include<string>
using namespace std;
//Calls definition
class bigPosInt
{
//Data member to store the number in string format
string number;
public:
//Parameterized Constructor to initialize the member
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<<endl;
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("129");
bigPosInt num2("125");
//Displays the numbers
cout <<"The First Number: "<<num1;
cout <<"The Second Number: "<<num2;
//Adds two objects and stores the result in num3 object
bigPosInt num3 = num1 + num2;
//Displays the result
cout<<" Result: "<<num3;
//Subtracts two objects and stores the result in num4 object
bigPosInt num4 = num1 - num2;
//Displays the result
cout<<" Result: "<<num4;
return 0;
}//End of main

Sample Run:

The First Number: 129
The Second Number: 125

Result: 254

Result: 004