Question
The following class (long_number) represents a number of any length.
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
class long_number
{
vector<int> vnumber;
};
Complete the following code so that the default constructor generates the number 0.
____:
____ ()
{
vnumber. ____ ( ____ );
}
Continuing with the code on Question 4, complete the following code which implements the function print, which prints the long_number
____ print()
{
For (int ____ : ____ )
{
____ << n;
}
cout << endl;
}
If your code is correct the following code should show one (and only one) 0 on the screen.
int main()
{
long_number n1;
n1.print();
cin.get();
return 0;
}
Continuing with the code in Question 5, complete the following code which implements a constructor that turns an array of chars into a long_number.
For example, if the string is "12345", the corresponding long_number will have the following value in its fields:
vnumber = {1,2,3,4,5}
____ ( ____ input[], int_size)
{
for (int i = 0; i < ____ ; i++)
{
vnumber. ____ (( ____ )input[ ____ ] – (int) ‘____ ‘);
}
}
If your code is correct the following code should show 12345 on the screen.
int main()
{
char input1[] = "12345";
long_number n1(input1, 5);
n1.print();
cin.get();
return 0;
}
Continuing with the code in Question 6, complete the following code which implements the operator+ so that the operator adds to long_numbers together.
friend long_number ____ (long_number lhs, ____ long_number& rhs)
{
int carry = ____ ;
int i = ____ (lhs. ____ (), rhs. ____ ())- ____ ;
for (;i>=0;I ____)
{
int sum = lhs.vnumber[i] + rhs.vnumber[i] + carry;
lhs.vnumber[i] = sum ____ 10;
carry = sum ____ 10;
}
if (carry > 0)
{
lhs.vnumber. ____ (lhs.vnumber. ____ (), ____ );
}
____ lhs;
}
Question 8
Use the long_number from the previous part to calculate the sum of these two numbers:
91942213363574161572522430563301811072406154908250
23067588207539346171171980310421047513778063246676
[Extra Credit - 15 points]
Submit your code for all the previous questions as one cpp file.
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
class long_number
{
vector<int> vnumber;
public:
long_number () //Default constructor. Initializes the long_number to 0
{
vnumber. push_back (0); //Pushes 0 in the vector vnumber which holds the number
}
void print() //Function to print the number
{
for (int n : vnumber ) //For each loop to iterate all vnumber vector elements
{
cout << n; //Printing each element
}
cout << endl;
}
long_number(char input[],int size) //Parameterized constructor to
create a long_number using string input
{
for(int i=0;i<size;i++) //Iterating the char array
and pushing each element into vnumber vector
{
vnumber.push_back((int)input[i]-(int)'0'); //Converts the char
character into respective digits by subtracting ASCII value of
zero
}
}
friend long_number operator +(long_number lhs,long_number const
&rhs); //Declaring the function overloading + operator
};
long_number operator +(long_number lhs,long_number const &rhs)
//Defining the operator overloading function
{
int carry=0;
int i=min(lhs.vnumber.size(),rhs.vnumber.size())-1; //Initializing
i to minimum length of the two numbers -1
for(;i>=0;i--) //Loop to carry out the addition
{
int sum=lhs.vnumber[i]+rhs.vnumber[i]+carry; //Adds the digits
present at i index of both numbers
lhs.vnumber[i]=sum%10; //Sets digit at i in lhs to sum%10 as we
need the last digit of sum only
carry=sum/10; //Sets carry to most significant digit in sum
}
if(carry>0) //Adds additional digit at most significant place if
carry is greater than 0
{
lhs.vnumber.insert(lhs.vnumber.begin(),carry); //Inserts the
remaining carry to the beginning of lhs
}
return lhs; //returning lhs
}
int main()
{ long_number n1; //Demonstrating the default constructor
n1.print(); //Zero will be printed here
long_number n2("12345",5); //Demonstrating parameterized
constructor
n2.print(); //12345 would be printed
long_number
n3("91942213363574161572522430563301811072406154908250",50);
long_number
n4("23067588207539346171171980310421047513778063246676",50);
n4=n3+n4; //Showing the operating overloading function
to add the given long numbers
n4.print(); //Printing the result of addition
cin.get();
return 0;
}
If you find any problem in the above code, let me know in
comments and I would reply ASAP
Question The following class (long_number) represents a number of any length. #include <iostream> #include <string> #include...