从中缀转换为后缀 [英] conversion from infix to postfix

查看:86
本文介绍了从中缀转换为后缀的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨......我需要一个程序,使用堆栈将中缀算术表达式转换为postfix ...任何人都可以告诉我如何实现这个?谢谢

Hi... I need a program to convert an infix arithmetic expression to postfix with the use of stacks... Can anyone tell me how do i go about implementing this? thanks

推荐答案

有一个标准算法可以做到这一点。 此网站的最后一段有步骤。


按正常顺序打印数字,将括号和运算符压入堆栈。当括号排成一行时,忽略它们,不要打印任何东西。如果他们不排队,这是一个错误。然后,根据操作员(PEMDAS)的优先级,将操作员从堆栈中推出或弹出。如果弹出它,请在输出中显示它。


网站更好地解释了,你为什么不去看看。
There is a standard algorithm to do this. The last paragraph of this website has the steps.

Print the numbers out in normal order, push parenthesis and operators onto the stack. When parenthesis line up, ignore them, don''t print anything. If they don''t line up, that''s an error. Then, depending upon priority of the operator (PEMDAS) push or pop the operator off the stack. If you pop it, display it in the output.

The website explains it better, why don''t you go have a look.


你可以帮助我检查这个..我迷失在我自己的程序中...我不知道它的某些部分是错的......任何人都可以告诉我...谢谢


#include< iostream>

#include< stack>

#include< vector>

#include< sstream>

using namespace std ;


int main()

{

string line,str;

int number;


cout<<"" Sample Input 1"<<"" "<< endl;

cin>> line;


getline(cin,line);

istringstream ISS(线); //创建字符串流


while(!iss.eof())

{

iss>>海峡; //从iss读取到str直到满足下一个空格

}


//后缀评估

int eval_postfix(const vector< ; char>& v,stack< int>& st)

{

for(vector< char> :: const_iterator str = v.begin(begin() ; str!= v.end(); ++ str)

{

if(''0''< = str&& str< =' '9'')

st.push(str - ''0'')

else

{

int arg2 = st.top();

st.pop();

int arg1 = st.top();

st .pop();

int result;


if(str ==" +" || str ==" - ")/ /当你比较char时,它'''''和'' - ''

{

case''+' ':result = arg1 + arg2;

返回1;

break;

case'' - '':result = arg1 - arg2;

resturn 2;

休息;

}

else if(str ==" *" || str ==" /")

{

case''*'':result = arg1 * arg2;

break;

返回3;

case''/'':result = arg1 / arg2;

break;

返回4;

} //结束if else


st.push(结果);

} //结束其他

} //结束


int i = st.top();

st .pop();

返回i;

} //结束eval_postfix


系统(暂停 ;);

返回0;

}
Hi can anyone help me to check this... Im lost in my own program... I noe some part of it is wrong... can anyone tell me... thanks

#include <iostream>
#include <stack>
#include <vector>
#include <sstream>
using namespace std;

int main()
{
string line, str;
int number;

cout<<"Sample Input 1"<<" "<<endl;
cin>>line;

getline(cin, line);
istringstream iss(line); //create string stream

while(!iss.eof())
{
iss >> str; //reads from iss into str until next space is met
}

//Postfix evaluation
int eval_postfix(const vector<char>&v, stack<int>&st)
{
for (vector<char>::const_iterator str = v.begin(begin(); str !=v.end(); ++str)
{
if (''0''<= str && str<=''9'')
st.push(str-''0'')
else
{
int arg2 = st.top();
st.pop();
int arg1 = st.top();
st.pop();
int result;

if (str == "+" || str == "-") //when you compare for char, it''s ''+'' and ''-''
{
case ''+'': result = arg1 + arg2;
return 1;
break;
case ''-'': result = arg1 - arg2;
resturn 2;
break;
}
else if (str == "*" || str == "/")
{
case ''*'': result = arg1 * arg2;
break;
return 3;
case ''/'': result = arg1 / arg2;
break;
return 4;
} //end if else

st.push(result);
} //end else
}//end for

int i = st.top();
st.pop();
return i;
}//end eval_postfix


system("pause");
return 0;
}


您好我修改我的代码但我无法执行它.. ..任何人都可以帮我发现错误..谢谢

Hi i modify my codes but i''m unable to execute it.... can anyone help me to spot the mistake.. thanks

展开 | 选择 | Wrap | 行号


这篇关于从中缀转换为后缀的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