Writing HugeInteger class

牧云@^-^@ 提交于 2019-12-08 11:17:08

问题


I am new to C++ and this is what I am suppose to do for an assignment.

Create a class HugeInteger that uses a 40-element array of digits to store integers as large as 40 digits each. Provide member functions input, output, add and subtract. For comparing HugeInteger objects, provide functions isEqualTo, isNotEqualTo, isGreaterThan, isLessThan, isCreaterThanOrEqualTo and isLessThanOrEqualTo - each of these is a "predicate" function that simply returns true if the relationship holds between the two HugeIntegers and returns false if the relationship does not hold. Also, provided predicate function isZero.

Recommendations for extra points: Provide member functions multiply, divide, and modulus.


I am having trouble figuring out how to compare two objects in my class.

My header coding looks like this:

#pragma once
#include <iostream>
using namespace std;

static const int MAXINTEGER = 40;

//HugeInteger Class
class HugeInteger
{
 public:
    HugeInteger(void); //constructor
    ~HugeInteger(void); //destructor

    void HugeInteger::input(int[MAXINTEGER]); //input array to internal array
    void HugeInteger::output(void); //write out array to screen

    bool isZero(void); //test to see if it is zero
    bool HugeInteger::isEqual(HugeInteger other); //test to see if the objects are equal
    bool HugeInteger::isNotEqual(HugeInteger other); //test to see if the objects are not equal
    bool HugeInteger::isGreaterThan(HugeInteger other); //test to see if one object is greater than the other
    bool HugeInteger::isLessThan(HugeInteger other); //test to see if one object is less than the other
    bool HugeInteger::isGreaterThanOrEqual(HugeInteger other); //test to see if one object is greater than or equal to the other
    bool HugeInteger::isLessThanOrEqual(HugeInteger other); //test to see if one obejct is less than or equal to the other

    HugeInteger HugeInteger::add(HugeInteger other); //adds two objects
    HugeInteger HugeInteger::subtract(HugeInteger other); //subtract two objects
    HugeInteger HugeInteger::multiply(HugeInteger other); //multiply two objects
    HugeInteger HugeInteger::divide(HugeInteger other); //divide two objcts

private:
    bool isPositive; //needed when we do subtraction
    int hugeIntergerOne[MAXINTEGER]; //internal array
};

My .cpp coding looks like this:

#include "HugeInteger.h"
#include <iostream>
using namespace std;

//HugeInteger Class Functions

HugeInteger::HugeInteger(void) //constructor
{
    //zero out internal array
    for (int i = MAXINTEGER - 1; i >= 0; i--)
        this ->hugeIntergerOne[i] = 0;
}

HugeInteger::~HugeInteger() //destructor
{
    //de-allocates internal array
}

void HugeInteger::input(int newArray[MAXINTEGER])
{
    //copies 'newArray' into internal array
    for (int index = MAXINTEGER - 1; index >= 0; index--)
        this->hugeIntergerOne[index] = newArray[index];
}

void HugeInteger::output()
{
    //outputs internal array to screen
    for (int index = 0; index < MAXINTEGER; index++)
        cout << this->hugeIntergerOne[index] << " ";
}

bool HugeInteger::isZero(void) //test zero function
{
    bool result = true;
    //test whether every element of internal array is zero
    for (int index = MAXINTEGER - 1; index >= 0; index--)
        if (this->hugeIntergerOne[index] != 0)
            result = false;
    return result;
}

bool HugeInteger::isEqual(HugeInteger other) //test equal function
{
    bool result = true;
    for (int i = 0; i < MAXINTEGER; i++)
        if (this->hugeIntergerOne[i] != other.hugeIntergerOne[i])
            bool result = false;
    return result;
}

bool HugeInteger::isNotEqual(HugeInteger other) //test not equal function
{
    bool result = true;
    for (int index = MAXINTEGER - 1; index >= 0; index--)
        if (this->hugeIntergerOne[index] == other.hugeIntergerOne[index])
            bool result = false;
    return result;
}

bool HugeInteger::isGreaterThan(HugeInteger other) //test greater than function
{
    bool result = false;
    for (int index = MAXINTEGER - 1; index >= 0; index--)
        if (this->hugeIntergerOne[index] > other.hugeIntergerOne[index])
            bool result = true;
    return result;
}

