Bigint +运算符

发布于 2025-01-02 13:04:30 字数 2664 浏览 1 评论 0原文

我正在做一个 bigint 项目,我很困惑为什么我的加法运算符在测试用例上不能正常工作。

我排除了 .h 文件,因为它可能是不必要的。

bigint.cpp

#include "bigint.h"
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cassert>



bigint::bigint()
{                           //Default constructor that sets digit to ZERO
   for (int i = 0; i < MAX; i++) 
   {
     digits[i] = 0;
   }
 }


bigint::bigint(int n)
{

for(int i = 0; i < MAX; ++i)       //Sets the digit to ZERO
    digits[i] = 0; 

    for (int i = 0; n != 0 ; ++i) 
{
    digits[i] = (n % 10);       //
           n = n / 10;
}


}


bigint::bigint(const char new_digits[])  
{
int null = 0;
int temp = 0;

for(int i = 0; i < MAX; ++i)
{
    digits[i] = 0;
}

while(new_digits[null] != '\0')
    ++null;
    --null;
temp = null;

for(int j = 0; j < MAX && temp >= 0; ++j)
{
    digits[j] = new_digits[temp] - '0';
    temp -= 1;
}
}


bool bigint::operator==(const bigint& equal) const
{
int i = 0;

while(i < MAX)
{
    if(digits[i] != equal.digits[i])
    {
        return false;
    }

    ++i;
}
return true;
}


std::ostream& operator<<(std::ostream& output, const bigint& source)
{

int sub1 = MAX - 1; //subtracts 1 from the maximum size 

while(source.digits[sub1] == 0)
{
    --sub1;                            //EMPTY
}

while(sub1 > -1)
{
    output << source.digits[sub1]; 
    --sub1;
}

std::cout << std:: endl;

return output; 
}

std::istream& operator>>(std::istream& in, bigint& source)
{
char getdata[MAX];
char user_input;
int i = 0;



    in.get(user_input);
    while(!in.eof() && user_input != ';')
{
    in.get(user_input);
    source.digits[i] = user_input;
    ++i;
}

    source = bigint(getdata);

    return in;
}

char bigint::operator[](const int i)
{
return digits[i];
}

bigint bigint::operator+(const bigint rhs)
{
   bigint result;
    int i = 0;

    for( ; i < MAX; ++i) 
    {

        if((digits[i] + rhs.digits[i]) > 9)
        {
             digits[i+1] = digits[i+1] + 1 ;


        }

               result.digits[i] = (digits[i] + rhs.digits[i]);
               result.digits[i] = result.digits[i] % 10;
     }

    return result;




}

Main.cpp(测试用例)

int main()
{
          // Setup fixture
    bigint left("1");
    bigint right("9");
    bigint result;

    // Test 
    result = (left + right);

     Verify
   assert(left   == "1");
   assert(right  == "9");
   assert(result == "10");

}

在此测试用例中,程序在assert(result == "10"); 处中止;

但如果我有相同的测试用例,除了断言(结果== 10); 程序运行。

谁能说出为什么吗?

I'm doing a bigint project and I am stumped as to why my addition operator isn't working properly on a test case.

I'm excluding the .h file because its probably unnecessary.

bigint.cpp

#include "bigint.h"
#include<iostream>
#include<fstream>
#include<cstdlib>
#include<cassert>



bigint::bigint()
{                           //Default constructor that sets digit to ZERO
   for (int i = 0; i < MAX; i++) 
   {
     digits[i] = 0;
   }
 }


bigint::bigint(int n)
{

for(int i = 0; i < MAX; ++i)       //Sets the digit to ZERO
    digits[i] = 0; 

    for (int i = 0; n != 0 ; ++i) 
{
    digits[i] = (n % 10);       //
           n = n / 10;
}


}


