为什么这段代码不能正常工作?我的意思是,为什么我在运行它时没有显示任何内容?

时间:2016-05-22 18:19:23

标签: c++ overloading

重载方法有什么问题吗?我认为它应该可以工作,但事实并非如此。


#include <iostream>
#include <string>
#include <vector>
using namespace std;

class BIGINT
{
    vector<int> big_number;
public:
    BIGINT(){}
    BIGINT(int);
    BIGINT(string);
    BIGINT operator+(const BIGINT&)const;
    BIGINT operator+(int)const;
    BIGINT& operator+=(const BIGINT&);
    BIGINT& operator==(const BIGINT&);
    vector<int> get_bn() { return big_number; }

    friend ostream& operator<<(ostream& out,const BIGINT&);
};

//构造

BIGINT::BIGINT(int n)
{
    vector<int> temp;
    while (n > 0)
    {
        int a;
        a = n % 10;
        n /= 10;
        temp.push_back(a);
    }
    for (int i = temp.size() - 1;i > 0;i--)
    {
        big_number.push_back(temp[i]);
    }
}

BIGINT::BIGINT(string sn)
{
    vector<char> temp;
    for (int i = 0;i < sn.size();i++)
    {
        temp.push_back(sn[i]);
    }
    for (int i = 0;i < temp.size();i++)
    {
        big_number.push_back(temp[i]-48);
    }
}

//重载operator +

BIGINT BIGINT::operator+(const BIGINT& bn) const
{
    BIGINT temp;
    int size;
    int k = 0;
    if (bn.big_number.size()>big_number.size()) size = big_number.size();
    else size = bn.big_number.size();
    for (int i = size - 1;i > 0;i--)
    {
        if(k)
        {
            if (big_number[i] + bn.big_number[i]>9)
            {
                temp.big_number.push_back(big_number[i] + bn.big_number[i] - 10 + 1);
                k = 1;
            }
            else
            {
                temp.big_number.push_back(big_number[i] + bn.big_number[i] + 1);
                k = 0;
            }
        }
        else
        {
            if(big_number[i]+bn.big_number[i]>9)
            {
                temp.big_number.push_back(big_number[i] + bn.big_number[i] - 10);
                k = 1;
            }
            else
            {
                temp.big_number.push_back(big_number[i] + bn.big_number[i]);
                k = 0;
            }
        }

    }
    return temp;
}

BIGINT BIGINT::operator+(int n)const
{
    BIGINT temp;
    int size;
    int k = 0;
    vector<int> temp_int;
    while (n > 0)
    {
        int a;
        a = n % 10;
        n /= 10;
        temp_int.push_back(a);
    }
    if (temp_int.size()>big_number.size()) size = big_number.size();
    else size = temp_int.size();
    for (int i = size - 1;i > 0;i--)
    {
        if (k)
        {
            if (big_number[i] + temp_int[i]>9)
            {
                temp.big_number.push_back(big_number[i] + temp_int[i] - 10 + 1);
                k = 1;
            }
            else
            {
                temp.big_number.push_back(big_number[i] + temp_int[i] + 1);
                k = 0;
            }
        }
        else
        {
            if (big_number[i] + temp_int[i]>9)
            {
                temp.big_number.push_back(big_number[i] + temp_int[i] - 10);
                k = 1;
            }
            else
            {
                temp.big_number.push_back(big_number[i] + temp_int[i]);
                k = 0;
            }
        }
    }
    return temp;
}


BIGINT operator+(int n, BIGINT bn)
{
    BIGINT temp;
    int size;
    int k = 0;
    vector<int> temp_int;
    while (n > 0)
    {
        int a;
        a = n % 10;
        n /= 10;
        temp_int.push_back(a);
    }
    if (temp_int.size()>bn.get_bn().size()) size = bn.get_bn().size();
    else size = temp_int.size();
    for (int i = size - 1;i > 0;i--)
    {
        if (k)
        {
            if (bn.get_bn()[i] + temp_int[i]>9)
            {
                temp.get_bn().push_back(bn.get_bn()[i] + temp_int[i] - 10 + 1);
                k = 1;
            }
            else
            {
                temp.get_bn().push_back(bn.get_bn()[i] + temp_int[i] + 1);
                k = 0;
            }
        }
        else
        {
            if (bn.get_bn()[i] + temp_int[i]>9)
            {
                temp.get_bn().push_back(bn.get_bn()[i] + temp_int[i] - 10);
                k = 1;
            }
            else
            {
                temp.get_bn().push_back(bn.get_bn()[i] + temp_int[i]);
                k = 0;
            }
        }

    }
    return temp;
}

//重载运算符+ =

BIGINT& BIGINT::operator+=(const BIGINT& bn) 
{
    int size;
    int k = 0;
    if (bn.big_number.size()>big_number.size()) size = big_number.size();
    else size = bn.big_number.size();
    for (int i = size - 1;i > 0;i--)
    {
        if (k)
        {
            if (big_number[i] + bn.big_number[i]>9)
            {
                big_number[i] = big_number[i] + bn.big_number[i] - 10 + 1;
                k = 1;
            }
            else
            {
                big_number[i] = big_number[i] + bn.big_number[i] + 1;
                k = 0;
            }
        }
        else
        {
            if (big_number[i] + bn.big_number[i]>9)
            {
                big_number[i] = big_number[i] + bn.big_number[i] - 10;
                k = 1;
            }
            else
            {
                big_number[i] = big_number[i] + bn.big_number[i];
                k = 0;
            }
        }

    }
    return *this;
}

//重载运算符==

BIGINT& BIGINT::operator==(const BIGINT& bn)
{
    for (int i = 0;i < big_number.size();i++)
    {
        big_number[i] == bn.big_number[i];
    }
    return *this;
}

//重载运算符&lt;&lt;

ostream& operator<<(ostream& out, const BIGINT& bn)
{
    for (int i = 0;i < bn.big_number.size();i++)
    {
        out << bn.big_number[i];
    }
    return out;
}

//主

int main()
{
    BIGINT a(123);
    BIGINT b(2);
    BIGINT c;
    c = a + b;
    cout << c << endl;
    system("pause");
    return 0;
}

1 个答案:

答案 0 :(得分:1)

这浪费了计算资源:

for (int i = 0;i < big_number.size();i++)
{
    big_number[i] == bn.big_number[i];
}
return *this;

可能违反十大计算指令之一:
计算结果应存储在变量或内存中。

因此,您比较了两个阵列插槽。非常重要。

为什么要返回对象的副本?
请记住,通过数学,您可以从比较中返回truefalse,而不是与之比较的对象。

这样的事情怎么样:

bool BIGINT::operator==(const BIGINT& bn)
{
    for (int i = 0;i < big_number.size();i++)
    {
        if (big_number[i] != bn.big_number[i])
        {
            return false;
        }
    }
    return true;
}

在找到不相等的第一组插槽时终止循环。搜索数组的其余部分没有意义。只需要有一个不平等来证明这种平等是错误的。

编辑1:用于平等的容器长度
要使容器相同,它们必须具有相同的大小或具有相同数量的元素。

因此,等式函数现在变为:

bool BIGINT::operator==(const BIGINT& bn)
{
    if (big_number.size() != bn.big_number.size())
    {
        return false;
    }
    for (int i = 0;i < big_number.size();i++)
    {
        if (big_number[i] != bn.big_number[i])
        {
            return false;
        }
    }
    return true;
}