奇怪的函数返回结果

Strange function return result

本文关键字:返回 结果 函数      更新时间:2023-10-16
float Calculate(const string &query)
{
        std::cout << "Query: " << query << "n";
        unsigned int size = query.length();
        char stack[70];
        float res;
        int m = 0;
        for (int i = 0; i < size; i++)
        {
                if (query[i] >= '0' && query[i] <= '9')
                {
                        stack[m] = query[i] - '0';
                        m++;
                        continue;
                }
                switch (query[i])
                {
                        case '+':
                        {
                                res = stack[m - 2] + stack[m - 1];
                                break;
                        }
                        case '-':
                        {
                                res = stack[m - 2] - stack[m - 1];
                                break;
                        }
                        case '*':
                        {
                                res = stack[m - 2] * stack[m - 1];
                                break;
                        }
                        case '/':
                        {
                                res = stack[m - 2] / stack[m - 1];
                                break;
                        }
                }
                    stack[m - 2] = res;
                m--;
                cout << "RES: " << res << "n";
        }
        return res;
}

它计算反向抛光符号。

当我调用类似于:Calculate("11+")的东西时,它会返回正确的结果:2

但是,当我在得到RPN字符串后传递一个变量时:

string inputStr;
string outputStr;
cout << "Put exercisen";
getline(std::cin, inputStr);
outputStr = GetRPN(inputStr);
cout << "Output str :" << outputStr << ":n";
float res = Calculate(outputStr);
std::cout << res << "n";

所以,当我输入字符串1+1时,函数GetRPN返回11+,我在第二个cout中看到了这一点。但结果是0

可能是什么?


string GetRPN(string input)
{
    vector <char> operation;
    string outputStr;      //output string, keep RPN
    int stack_count = 0;
    for(int i = 0; i < input.length(); i++)
    {
        if(input[i] >= '0' && input[i] <= '9')
        {
            outputStr += input[i];
        }
        else
        {
            if(operation.empty())
            {
                operation.push_back(input[i]);
                stack_count++;
            }
            else if(operation[stack_count - 1] == '+' || operation[stack_count - 1] == '-')
            {
                operation.push_back(input[i]);
                stack_count++;
            }
            else if ((operation[stack_count - 1] == '*' || operation[stack_count - 1] == '/') && (input[i] == '*' || input[i] == '/'))
            {
                outputStr += operation[stack_count - 1]; // move mark of operation to output str
                operation.pop_back(); // delet last element from vector
                operation.push_back(input[i]);// plus new operation mark to vector
                stack_count++;
            }
            else if (operation[stack_count - 1] == '*' || operation[stack_count - 1] == '/')
            {
                outputStr += input[i];
            }
        }
    }
    for(int i = operation.size(); i >= 0; i--)
    {
        outputStr += operation[i]; // move all operation marks to otput str
    }
    return outputStr;
}

您的周期在此

for(int i = operation.size(); i >= 0; i--)
{
    outputStr += operation[i]; // move all operation marks to otput str
}

没有任何意义。很明显,您试图访问无效索引处的矢量。当i等于operation.size()时,访问operation[i]处的元素是非法的。索引超出范围。

任何自尊的执行都会立即报告这个问题并断言。无论如何,正如我在评论中所说,类似的问题都可以通过调试代码来解决。为什么你要求别人调试你的代码而不是自己调试?

如果您的字符串中有任何空白或不可打印的字符,您最终将使用负索引存储到stack中,这将覆盖堆栈帧中的其他内容,并可能导致任何事情发生。

您应该为Calculate添加一些错误检查——开关应该有一个default,它会打印一条可感知的错误消息,并且您应该在访问stack[m]stack[m-2]之前检查m的值,以确保堆栈没有下溢或上溢(如果有,则应该打印一个可感知的错误(。)您应该能够将ANY随机字符串传递给Calculate,并让它告诉您为什么它不是有效的RPN表达式。