我是C ++的新手,这就是我想为作业做的事情。
创建一个HugeInteger类,它使用40个元素的数字数组来存储每个大小为40位的整数。提供成员函数输入,输出,加减。为了比较HugeInteger对象,提供函数isEqualTo,isNotEqualTo,isGreaterThan,isLessThan,isCreaterThanOrEqualTo和isLessThanOrEqualTo - 这些函数中的每一个都是"谓词"如果两个HugeIntegers之间的关系成立则返回true的函数,如果关系不成立则返回false。另外,提供的谓词函数是零。
关于额外点的建议:提供成员函数乘法,除法和模数。
我无法弄清楚如何比较班上的两个对象。
我的标题编码如下:
#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。我无法弄清楚我做错了什么或如何解决它。任何帮助是极大的赞赏!谢谢!
答案 0 :(得分:1)
您正在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也是一种好习惯。一个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;
}