Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 奇怪的函数返回结果_C++_Function - Fatal编程技术网

C++ 奇怪的函数返回结果

C++ 奇怪的函数返回结果,c++,function,C++,Function,所以,当我输入字符串:1+1时,函数GetRPN返回11+,我在第二栏看到了这一点。但结果是0 可能是什么 string GetRPN(字符串输入) { 向量运算; string outputStr;//输出字符串,保留RPN int stack_count=0; 对于(int i=0;i='0'&&input[i]=0;i--) { outputStr+=操作[i];//将所有操作标记移动到otput str } 返回outputStr; } 如果字符串中有任何空格或不可打印字符,则最终将以

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

可能是什么


string GetRPN(字符串输入)
{
向量运算;
string outputStr;//输出字符串,保留RPN
int stack_count=0;
对于(int i=0;i='0'&&input[i]=0;i--)
{
outputStr+=操作[i];//将所有操作标记移动到otput str
}
返回outputStr;
}

如果字符串中有任何空格或不可打印字符,则最终将以负索引存储到
堆栈中,这将覆盖堆栈框架中的其他内容,并可能导致任何情况发生

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

您的循环在这里

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;
}
没有任何意义。您显然试图在无效索引处访问向量。当
i
等于
operation.size()
时,在
operation[i]
处访问元素是非法的。索引超出范围


任何自尊的实现都会立即用断言报告此问题。在任何情况下,正如我在评论中所说的,这样的问题都是通过调试代码来解决的。为什么你要让其他人调试你的代码而不是自己调试呢?

确保没有任何尾随空格或其他“垃圾”从GetRPN返回的空白字符-您可能会打印出长度。不确定
GetRPN
的作用,但它可能会在字符串outputStr中留下一个最后的
\n
\r
,这在
开关(查询[i])
中无法匹配,导致
res以某种方式为空…您的
计算()
看起来不错,给定相同的输入应该会输出相同的结果。你也可以发布你的
GetRPN()
函数吗?我从来不知道
std::string
lenght()
成员函数,它返回与
size()
相同的值。GetRPN的最后一个循环看起来可疑-你不想从I=operation.size()开始吗-1?如果您有一个2字符的字符串,那么您只想附加元素1和0,而不是2、1和0。但是,这并不能解释为什么您会看到正确的输出。
string inputStr;
string outputStr;

cout << "Put exercise\n";
getline(std::cin, inputStr);

outputStr = GetRPN(inputStr);
cout << "Output str :" << outputStr << ":\n";

float res = Calculate(outputStr);
std::cout << res << "\n";
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
}