【问题标题】:Adding big Integers using stack in C++在 C++ 中使用堆栈添加大整数
【发布时间】:2015-10-30 19:15:12
【问题描述】:

我正在使用 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

如果有人对我的程序有什么问题有任何想法,我将不胜感激。

【问题讨论】:

  • 听起来您可能需要学习如何使用调试器来单步调试您的代码。使用好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏差在哪里。如果您要进行任何编程,这是必不可少的工具。延伸阅读:How to debug small programs
  • @NathanOliver 我到底做了那里写的?但我还是不知道我的代码有什么问题。
  • 这里不需要你的复制构造函数和赋值运算符。您应该通过 const 引用传递大对象。

标签: c++ stack bigdata add biginteger


【解决方案1】:

您的代码存在一些问题。首先你在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;
}

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

【讨论】:

    猜你喜欢
    • 2011-12-14
    • 2017-03-02
    • 2021-04-15
    • 1970-01-01
    • 2020-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-30
    相关资源
    最近更新 更多