Question: RUSH RUSH RUSH************************************* C++**********************************test12 Upload BigNumber.cpp and BigNumber.h, and your main function. Upload only these files. Add a new constructor the BigNumber class. The constructors
RUSH RUSH RUSH************************************* C++**********************************test12
Upload BigNumber.cpp and BigNumber.h, and your main function.
Upload only these files.
Add a new constructor the BigNumber class. The constructors one parameter is a string. Use the isdigit function to verify that the elements of the string are char versions of digits. To convert a char version of a digit to an int, subtract 48 from the character. What do you do about elements of the string that are not char versions of digits? You decide.
For example,
char c = 3
int i = c 48;
converts the character 3 to the integer 3.
Be careful that you do not reverse the order of the digits when you convert from a string representation to a vector
For example, in the string 123,
1 is at position 0
2 is at position 1
3 is at position 2
The constructor, when it copies the string, 123, to the vector,
puts a 1 at position 2
puts a 2 at position 1
puts a 3 at position 0
===============================source.cpp
#include
#include
#include "dynamicArray.h"
#include
using namespace std;
dynamicArray mul(const dynamicArray& n1, const dynamicArray& n2)
{
int size1 = n1.size();
int size2 = n2.size();
dynamicArray p12(size1 + size2);
for (int i1 = 0; i1 < size1; ++i1)
{
int ni1 = n1.get(i1);
for (int i2 = 0; i2 < size2; ++i2)
{
int pi1i2 = ni1 * n2.get(i2) + p12.get(i1 + i2);
if (pi1i2 > 9)
{
int overflow = p12.get(i1 + i2 + 1);
p12.put(i1 + i2 + 1, overflow + pi1i2 / 10);
pi1i2 = pi1i2 % 10;
}
p12.put(i1 + i2, pi1i2);
}
}
return p12;
}
dynamicArray add(const dynamicArray& n1, const dynamicArray& n2)
{
int size1 = n1.size();
int size2 = n2.size();
int size = min(size1, size2);
dynamicArray s12(max(size1, size2) + 1);
for (int i = 0; i < size; ++i)
{
int si = n1.get(i) + n2.get(i) + s12.get(i);
if (si > 9)
{
int carry = s12.get(i + 1);
s12.put(i + 1, carry + si / 10);
si = si % 10;
}
s12.put(i, si);
}
for (int i = size; i < size1; ++i)
{
int si = n1.get(i) + s12.get(i);
s12.put(i, si);
}
return s12;
}
dynamicArray sub(const dynamicArray& n1, const dynamicArray& n2)
{
int size1 = n1.size();
int size2 = n2.size();
int size = min(size1, size2);
dynamicArray d12(max(size1, size2));
for (int i = 0; i < size; ++i)
{
int di = n1.get(i) - n2.get(i) - d12.get(i);
if (di < 0)
{
int borrow = d12.get(i + 1);
d12.put(i + 1, borrow + 1);
di = di + 10;
}
d12.put(i, di);
}
for (int i = size; i < size1; ++i)
{
int di = n1.get(i) - d12.get(i);
if (di < 0)
{
int borrow = d12.get(i + 1);
d12.put(i + 1, borrow + 1);
di = di + 10;
}
d12.put(i, di);
}
for (int i = size; i < size2; ++i)
{
int di = 0 - d12.get(i);
if (di < 0)
{
int borrow = d12.get(i + 1);
d12.put(i + 1, borrow + 1);
di = di + 10;
}
d12.put(i, di);
}
return d12;
}
int main()
{
dynamicArray n1(3);
dynamicArray n2(3);
n1.put(0, 5);
n1.put(1, 3);
n1.put(2, 1);
n2.put(0, 1);
n2.put(1, 2);
n2.put(2, 8);
dynamicArray p12 = mul(n1, n2);
n1.show();
cout << " x ";
n2.show();
cout << " = ";
p12.show();
cout << endl;
p12 = mul(n2, n1);
n2.show();
cout << " x ";
n1.show();
cout << " = ";
p12.show();
cout << endl;
dynamicArray a12 = add(n1, n2);
n1.show();
cout << " + ";
n2.show();
cout << " = ";
a12.show();
cout << endl;
a12 = add(n2, n1);
n2.show();
cout << " + ";
n1.show();
cout << " = ";
a12.show();
cout << endl;
dynamicArray d12 = sub(n1, n2);
n1.show();
cout << " - ";
n2.show();
cout << " = ";
d12.show();
cout << endl;
d12 = sub(n2, n1);
n2.show();
cout << " - ";
n1.show();
cout << " = ";
d12.show();
cout << endl;
return 0;
}
==============================dynamicArray.h
#pragma once
class dynamicArray
{
public:
const dynamicArray& operator=(const dynamicArray& source);
dynamicArray(int new_size = 16);
dynamicArray(const dynamicArray& source);
~dynamicArray();
int get(int where) const;
int size() const;
void put(int where, int what);
void show() const;
private:
void copy(const dynamicArray& source);
int _size;
int *_elts;
};
============================dynamicArray.cpp
#include
#include
#include "dynamicArray.h"
using namespace std;
const dynamicArray& dynamicArray::operator=(const dynamicArray& source)
{
if (this != &source)
{
delete[] _elts;
copy(source);
}
return *this;
}
dynamicArray::dynamicArray(int new_size)
{
_size = new_size;
_elts = new int[_size];
for (int i = 0; i < _size; ++i)
_elts[i] = 0;
}
dynamicArray::dynamicArray(const dynamicArray& source)
{
copy(source);
}
dynamicArray::~dynamicArray()
{
delete[] _elts;
_elts = nullptr;
_size = -1;
}
void dynamicArray::copy(const dynamicArray& source)
{
_size = source._size;
_elts = new int[_size];
for (int i = 0; i < _size; ++i)
_elts[i] = source._elts[i];
}
int dynamicArray::get(int where) const
{
assert(0 <= where && where < _size);
return _elts[where];
}
int dynamicArray::size() const
{
return _size;
}
void dynamicArray::put(int where, int new_elt)
{
assert(0 <= where && where < _size);
_elts[where] = new_elt;
}
void dynamicArray::show() const
{
for (int i = _size - 1; i >= 0; --i)
cout << _elts[i];
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
