2 u013858410 u013858410 于 2014.03.10 22:19 提问

实现中序波兰数,为什么程序会总是崩掉啊?求大神指点啊。。。。已调式许久

#include
#include
#include
using namespace std;

void tran()/*中序转化成后续*/
{ stackoperation;
stackplus;
stackreplus;
char letter;

while(cin.get(letter)&&letter!='/n'){
if(isdigit(letter))
{ cout< plus.push(letter);
}
else if (letter=='(')
operation.push(letter);
else if(letter=='+'||letter=='-'||letter=='*'||letter=='/')
{
if(operation.empty())
operation.push(letter);
else
if((letter=='+'||letter=='-')&&(operation.top()=='*'||operation.top()=='/'))
{ cout plus.push(operation.top());
operation.pop();
operation.push(letter);
}
else
operation.push(letter);}
else
{
while(operation.top()!='('){
cout plus.push(operation.top());
operation.pop();}
operation.pop();}}
while(!operation.empty())
{
cout plus.push(operation.top());
operation.pop();}
while(!plus.empty()){
replus.push(plus.top());
plus.pop();
}}
/*运算后序波兰数*/
void result(stack&numbers,char letter){
int result1,result2;
result1=numbers.top();
numbers.pop();
result2=numbers.top();
numbers.pop();
switch(letter){
case'+':numbers.push(result1+result2);break;
case'-':numbers.push(result2-result1);break;
case'*':numbers.push(result2*result1);break;
case'/':numbers.push(result2/result1);break;}}
/*读取结果*/
void readit(stack&replus){
stacknumbers;
int number;
char letter;
letter=replus.top();
replus.pop();
while(letter!='\n'){
if(isdigit(letter)){
number=(int)letter-48;
numbers.push(number);}
else if(letter=='+'||letter=='-'||letter=='*'||letter=='/')
result(numbers,letter);
letter=replus.top();
replus.pop();}
cout<<numbers.top();}

int main(){
stackreplus;
cout<<"plaese write out your numbers and your operations"<<endl;
tran();
readit(replus);

return 0;}
Csdn user default icon
上传中...
上传图片
插入图片