1

我正在为学校做一个项目,我需要创建一个 bigint 类,到目前为止它有 4 个要求。

1.) 编写一个方法来编写一个 bigint,每行最多打印 80 位数字。

2.) 编写一个方法来比较两个 bigint 是否相等。它应该返回一个布尔值。

3.) 编写一个方法来将 bigint 初始化为您提供的 int 值 [0, maxint]。

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

顺便说一句,我不想​​为我一小时前刚开始的​​作业找到答案。我整天都在为此工作,最后我屈服并寻求帮助。

4

3 回答 3

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应该做什么。

  • 您的构造函数 usingint与 using 的构造函数不兼容 char[]。您需要决定您的内部表示是大端还是小端。Little-endian 对于算术运算可能更容易,但这意味着您必须以相反的顺序处理BigInt( char[] ). 实际上,您一开始就好像要以相反的顺序处理字符,但最终您会在两个数组上倒退,从不初始化 的结尾digit,也不会更改顺序。 (您还需要使用标准函数检查每个char真的是数字。)isdigit

一般来说,只要它们完成工作(并且您的任务允许),您就应该使用标准函数。BigInt::BigInt( char[] )例如,innew_digit + strlen( new_digit )会给你一个“迭代器”到'\0'. (使用迭代器后退比前进有点棘手,因为你不能将它递减到数组的前面之外。像:

const const* source = new_digits + strlen( new_digits );
while ( source != new_digits ) {
    -- source;
    //  ...
}

但是,效果很好。

于 2012-01-30T09:17:48.473 回答
1

里面的循环内容operator==不起作用。您正在做的是将 的digit数组(从技术上讲是一个指针)num1与 . 的digit数组指针进行比较num1。这将永远是真的。您应该将 in 中的每个索引与 inthis->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;
}

如您所见,我还将比较从等于更改为不等于。这是因为否则,如果两个bigint对象中只有第一个数字相同,那么使用 equal 将使函数true仅在检查第一个数字后返回。

于 2012-01-30T06:34:03.070 回答
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;
}

因此,由于这是作业,我没有给出确切的解决方案,但对于方法,逐位比较并在运算符重载时查看内容,它应该会有所帮助。阅读、尝试和学习。

对于第二个问题,您需要每行打印 80 位数字,因此运行一个循环,从头到尾对所有字符进行循环,当循环计数器达到 80(或 79 是您从 0 开始)时,输出一个换行符。这是一种解决方案。

并且下次更清楚地提及要求。

于 2012-01-30T06:37:44.590 回答