我正在为学校做一个项目,我需要创建一个bigint类,到目前为止它有4个要求。
1。)编写一个方法来编写一个每行最多打印80位的bigint。
2。)写一个比较两个bigint是否相等的方法。它应该返回一个布尔。
3。)编写一个方法将bigint初始化为你提供的[0,maxint]的int值。
4。)编写一个方法来将bigint初始化为char []。
我认为我有2和3是正确的,但是我在比较两个bigint时遇到了麻烦,我希望有人能够指导我如何将打印限制为每行80位数。
到目前为止,这是我的代码:
.h文件
class bigint
{
public:
bigint(); //default constructor
bool operator==(const bigint& num1);
bigint( int n);
bigint(char new_digits[]);
private:
int digit[MAX];
int digitb[MAX];
};
这是实施文件:
#include "bigint.h"
#include<cassert>
#include<iostream>
//default constructor
bigint::bigint()
{
for (int i = 0; i < MAX; i++)
{
digit[i] = 0;
}
}
bigint::bigint( int n )
{
int i = 0;
while(n > 0)
{
digit[i] = n % 10;
n = n /10;
++i;
break;
}
for(i; i< MAX; ++i)
digit[i] = 0;
}
bool bigint::operator==(const bigint& num1)
{
for(int i = 0; i < MAX; i++)
{
if (num1.digit == num1.digit)
return true;
}
return false;
}
bigint::bigint(char new_digit[])
{
int i = 0;
//Reads the characters of numbers until it is ended by the null symbol
while(new_digit[i] != '\0')
++i;
--i;
//Converts the characters into int values and puts them in the digit array
while( i >= 0)
{
digit[i] = new_digit[i] - '0';
--i;
}
}
}
int main()
{
#include<iostream>
using namespace std;
using PROJECT_1::bigint;
bigint a(0);
assert(a == 0);
}
顺便说一句,我不是想找到我刚刚开始一小时前做作业的答案。我整天都在努力工作,最后我放弃了并寻求帮助。
答案 0 :(得分:1)
operator==
内的循环内容不起作用。您正在做的是将digit
数组(技术上是指针)与num1
数组指针digit
进行比较。这将永远是真的。您应该将num1
中的每个索引与this->digit
中的相应索引进行比较。像这样的东西:
num1.digit
如您所见,我也将比较从等于改为不等。这是因为否则如果只有bool bigint::operator==(const bigint& num1)
{
for(int i = 0; i < MAX; i++)
{
if (digit[i] != num1.digit[i])
return false;
}
return true;
}
个对象中的第一个数字是相同的,那么在仅检查第一个数字后,使用equal将使函数返回bigint
。
答案 1 :(得分:1)
您的代码存在许多问题。最直接的一个
已经指出:==
运算符两边的表达式
是相同的,所以函数自然会返回true
。最多
用C ++编写这个函数的惯用方法是:
return std::equals(
std::begin( digit ), std::end( digit ), std::begin( num1.digit ) );
在专业代码中,我会考虑其他任何糟糕的编程。 在一项简单的任务中,由于其中一个目标可能不太明确 学习如何自己写这些东西,你可能不被允许 在这种情况下使用标准算法。我仍然会这样做 然而,基本方法是使用“迭代器”,而不是 索引:
int const* current = std::begin( digit );
int const* other = std::begin( num1.digit );
int const* end = std::end( digit );
while ( current != end && *current == *other ) {
++ current;
++ other;
}
return current == end;
至于其余的代码:
operator==
绝对应该是const
;否则,即使像myBigInt == 0
这样简单的事情也行不通。
事实上,operator==
应该是非会员。我喜欢
成员函数isEqual
,并且operator==
(和
operator!=
)称之为friend
,但它是完全有效的
选项也是。
不确定digitb
应该做什么。
您使用int
的构造函数与使用的构造函数不兼容
char[]
。您需要决定您的内部表示是否是
big-endian或little-endian。 Little-endian可能更容易
算术运算,但这意味着你必须处理
BigInt( char[] )
中的数字顺序相反。事实上,你开始吧
好像你要以相反的顺序处理字符,但是
你最终会在两个阵列上倒退,从不初始化结束
digit
,而不是改变顺序。 (你还需要检查一下
每个char
确实是一个数字 - 使用标准isdigit
功能)。
一般情况下,只要他们这样做,就应该使用标准功能
工作(你的任务允许)。在BigInt::BigInt( char[] )
,for
例如,new_digit + strlen( new_digit )
会给你一个
“迭代器”到'\0'
。 (使用迭代器去
倒退比前锋有点棘手,因为你不是
允许将它减少到数组的前面。类似的东西:
const const* source = new_digits + strlen( new_digits );
while ( source != new_digits ) {
-- source;
// ...
}
然而,效果很好。
答案 2 :(得分:0)
我觉得你的比较实现不正确(我可能错了,因为你没有说明比较的条件是什么,所以这是我的假设)因为:
您正在比较同一个传递的bigint中的两个数字,请参阅:
bool bigint::operator==(const bigint& num1)
{
for(int i = 0; i < MAX; i++)
{
if (num1.digit == num1.digit) // it'll be always true
return true;
}
return false;
}
因为这是作业,我没有提供精确的解决方案,但是对于方法,逐位比较并查看有关运算符重载的this,它应该有所帮助。阅读,尝试和学习。
对于第二个问题,你需要每行打印80位数,所以运行一个循环,从头到尾为所有字符,当循环计数器达到80(或79从0开始)时,输出换行符字符。这是一个解决方案。
下次提到要求更清楚。