写作HugeInteger课程



我是C++的新手,这就是我应该为赋值所做的。

创建一个类HugeInteger,该类使用由数字组成的40元素数组来存储每个数字多达40位的整数。提供成员函数的输入、输出、加法和减法。为了比较HugeInteger对象,请提供函数isEqualTo、isNotEqualTo,isGreaterThan、isLessThan、isCreaterHanOrEqualTo和isLessThanOrEqualTo-这些函数都是一个"谓词"函数,如果两个HugeIntegger之间的关系成立,则只返回true,如果关系不成立,则返回false。此外,提供的谓词函数是Zero。

关于额外点的建议:提供成员函数乘法、除法和模数。


我很难弄清楚如何比较班上的两个对象。

我的标题编码如下:

#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 << "nnmyHugeInteger1:n" << endl;
    myHugeInteger1.output();
    cout << "nnmyHugeInteger2: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?nn";
    cout << myHugeInteger0.isZero() << endl << endl;
    cout << "Are all of the elements in myHugeInteger1 equal to zero?nn";
    cout << myHugeInteger1.isZero() << endl << endl;
    cout << "Are all of the elements in myHugeInteger2 equal to zero?nn";
    cout << myHugeInteger2.isZero() << endl << endl;
    //intro to adding the objects
    cout << "nn" << 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 equalsnn";
    myHugeInteger3.output();
    cout << endl;
    myHugeInteger3 = myHugeInteger0.add(myHugeInteger2);
    cout << "nThe sum of myHugeInteger0 plus myHugeInteger2 equalsnn";
    myHugeInteger3.output();
    cout << endl;
    myHugeInteger3 = myHugeInteger1.add(myHugeInteger2);
    cout << "nThe sum of myHugeInteger1 plus myHugeInteger2 equalsnn";
    myHugeInteger3.output();
    //intro to subtracting the objects
    cout << "nn" << 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 equalsnn";
    myHugeInteger3.output();
    cout << endl;
    myHugeInteger3 = myHugeInteger1.subtract(myHugeInteger0);
    cout << "nThe difference of myHugeInteger1 minus myHugeInteger0 equalsnn";
    myHugeInteger3.output();
    cout << endl;
    myHugeInteger3 = myHugeInteger0.subtract(myHugeInteger2);
    cout << "nThe difference of myHugeInteger0 minus myHugeInteger2 equalsnn";
    myHugeInteger3.output();
    cout << endl;
    myHugeInteger3 = myHugeInteger2.subtract(myHugeInteger0);
    cout << "nThe difference of myHugeInteger2 minus myHugeInteger0 equalsnn";
    myHugeInteger3.output();
    cout << endl;
    myHugeInteger3 = myHugeInteger1.subtract(myHugeInteger2);
    cout << "nThe difference of myHugeInteger1 minus myHugeInteger2 equalsnn";
    myHugeInteger3.output();
    cout << endl;
    myHugeInteger3 = myHugeInteger2.subtract(myHugeInteger1);
    cout << "nThe difference of myHugeInteger2 minus myHugeInteger1 equalsnn";
    myHugeInteger3.output();
    cout << endl;
    //intro to multipling the objects
    cout << "nn" << 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 equalsnn";
    myHugeInteger3.output();
    cout << endl;
    myHugeInteger3 = myHugeInteger0.multiply(myHugeInteger2);
    cout << "nThe product of myHugeInteger0 times myHugeInteger2 equalsnn";
    myHugeInteger3.output();
    cout << endl;
    myHugeInteger3 = myHugeInteger1.multiply(myHugeInteger2);
    cout << "nThe product of myHugeInteger1 times myHugeInteger2 equalsnn";
    myHugeInteger3.output();
    cout << endl;
    //intro to dividing the objects
    cout << "nn" << 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 equalsnn";
    myHugeInteger3.output();
    cout << endl;
    myHugeInteger3 = myHugeInteger0.divide(myHugeInteger2);
    cout << "nThe dividen of myHugeInteger0 divided by myHugeInteger2 equalsnn";
    myHugeInteger3.output();
    cout << endl;
    myHugeInteger3 = myHugeInteger1.divide(myHugeInteger2);
    cout << "nThe dividen of myHugeInteger1 divided by myHugeInteger2 equalsnn";
    myHugeInteger3.output();
    cout << endl;
    myHugeInteger3 = myHugeInteger2.divide(myHugeInteger1);
    cout << "nThe dividen of myHugeInteger2 divided by myHugeInteger1 equalsnn";
    myHugeInteger3.output();
    cout << endl;
    //intro to comparing objects
    cout << "nn" << 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? nn";
    cout << myHugeInteger0.isEqual(myHugeInteger1) << endl << endl;
    cout << "Is myHugeInteger0 equal to myHugeInteger2? nn";
    cout << myHugeInteger0.isEqual(myHugeInteger2) << endl << endl;
    cout << "Is myHugeInteger1 equal to myHugeInteger2? nn";
    cout << myHugeInteger1.isEqual(myHugeInteger2) << endl << endl;
    //see if the objects are not equal
    cout << "Is myHugeInteger0 not equal to myHugeInteger1? nn";
    cout << myHugeInteger0.isNotEqual(myHugeInteger1) << endl << endl;
    cout << "Is myHugeInteger0 not equal to myHugeInteger2? nn";
    cout << myHugeInteger0.isNotEqual(myHugeInteger2) << endl << endl;
    cout << "Is myHugeInteger1 not equal to myHugeInteger2? nn";
    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。我不知道我做错了什么,也不知道如何解决。任何帮助都将不胜感激!非常感谢。

您正在for循环的条件中创建一个新的本地bool result,因此您返回的result始终是设置为true的outerscoped。

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

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也是一种很好的做法。常量函数表示它不会改变对象本身的状态。它没有"副作用"。当我们谈论线程和对象的线程安全性时,这一点很重要。

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

相关内容

  • 没有找到相关文章

最新更新