Bigint +operator
Bigint +operator
我正在做一个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] != ' ')
++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");
}
在这个测试用例中,程序在断言时中止(结果=="10");
但是如果我有相同的测试用例,除了断言(结果==10);程序运行。
有人能说出原因吗?
首先,您应该实现赋值运算符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
参数的值,其中您可能应该传递引用(&
)。
最后,这里的缩进是恶意的:
while(new_digits[null] != ' ')
++null;
--null;
这里的循环体是什么?没错,不是第三行。请不要那样缩进代码,这会让小猫哭的。至少在给小猫编程。
注意:我在这里没有看到任何动态内存分配代码,这意味着digits
可能是一个静态大小的数组。如果你要这样做,请确保它足够大,并注意如果超过它的大小,你会摔坏的。
相关文章:
- 为什么 std::optional::operator=(U&&) 要求你是非标量类型?
- 'operator='已弃用:改用 QDir::setPath()
- 过载'operator new'如何导致无限循环?
- 与'operator='不匹配(操作数类型'String'且"void")
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 处理"no operator found"
- 如何编写 operator= 用于使用虚拟方法与非平凡成员的匿名联合
- 运算符重载:"operator+"必须采用零个或一个参数
- 使用 operator() 扩展 Eigen::EigenBase
- 错误消息:使用"string* +="后"no match for 'operator+='"
- 有没有办法修改'operator->',以便'z->im'返回复数的虚部
- "operator +="行为异常的定义
- C++ "error: use of overloaded operator '*' is ambiguous"似乎只有一场比赛
- 错误:二进制'operator*' 'float'和'float[0]'类型的操作数无效
- 在 Eclipse: "error: no match for 'operator='" 中获取错误消息
- 为什么 operator() 处的指针成员不起作用?
- 为什么 string_view::operator== 按值接受参数
- "operator()"在重载运算符方法中是什么意思,在priority_queue(STL)中用作C++中的比较器?
- Valarray 切片数组与 'operator*' 不匹配
- Bigint +operator