I’m attempting to make a class that holds a (in theory) infinite amount of digits using c-strings, since ints have a limit. I am having a lot of trouble with multiplication and have begun to confuse myself. I am a student, so any help and mistakes I have not caught are very appreciated. I’ve been playing around with just the multiplication for 4 hours now.
The current problem is that I am calculating the results right (like if you try x = 12 and y = 4, I will only get 8 from it, then the final answer becomes 7 which is odd) but it isn’t storing properly.
definition:
MyInt operator* (const MyInt& x, const MyInt& y)
{
MyInt steps[x.numDigits - 1]; // Create an array of MyInts
int carry = 0; // Holds the 'carry the one'
int result, xInt, yInt; // For adding old-school
// Add each digit separately.
for (int i = 0; i < x.numDigits; i++)
{
steps[i].numDigits = y.numDigits; // set the numDigits to y's
// Resize the array to the size of numDigits
steps[i].Resize(steps[i].numDigits);
cout << "x.numDigits = " << x.numDigits << '\n'; // DELETE THESE
cout << "numDigits = " << steps[i].numDigits << '\n';
// Figure out xInt's value
xInt = C2I(x.myNumber[x.numDigits - i - 1]);
// Now multiply xInt by each digit of y
for (int j = 1; j <= y.numDigits; j++)
{
// yInt's value for this run through
yInt = C2I(y.myNumber[y.numDigits - j]);
// Answer is xInt * yInt + the remainder
result = ((xInt * yInt) + carry);
carry = 0; // Reset carry to zero
// If the result is 10 or higher, carry the excess
if (result > 9)
{
carry = result / 10;
result = result % 10;
}
// Assign result to the appropriate slot in the new number
steps[i].myNumber[(steps[i].numDigits - j)] = I2C(result);
cout << "ASSIGNED " << steps[i].myNumber[steps[i].numDigits - j] //DELETE THESE
<< " TO SLOT " << (steps[i].numDigits - j)
<< " with a rem = " << carry << '\n';
}
cout << "YOU GOT OUT OF THE J FOR LOOP\n";
// If carry wasn't reset to 0, that means the loop ended.
// This means there is a # that cannot fit in the current
// array size. We must resize, and then assign
// the extra characters into the array.
if (carry > 0)
{
int carryCopy = carry; // Copy of n for counting numDigits
int carryCount = 0; // Counts up how many digits are in carry
while(carryCopy > 0) // Figure out how many #'s there are
{
carryCopy = carryCopy / 10;
carryCount++;
}
// Figure out the new size
steps[i].numDigits = steps[i].numDigits + carryCount;
// Resize to new size
steps[i].Resize(steps[i].numDigits + carryCount);
// Copy in the new digits
for (int k = carryCount-1; k >= 0; k--)
{
steps[i].myNumber[k] = I2C(carry % 10);
carry = carry / 10;
}
}
}
cout << "What you have so far is " << steps[0] << "\n"; // DELETE
cout << "YOU GOT TO THE ADDING PART\n"; // DELETE
MyInt r = 0; // Create MyInt for total result
// Add up all of the arrays in steps[] into r
for (int l = 0; l < x.numDigits - 1; l++)
r = r + steps[l];
return r; // Result
}
Header file
#include <iostream>// for ostream, istream
using namespace std;
class MyInt
{
// these overload starters are declared as friend functions
friend MyInt operator+ (const MyInt& x, const MyInt& y);
friend MyInt operator* (const MyInt& x, const MyInt& y);
friend bool operator< (const MyInt& x, const MyInt& y);
friend bool operator> (const MyInt& x, const MyInt& y);
friend bool operator<= (const MyInt& x, const MyInt& y);
friend bool operator>= (const MyInt& x, const MyInt& y);
friend bool operator== (const MyInt& x, const MyInt& y);
friend bool operator!= (const MyInt& x, const MyInt& y);
friend ostream& operator<< (ostream& s, const MyInt& n);
friend istream& operator>> (istream& s, MyInt& n);
public:
MyInt(int n = 0); // first constructor
MyInt(const char * n); // second constructor
~MyInt(); // Destructor
MyInt(const MyInt & n); // Copy Constructor
MyInt& operator= (const MyInt & n); // Assignment operator
// be sure to add in the second constructor, and the user-defined
// versions of destructor, copy constructor, and assignment operator
private:
// member data (suggested: use a dynamic array to store the digits)
unsigned int numDigits; // The number of digits in myInt
char * myNumber; // Pointer to dynamic array of digits
void Resize(unsigned int newSize); // Resize array
};
and the main program I am using to test:
int main()
{
// demonstrate behavior of the two constructors and the << overload
MyInt x(12345), y("9876543210123456789"), r1(-1000), r2 = "14H67", r3;
char answer;
cout << "Initial values: \nx = " << x << "\ny = " << y
<< "\nr1 = " << r1 << "\nr2 = " << r2 << "\nr3 = " << r3 << "\n\n";
// demonstrate >> overload
cout << "Enter first number: ";
cin >> x;
cout << "Enter second number: ";
cin >> y;
cout << "You entered:\n";
cout << " x = " << x << '\n';
cout << " y = " << y << '\n';
// demonstrate assignment =
cout << "Assigning r1 = y ...\n";
r1 = y;
cout << " r1 = " << r1 << '\n';
// demonstrate comparison overloads
if (x < y) cout << "(x < y) is TRUE\n";
if (x > y) cout << "(x > y) is TRUE\n";
if (x <= y) cout << "(x <= y) is TRUE\n";
if (x >= y) cout << "(x >= y) is TRUE\n";
if (x == y) cout << "(x == y) is TRUE\n";
if (x != y) cout << "(x != y) is TRUE\n";
// demonstrating + and * overloads
r1 = x + y;
cout << "The sum (x + y) = " << r1 << '\n';
r2 = x * y;
cout << "The product (x * y) = " << r2 << "\n\n";
cout << "The sum (x + 12345) = " << x + 12345 << '\n';
cout << "The product (y * 98765) = " << y * 98765 << '\n';
}
You’re doing all the multiplication first, then the adding. This is inefficient and makes your code more complicated.
Instead, you should add the results together as you compute each multiply stage.
For example, instead of:
do this:
This gets rid of the need for the
stepsarray (Sin my example).Also, consider storing the digits in your array least-significant-digit first. This allows you to replace the
* 10^Nsteps with index shifts.Where the
()part is the digits shifted past by the indexing ofR. This technique also makes it easier to add or remove most-significant-digits since they’re at the end of the array and not the beginning.