奇怪的函数返回结果
Strange function return result
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表达式。
相关文章:
- C++Brute Force攻击函数不会返回结果
- 如何在一个表达式中生成并返回结果?
- 使用std::tie进行类似golang的错误处理,同时返回结果,是否有缺点?(C++11)
- C++ 线程端回调并返回结果
- C++使用存储过程返回结果
- reinterpret_cast总是返回结果C++?
- 尝试以随机字符(长读取)打印字符串返回结果
- 通过C 中的参考返回结果
- 使用枚举确定返回结果的类型(使用宏的黑客)
- 返回结果不可见
- 打开一个 Shell 程序,向其传递参数,然后返回结果
- 如何在递归函数中计算返回结果
- 整数列表的交集-无法返回结果列表
- 来自 open() 的返回结果代码之间的差异:25 与 3
- 使用 c++11 线程返回结果的正确方法是什么
- 传递StringBuilder并从c++dll向c#返回结果
- 如何跳出一些深度递归的函数并直接返回结果
- 使用MPI临时接收数据,然后返回结果
- C++,我不知道为什么我的函数不返回结果
- QDialog show()后面跟着返回结果操作