在 C++ 中使用堆栈添加大整数

Adding big Integers using stack in C++

我正在使用 C++ 中的堆栈将两个整数相加。这是我的代码:

bih_int.h
#include"../../../std_lib_facilities.h"
#include <stack>


stack<char> convert_to_stack(string);
string convert_to_string(stack<char>);
int toDigit(char);
char toChar(int);
int unit(int);
int carry(int);

class Big_Int
{
public:
    Big_Int();                // default constructor
    Big_Int(const Big_Int&);  // copy constructor
    Big_Int(string);          // constructor
    Big_Int(stack<char>);     // constructor
    Big_Int& operator=(const Big_Int&);
    Big_Int operator+(const Big_Int&);
    friend ostream& operator<<(ostream& , const Big_Int&);
    friend istream& operator>>(istream&, const Big_Int&);
    void p() { cout << number << endl; }
private:
    string number;
    stack<char> abs_value;
};


big_int.cpp
#include "big_int.h"

Big_Int::Big_Int(string value)
{
    number = value;
    abs_value = convert_to_stack(number);
}

Big_Int::Big_Int(stack<char> value)

{
    abs_value = value;
    number = convert_to_string(abs_value);
}

Big_Int::Big_Int(const Big_Int& arg)
{
    number = arg.number;
    abs_value = arg.abs_value;
}

stack<char> convert_to_stack(string str)
{
    stack<char> stk;

    for (int i = 0; i < str.length(); i++)
        stk.push(str[i]);
    return stk;
}

string convert_to_string(stack<char> stk)
{
    string str;
    while (!stk.empty())
    {
        str.push_back(stk.top());
        stk.pop();
    }
    reverse(str.begin(), str.end());
    return str;
}

Big_Int& Big_Int::operator=(const Big_Int& arg)
{
    number = arg.number;
    abs_value = arg.abs_value;
    return *this;
}

Big_Int Big_Int::operator+(const Big_Int& i)
{
    stack<char> num1 = convert_to_stack(number);
    stack<char> num2 = convert_to_stack(i.number);
    stack<char> res;
    int result = 0;

    while (!(num1.empty()) || !(num2.empty()))
    {
        if (!(num1.empty()))
        {
            result = +(toDigit(num1.top()));
            num1.pop();
        }
        if (!(num2.empty()))
        {
            result = +(toDigit(abs_value.top()));
            num2.pop();
        }
        res.push(toChar(unit(result)));
        result = carry(result);
    }
    if (result != 0) res.push(toChar(result));
    return Big_Int(res);
}

ostream& operator<<(ostream& os, const Big_Int& num)
{
    os << num.number << endl;
    return os;
}

istream& operator>>(istream& is, const Big_Int& num)
{
    string s;
    is >> s;
    return is;
}

int toDigit(char c)
{
    int num = c - '0';
    return num;
}

char toChar(int num)
{

    char ch = num + '0';
    return ch;
}

int unit(int num)
{
    int n = num % 10;
    return n;
}
int carry(int num)
{
    int n = num - (num % 10);
    return n;
}

main.cpp
#include "big_int.h"

int main()
{
    string num1, num2;
cin >> num1;
Big_Int i1(num1);
cout << i1 << endl;
cin >> num2;
Big_Int i2(num2);
cout << i2<< endl;
Big_Int sum(i1 + i2);
cout << sum << endl;
}

编译和链接都很好。但它没有添加正确。 这是我 运行 时显示的内容:

56
56

79
79

21D

如果有人知道我的程序有什么问题,我将不胜感激。

您的代码存在一些问题。首先,您在 if (!(num2.empty())).

中使用了错误的容器
result = +(toDigit(abs_value.top()));

应该是

result = +(toDigit(num2.top()));

其次,您在 operator+ 中重新分配 result 而不是添加它。

result = +(toDigit(num1.top()));
and
result = +(toDigit(num2.top()));

应该是

result += +(toDigit(num1.top()));
and
result += +(toDigit(num2.top()));

最后,您的 unit()carry() 函数不正确。使用 unit() 你只需要数字的个位,所以你只需要

int unit(int num)
{
    return num % 10;
}

然后使用 carry() 函数,因为你只想要十位,你可以使用

int carry(int num)
{
    return num / 10;
}

进行这些更改后,代码会针对我测试过的所有输入运行