bool HugeInteger::isLessThan(HugeInteger other) //test less than function
{
    bool result = false;
    for (int index = MAXINTEGER - 1; index >= 0; index--)
        if (this->hugeIntergerOne[index] < other.hugeIntergerOne[index])
            bool result = true;
    return result;
}

bool HugeInteger::isGreaterThanOrEqual(HugeInteger other) //test greater than or equal function
{
    bool result = false;
    for (int index = MAXINTEGER - 1; index >= 0; index--)
        if (this->hugeIntergerOne[index] >= other.hugeIntergerOne[index])
            bool result = true;
    return result;
}

bool HugeInteger::isLessThanOrEqual(HugeInteger other) //test less than or equal function
{
    bool result = false;
    for (int index = MAXINTEGER - 1; index >= 0; index--)
        if (this->hugeIntergerOne[index] <= other.hugeIntergerOne[index])
            bool result = true;
    return result;
}

HugeInteger HugeInteger::add(HugeInteger other) //adds objects
{
    HugeInteger result;
    for (int i = 0; i < MAXINTEGER; i++)
    {
        result.hugeIntergerOne[i] = this -> hugeIntergerOne[i] + other.hugeIntergerOne[i];
    }
    return result;
}

HugeInteger HugeInteger::subtract(HugeInteger other) //subtracts objects
{
    HugeInteger result;
    for (int i = 0; i < MAXINTEGER; i++)
    {
        result.hugeIntergerOne[i] = this->hugeIntergerOne[i] - other.hugeIntergerOne[i];
    }
    return result;
}

HugeInteger HugeInteger::multiply(HugeInteger other) //multiplies objects
{
    HugeInteger result;
    for (int i = 0; i < MAXINTEGER; i++)
    {
        result.hugeIntergerOne[i] = this->hugeIntergerOne[i] * other.hugeIntergerOne[i];
    }
    return result;
}

HugeInteger HugeInteger::divide(HugeInteger other) //divides objects
{
    HugeInteger result;
    for (int i = 0; i < MAXINTEGER; i++)
    {
        result.hugeIntergerOne[i] = this->hugeIntergerOne[i]/other.hugeIntergerOne[i];
    }
    return result;
}

And my main programming code looks like this:

#include <iostream>
#include "HugeInteger.h"
using namespace std;

