I have included all the files and information below. Can someone please help me
ID: 3810334 • Letter: I
Question
I have included all the files and information below. Can someone please help me with this? Please fill in the parts where it says code needed here. Please and thanks for any and all help!!!!!
You are to complete the implementation of the Natural class. This is a class that allows a user to work with nonnegative integers with an arbitrary number of digits. It does this by storing such a number as a string of digits. For example, the number 2759 is stored as the string "9572". Note that the digits are stored in order of significance; that is, the digit that goes with 10 to the power i is stored in position i - 1 in the array of characters constituting the string.
In the associated folder, you'll find the following:
(1) the specification (header file) for the Natural class;
(2) a template for the implementation (.cpp file) for the class;
(3) the file natural_test.cpp, which contains a program to test the operations of Natural.
You are to complete the implementation template.
(template where it says coed neede here that is where I need the help)
#include "natural.h"
#include <string>
#include <iostream>
#include <cmath>
using namespace std;
Natural::Natural(string digits)
{
dig = reverse(digits);
len = digits.length();
//Check for insignificant zeros.
while (len > 1 && dig[len - 1] == '0')
{
dig.erase(len - 1, 1);
len--;
}
}
Natural::Natural()
{
dig = "0";
len = 1;
}
string Natural::string_of_digits()
{
//Your code goes here.
}
int Natural::digit(int k)
{
//Your code goes here.
}
int Natural::cslog()
{
return len;
}
void Natural::operator+(Natural n2)
{
int* temp; //base addresses for dynamically allocated array
int carry = 0; //used for the "carry" digit
int i;
int maxlen = max(len,n2.len) + 1;
//Dynamically allocate an array temp to hold the digits of the sum.
//Initially, make temp[i] the ith digit of the left operand
//for 0 <= i < len, and 0 for len <= i < maxlen.
//Note that the sum could have as many as maxlen digits.
temp = new int[maxlen];
for (i = 0; i < len; i++)
temp[i] = dig[i] - 48; //Recall that 48 is the ASCII code for 0.
for (i = len; i < maxlen; i++)
temp[i] = 0;
//Compute the sum by adding the digits of the right operand
//to the digits in temp, carrying a 1 if necessary.
//Hint: the ith digit of n2 is n2.dig[i] - 48.
for (i = 0; i < n2.len; i++)
{
//Your code goes here.
}
temp[n2.len] = temp[n2.len] + carry;
//Compute the number of significant digits in the sum.
if (temp[maxlen - 1] == 0)
len = maxlen - 1;
else
len = maxlen;
//Convert the sum to a string, and store it in dig.
dig.clear();
for (i = 0; i < len; i++)
dig.append(1,temp[i] + 48);
}
void operator*(Natural n2)
{
//Implementation of this operation is optional.
}
(Header file)
#include <string>
using namespace std;
class Natural
{ //A class for "natural numbers" - that is, nonnegative integers.
//Such numbers are permitted to have any number of digits.
public:
Natural(string digits);
//Constructor. Creates a natural number using digits.
Natural(); //Default constructor. Creates the natural number 0.
string string_of_digits();
//n.string_of_digits() returns n as a string
int digit(int k);
//n.digit(k) returns the kth digit of n;
//that is, the coefficient of 10^k.
int cslog();
//n.cslog() returns the number of (significant) digits in n.
void operator+(Natural n2);
//n1 + n2 makes n1 the sum of n1 and n2.
void operator*(Natural n2);
//n1 * n2 makes n1 the product of n1 and n2.
private:
string dig;
int len;
string reverse(string s)
{
string result;
int len = s.length();
for (int i = 0; i < len; i++)
result.append(1,s[len - 1 - i]);
return result;
}
};
(The coed used for testing the progam)
#include <iostream>
#include <string>
#include "natural.h"
using namespace std;
int main()
{
Natural first, second, third;
string digits_of_number;
int i, k;
cout << "This program tests the Natural class." << endl;
cout << "Enter a nonnegative integer: ";
cin >> digits_of_number;
first = Natural(digits_of_number);
cout << "Enter another nonnegative integer: ";
cin >> digits_of_number;
second = Natural(digits_of_number);
cout << endl;
cout << "Your first number is: " << first.string_of_digits() << endl;
cout << "Your second number is: " << second.string_of_digits() << endl;
k = first.cslog();
cout << "Your first number has " << k << " digits." << endl;
k = second.cslog();
cout << "Enter an integer i between 0 and " << k - 1 << ": ";
cin >> i;
cout << "The digit that goes with 10 to the power " << i;
cout << " in your second number is: " << second.digit(i) << endl;
third = first;
third + second;
cout << "The sum of the two numbers you have entered is: " << endl;
cout << third.string_of_digits() << endl;
cout << endl;
system("pause");
return 0;
}
Explanation / Answer
Here is the Natural.cpp code for you:
#include "natural.h"
#include <string>
#include <iostream>
#include <cmath>
using namespace std;
Natural::Natural(string digits)
{
dig = reverse(digits);
len = digits.length();
//Check for insignificant zeros.
while (len > 1 && dig[len - 1] == '0')
{
dig.erase(len - 1, 1);
len--;
}
}
Natural::Natural()
{
dig = "0";
len = 1;
}
string Natural::string_of_digits()
{
//Your code goes here.
return dig;
}
int Natural::digit(int k)
{
//Your code goes here.
return dig[k] - '0';
}
int Natural::cslog()
{
return len;
}
void Natural::operator+(Natural n2)
{
int* temp; //base addresses for dynamically allocated array
int carry = 0; //used for the "carry" digit
int i;
int maxlen = max(len,n2.len) + 1;
//Dynamically allocate an array temp to hold the digits of the sum.
//Initially, make temp[i] the ith digit of the left operand
//for 0 <= i < len, and 0 for len <= i < maxlen.
//Note that the sum could have as many as maxlen digits.
temp = new int[maxlen];
for (i = 0; i < len; i++)
temp[i] = dig[i] - 48; //Recall that 48 is the ASCII code for 0.
for (i = len; i < maxlen; i++)
temp[i] = 0;
//Compute the sum by adding the digits of the right operand
//to the digits in temp, carrying a 1 if necessary.
//Hint: the ith digit of n2 is n2.dig[i] - 48.
for (i = 0; i < n2.len; i++)
{
//Your code goes here.
}
temp[n2.len] = temp[n2.len] + carry;
//Compute the number of significant digits in the sum.
if (temp[maxlen - 1] == 0)
len = maxlen - 1;
else
len = maxlen;
//Convert the sum to a string, and store it in dig.
dig.clear();
for (i = 0; i < len; i++)
dig.append(1,temp[i] + 48);
}
void operator*(Natural n2)
{
//Implementation of this operation is optional.
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.