在C ++中模拟自顶向下加法时的分段错误

时间:2015-02-18 06:16:10

标签: c++ pointers segmentation-fault addition

我正在尝试用C ++编写一个程序,它接受包含数字的两个字符串并将它们加在一起以返回“sum”(也是一个字符串)。 我用Java和Python编写了类似的程序,所以我决定在学习C ++的同时,我也可以编写类似的东西。我不知道为什么我收到错误,在使用Visual Studio或g ++编译时我没有收到任何错误。

以下是主程序中对相关功能的引用。

Number base(NULL);
Number r = base.addNums("1", "1");
cout << r.toString() << endl;

我确保我有一个构造函数如下,以引用addNums函数:

Number(void){}

我写了一些评论来尝试在编写头文件时解释我的思考过程。有问题的方法如下:

Number addNums(string in1, string in2){
        // Calling number 1: X, and number 2: Y
        const char* x;
        const char* y;
        x = in1.c_str();
        y = in2.c_str();

        // Flag for one number having more digits
        bool flag = false;
        // Flag for X having more digits
        bool xIsBigger = false;
        // For storing the sum later
        string summ = "";

        // Check and see if the flags are needed
        if (!(strlen(x) == strlen(y))){
            flag = true;
            if (strlen(x) > strlen(y)){
                xIsBigger = true;
            }
        }

        // Prepend the zeroes to the necessary variable
        //   to make it work as written addition does
        if (flag){
            if (xIsBigger){
                string zeroes;
                for (unsigned int i = 0; i < (strlen(x) - strlen(y)); ++i){
                    zeroes += "0";
                }
                string newYStr = zeroes + in2;
                const char* newY = newYStr.c_str();
                // Add zeroes to Y variable
                y = newY;
            } else{
                string zeroes;
                for (unsigned int i = 0; i < (strlen(y) - strlen(x)); ++i){
                    zeroes += "0";
                }
                string newXStr = zeroes + in1;
                const char* newX = newXStr.c_str();
                // Add variables to X value
                x = newX;
            }
        }

        // If we encounter x + y > 9, we need this
        int carry = 0;
        // Current digit being processed
        char digitX, digitY;
        // Digit to be carried
        char toCarry;

        // Iterate through the number right to left
        //   to simulate top-down addition
        for (int i = strlen(x) - 1; i >= 0; --i){
            digitX = x[i];
            digitY = y[i];

            // If we're carrying a 1, add it to the top number
            if (carry > 0){
                digitX += 1;
                carry = 0;
            }

            // Add together the two numbers stored in characters
            int currentSum = atoi(&digitX) + atoi(&digitY);

            // If x + y > 9, we need to carry
            if (currentSum > 9){
                string sumString = "" + currentSum;
                // Max possible is 9 + 9, so we only have to carry 1
                carry = 1;
                // Add the second digit in the number to the position in the sum
                summ = sumString.at(1) + summ;
            }
            // Didn't need a carry
            else{
                string sumString = "" + currentSum;
                summ = sumString + summ;
            }
        }
        // Return the object containing the sum
        return Number(summ);
    }

我对指针的使用相对较新,但在学习更多语言和编写程序的过程中,通过广泛的语法和语言特定的搜索,我被迫进入我的感受是需要使用它们。

我很抱歉,我无法提供更多信息,我感谢任何可以帮助我的帮助或批评。

提前致谢!

1 个答案:

答案 0 :(得分:0)

'string newYStr'可能超出范围?尝试在顶部声明它,然后检查。