bigint::bigint(const char new_digits[])  
{
int null = 0;
int temp = 0;

for(int i = 0; i < MAX; ++i)
{
    digits[i] = 0;
}

while(new_digits[null] != '\0')
    ++null;
    --null;
temp = null;

for(int j = 0; j < MAX && temp >= 0; ++j)
{
    digits[j] = new_digits[temp] - '0';
    temp -= 1;
}
}


bool bigint::operator==(const bigint& equal) const
{
int i = 0;

while(i < MAX)
{
    if(digits[i] != equal.digits[i])
    {
        return false;
    }

    ++i;
}
return true;
}


std::ostream& operator<<(std::ostream& output, const bigint& source)
{

int sub1 = MAX - 1; //subtracts 1 from the maximum size 

while(source.digits[sub1] == 0)
{
    --sub1;                            //EMPTY
}

while(sub1 > -1)
{
    output << source.digits[sub1]; 
    --sub1;
}

std::cout << std:: endl;

return output; 
}

std::istream& operator>>(std::istream& in, bigint& source)
{
char getdata[MAX];
char user_input;
int i = 0;



    in.get(user_input);
    while(!in.eof() && user_input != ';')
{
    in.get(user_input);
    source.digits[i] = user_input;
    ++i;
}

    source = bigint(getdata);

    return in;
}

char bigint::operator[](const int i)
{
return digits[i];
}

bigint bigint::operator+(const bigint rhs)
{
   bigint result;
    int i = 0;

    for( ; i < MAX; ++i) 
    {

        if((digits[i] + rhs.digits[i]) > 9)
        {
             digits[i+1] = digits[i+1] + 1 ;


        }

               result.digits[i] = (digits[i] + rhs.digits[i]);
               result.digits[i] = result.digits[i] % 10;
     }

    return result;




}

Main.cpp (Test case)

int main()
{
          // Setup fixture
    bigint left("1");
    bigint right("9");
    bigint result;

    // Test 
    result = (left + right);

     Verify
   assert(left   == "1");
   assert(right  == "9");
   assert(result == "10");

}

In this test case the program aborts at assert(result == "10");

but if I have the same test case except with assert(result == 10);
the program runs.

Can anyone say why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萤火眠眠 2025-01-09 13:04:30

首先,您应该实现赋值运算符 bigint::operator=(const bigint&)。

现在,在 operator+ 中,您正在更改左侧对象的内容,在这段代码中:

if((digits[i] + rhs.digits[i]) > 9)
{
     digits[i+1] = digits[i+1] + 1 ;
}

这不好。例如,如果您运行以下代码:

bigint x("5");
bigint y("6");

x+y;
x+y;

您最终的结果是 x 为 17。

接下来,您将传递 bigint::operator< 的 /code> 参数,您可能应该在其中传递引用 (&)。

最后,这里的缩进是恶意的:

while(new_digits[null] != '\0')
    ++null;
    --null;

这里的循环体中有什么?没错,不是第三行。请不要这样缩进代码,它会让小猫哭泣。至少,对小猫进行编程。

注意:我在这里没有看到任何动态内存分配代码,这意味着 digits 可能是一个静态大小的数组。如果你要这样做,请确保它足够大,并且要注意,如果超过它的大小,你就会破裂。

First off, you should implement bigint::operator=(const bigint&), the assignment operator.

Now, in operator+, you're altering the contents of the left-hand side object, in this code:

if((digits[i] + rhs.digits[i]) > 9)
{
     digits[i+1] = digits[i+1] + 1 ;
}

That's not good. For example, if you ran this code:

bigint x("5");
bigint y("6");

x+y;
x+y;

You would end up with x being 17.

Next, you're passing by value for the bigint::operator arguments where you should probably pass by reference (&).

Finally, your indentation here is actively malicious:

while(new_digits[null] != '\0')
    ++null;
    --null;

What is in the loop body here? That's right, not the third line. Please don't indent code like that, it makes kittens cry. Programming kittens, at least.

NB: I don't see any dynamic memory allocation code here, which means digits is probably a statically-sized array. Make sure it's big enough if you're going to do that and be aware you'll break if its size is exceeded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文