int main()
{
    //create three arrays
    int first[MAXINTEGER] = { 1, 2, 3, 4, 5, 6, 7, 8,
        9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
        33, 34, 35, 36, 37, 38, 39, 40 };
    int second[MAXINTEGER] = { 40, 39, 38, 37, 36, 35,
        34, 33, 32, 31, 30, 29, 28, 27, 26, 25, 24, 23,
        22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11,
        10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
    int zero[MAXINTEGER] = { 0 };

    //create objects
    HugeInteger myHugeInteger0;
    HugeInteger myHugeInteger1;
    HugeInteger myHugeInteger2;
    HugeInteger myHugeInteger3;

    //input arrays into objects
    myHugeInteger1.input(first);
    myHugeInteger2.input(second);
    myHugeInteger0.input(zero);

    //prints out the words true or false instead of a 1 or 0
    cout << boolalpha << endl;

    //opening statements
    cout << "Welcome!\n" << endl;
    cout << "We will be testing a bunch of different functions on class objects today.\n" << endl;
    cout << "I have created three class objects which are 40 element arrays.\n" << endl;

    system("pause");
    cout << "\n" << endl;

    //prints the elements in each object
    cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
    cout << "My three objecs are: \n" << endl;
    cout << "myHugeInteger0:\n" << endl;
    myHugeInteger0.output();
    cout << "\n\nmyHugeInteger1:\n" << endl;
    myHugeInteger1.output();
    cout << "\n\nmyHugeInteger2:\n" << endl;
    myHugeInteger2.output();
    cout << "\n" << endl;

    //intro to check if objecs are zero
    cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
    cout << "First, we will test to see if all of the elements in the arrays are equal to \nzero.\n" << endl;

    system("pause");
    cout << endl;

    //test if the all of the object elements are equal to zero
    cout << "Are all of the elements in myHugeInteger0 equal to zero?\n\n";
    cout << myHugeInteger0.isZero() << endl << endl;

    cout << "Are all of the elements in myHugeInteger1 equal to zero?\n\n";
    cout << myHugeInteger1.isZero() << endl << endl;

    cout << "Are all of the elements in myHugeInteger2 equal to zero?\n\n";
    cout << myHugeInteger2.isZero() << endl << endl;

    //intro to adding the objects
    cout << "\n\n" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
    cout << "Now we shall add the different arrays together.\n" << endl;

    system("pause");
    cout << endl;

    //add the different objects
    myHugeInteger3 = myHugeInteger0.add(myHugeInteger1);
    cout << "\nThe sum of myHugeInteger0 plus myHugeInteger1 equals\n\n";
    myHugeInteger3.output();
    cout << endl;

    myHugeInteger3 = myHugeInteger0.add(myHugeInteger2);
    cout << "\nThe sum of myHugeInteger0 plus myHugeInteger2 equals\n\n";
    myHugeInteger3.output();
    cout << endl;

    myHugeInteger3 = myHugeInteger1.add(myHugeInteger2);
    cout << "\nThe sum of myHugeInteger1 plus myHugeInteger2 equals\n\n";
    myHugeInteger3.output();

    //intro to subtracting the objects
    cout << "\n\n" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
    cout << "Now we shall subtract the different arrays.\n" << endl;

    system("pause");

    //subtract the different objects
    myHugeInteger3 = myHugeInteger0.subtract(myHugeInteger1);
    cout << "\nThe difference of myHugeInteger0 minus myHugeInteger1 equals\n\n";
    myHugeInteger3.output();
    cout << endl;

    myHugeInteger3 = myHugeInteger1.subtract(myHugeInteger0);
    cout << "\nThe difference of myHugeInteger1 minus myHugeInteger0 equals\n\n";
    myHugeInteger3.output();
    cout << endl;

    myHugeInteger3 = myHugeInteger0.subtract(myHugeInteger2);
    cout << "\nThe difference of myHugeInteger0 minus myHugeInteger2 equals\n\n";
    myHugeInteger3.output();
    cout << endl;

    myHugeInteger3 = myHugeInteger2.subtract(myHugeInteger0);
    cout << "\nThe difference of myHugeInteger2 minus myHugeInteger0 equals\n\n";
    myHugeInteger3.output();
    cout << endl;

    myHugeInteger3 = myHugeInteger1.subtract(myHugeInteger2);
    cout << "\nThe difference of myHugeInteger1 minus myHugeInteger2 equals\n\n";
    myHugeInteger3.output();
    cout << endl;

    myHugeInteger3 = myHugeInteger2.subtract(myHugeInteger1);
    cout << "\nThe difference of myHugeInteger2 minus myHugeInteger1 equals\n\n";
    myHugeInteger3.output();
    cout << endl;

    //intro to multipling the objects
    cout << "\n\n" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
    cout << "Now we shall multiply the different arrays together.\n" << endl;

    system("pause");

    //multiply the different objects
    myHugeInteger3 = myHugeInteger0.multiply(myHugeInteger1);
    cout << "\nThe product of myHugeInteger0 times myHugeInteger1 equals\n\n";
    myHugeInteger3.output();
    cout << endl;

    myHugeInteger3 = myHugeInteger0.multiply(myHugeInteger2);
    cout << "\nThe product of myHugeInteger0 times myHugeInteger2 equals\n\n";
    myHugeInteger3.output();
    cout << endl;

    myHugeInteger3 = myHugeInteger1.multiply(myHugeInteger2);
    cout << "\nThe product of myHugeInteger1 times myHugeInteger2 equals\n\n";
    myHugeInteger3.output();
    cout << endl;

    //intro to dividing the objects
    cout << "\n\n" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
    cout << "Now we shall divide the different arrays.\n" << endl;

    system("pause");

    //divide the different objects
    myHugeInteger3 = myHugeInteger0.divide(myHugeInteger1);
    cout << "\nThe dividen of myHugeInteger0 divided by myHugeInteger1 equals\n\n";
    myHugeInteger3.output();
    cout << endl;

    myHugeInteger3 = myHugeInteger0.divide(myHugeInteger2);
    cout << "\nThe dividen of myHugeInteger0 divided by myHugeInteger2 equals\n\n";
    myHugeInteger3.output();
    cout << endl;

    myHugeInteger3 = myHugeInteger1.divide(myHugeInteger2);
    cout << "\nThe dividen of myHugeInteger1 divided by myHugeInteger2 equals\n\n";
    myHugeInteger3.output();
    cout << endl;

    myHugeInteger3 = myHugeInteger2.divide(myHugeInteger1);
    cout << "\nThe dividen of myHugeInteger2 divided by myHugeInteger1 equals\n\n";
    myHugeInteger3.output();
    cout << endl;

    //intro to comparing objects
    cout << "\n\n" << endl;
    cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
    cout << "Now we shall compare the different arrays.\n" << endl;

    system("pause");
    cout << endl;

    //see if the objecs are equal
    cout << "Is myHugeInteger0 equal to myHugeInteger1? \n\n";
    cout << myHugeInteger0.isEqual(myHugeInteger1) << endl << endl;
    cout << "Is myHugeInteger0 equal to myHugeInteger2? \n\n";
    cout << myHugeInteger0.isEqual(myHugeInteger2) << endl << endl;
    cout << "Is myHugeInteger1 equal to myHugeInteger2? \n\n";
    cout << myHugeInteger1.isEqual(myHugeInteger2) << endl << endl;

    //see if the objects are not equal
    cout << "Is myHugeInteger0 not equal to myHugeInteger1? \n\n";
    cout << myHugeInteger0.isNotEqual(myHugeInteger1) << endl << endl;
    cout << "Is myHugeInteger0 not equal to myHugeInteger2? \n\n";
    cout << myHugeInteger0.isNotEqual(myHugeInteger2) << endl << endl;
    cout << "Is myHugeInteger1 not equal to myHugeInteger2? \n\n";
    cout << myHugeInteger1.isNotEqual(myHugeInteger2) << endl << endl;

    //see if the objects are greater than

    cout << "\nThat is all for today! Thank you for watching!\n" << endl;
    system("pause");
    return 0;
}

Every time I run my program when it goes to compare the two objects it returns true no matter what. I can not figure out what I am doing wrong or how to fix it. Any help is greatly appreciated! Thank You!


回答1:


You are creating a new local bool result inside the condition in your for loop so the result you return is always your outerscoped one that is set to true.

To help prevent these types of errors and make it easier to read, I would recommend always providing braces around your conditions and loops even if technically they are not required.

bool HugeInteger::isEqual(HugeInteger other) //test equal function
{
    bool result = true;
    for (int i = 0; i < MAXINTEGER; i++)
    {
        if (this->hugeIntergerOne[i] != other.hugeIntergerOne[i])
        {
            result = false; // You had: bool result = false;
        }
    }
    return result;
}

Edit - further explanation
In these lines of code you create a new bool called result which will only exist until the scope of the if statement is exited. If you were to add braces you would see where the scope ends. Because of this, you set the local scoped result to false, but the function scoped result still maintains the initial value of true that you gave it at the function beginning.

if (this->hugeIntergerOne[i] != other.hugeIntergerOne[i])
    bool result = false;

If we write those lines with the braces it is more clear what the scope is:

if (this->hugeIntergerOne[i] != other.hugeIntergerOne[i])
{
    bool result = false; // This is only in scope within the surrounding braces
}

Edit 2 - Coding practice suggestion
As pointed out by jww in the comments, your comparison functions are copying the HugeInteger class each time you call one of them. Without getting into the details (you can look this up or read your text book for the details), it is best practice to pass a const & to an object so that a second copy of the object is not made when calling the function. It is also good practice to mark functions as const whenever possible. A function that is const indicates that it will not change the state of object itself. It has no "side effects". This is important when we talk about threading and thread safety of objects.

bool HugeInteger::isEqual(const HugeInteger& other) const //test equal function
{
    bool result = true;
    for (int i = 0; i < MAXINTEGER; i++)
    {
        if (this->hugeIntergerOne[i] != other.hugeIntergerOne[i])
        {
            result = false; // You had: bool result = false;
        }
    }
    return result;
}


来源:https://stackoverflow.com/questions/29179496/writing-hugeinteger-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!