c++ - 编写 HugeInteger 类

标签 c++ multiprecision

我是 C++ 的新手,这是我应该为作业做的。

创建一个 HugeInteger 类,它使用一个包含 40 个元素的数字数组来存储每个最大 40 位的整数。提供成员函数输入、输出、加减。为了比较 HugeInteger 对象,提供函数 isEqualTo、isNotEqualTo、isGreaterThan、isLessThan、isCreaterThanOrEqualTo 和 isLessThanOrEqualTo - 其中每一个都是一个“谓词”函数,如果两个 HugeInteger 之间的关系成立,则返回 true,如果关系不成立,则返回 false。此外,提供谓词函数 isZero。

加分点推荐:提供成员函数multiply、divide、modules。


我无法弄清楚如何比较类(class)中的两个对象。

我的 header 编码如下所示:

#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
};

我的 .cpp 代码如下所示:

#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;
}

我的主要编程代码如下所示:

#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;
}

每次我运行我的程序时,当它去比较两个对象时,它无论如何都会返回 true。我不知道我做错了什么或如何解决它。任何帮助是极大的赞赏!谢谢!

最佳答案

您正在创建一个新的本地 bool result在 for 循环的条件内,所以 result您返回的始终是设置为 true 的外部范围。

为了帮助防止这些类型的错误并使其更易于阅读,我建议始终在您的条件和循环周围提供大括号,即使在技术上不需要它们也是如此。

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;
}

编辑 - 进一步说明
在这些代码行中,您创建了一个名为 result 的新 bool 值。它只会存在,直到退出 if 语句的范围。如果您要添加大括号,您会看到范围结束的位置。因此,您设置了本地范围的 result为 false,但函数范围为 result仍然保持您在函数开始时给它的初始值 true。

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

如果我们用大括号写这些行,那么范围是什么就更清楚了:

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

编辑 2 - 编码实践建议
正如 jww 指出的那样在评论中,您的比较函数正在复制 HugeInteger每次你给其中一个打电话时上课。在不深入细节的情况下(您可以查找或阅读教科书以获取详细信息),最好传递 const &到一个对象,以便在调用该函数时不会创建该对象的第二个拷贝。尽可能将函数标记为 const 也是一种很好的做法。 const 函数表示它不会改变对象本身的状态。它没有副作用”。当我们谈论对象的线程和线程安全时,这一点很重要。

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;
}

关于c++ - 编写 HugeInteger 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29179496/

相关文章:

c++ - 将函数作为参数从派生类传递给父方法

c++ - 我有不同的类,我想用这些类中的对象创建一个 vector 并按值对其进行排序

c - 存储和打印大于 2^64 的整数值

c++ - 使用 boost 多精度库的问题

c++ - 将 boost::multi precision::mpq_rational 舍入到最接近的整数

C++ elusive segmentation 错误

c++ - 如何知道 MPI 请求的接收者和发送者

c++ - 有哪些数据库持久性设计模式?

c++ - 使用 Boost mpfr_float 创建可变精度的数学常量,例如 pi 或 e