#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main()
{
string blah("512+4*+3−");
stack<int> astack;
int a=0;
int b=0;
int result=0;
int final=0;
for (string::iterator count=blah.begin(); count != blah.end(); count=count+1)
{
if (isdigit(*count))
{
astack.push(*count);
cout<<" "<<*count<<" ";
}
else
{
a=astack.top();
astack.pop();
b=astack.top();
astack.pop();
if(*count=='+')
{
result = a+ b;
}
else if (*count=='-')
{
result=a-b;
}
else if(*count=='*')
{
result=a*b;
}
astack.push(result);
}
}
final=astack.top();
cout<<final;
}
내 문제는 코드를 실행할 때마다 오류가 발생하는 것입니다. 내가 연산자를 사용하여 실행 해 보았을 때 스택에 두 개의 값이 표시되는 것처럼 보였습니다. 이유는 확실하지 않습니다.스택과 반복자가있는 접미사 계산기 작성
디버거에서 오류의 원인으로보고 된 라인을 알려주시겠습니까? –
첫 번째 문자가 숫자가 아닌 경우 어떻게됩니까? 빈 스택의 맨 위를 질의합니다. 이러한 가정을 확인하기 위해'assert'를 많이 사용하면 오류가 발생할 수 있습니다. –
글쎄, 당신이 올렸던'blah' 문자열의 "-"표시는 빼기 부호가 아닙니다. 또한, 복제 할 수 없습니다 : http://ideone.com/xqQSmM – PaulMcKenzie