c++实现逆波兰式
#include<iostream.h>
#include"Stack.h"
bool Priority(char in,char out);
bool IsDigit(char p);
void Nibolanshi(char str[]);
void Nibolanshi(char str[])
{
char p;
Stack<char> s(10);
s.Push('#');
int i=0;
while(str[i]!='\0')
{
if(IsDigit(str[i]))
{
cout<<str[i++];
continue;
}
p=s.GetTopData();
if(Priority(p,str[i]))
{
s.Push(str[i++]);
continue;
}
while(1)
{
if(Priority(p,str[i]))
{
s.Push(str[i]);
break;
}
if(p=='#')break;
cout<<p;
s.Pop();
p=s.GetTopData();
}
i++;
}
p=s.GetTopData();
while(p!='#')
{
cout<<p;
s.Pop();
p=s.GetTopData();
}
}
bool IsDigit(char p)
{
if(p=='('||p==')'||p=='+'||p=='-'||p=='*'||p=='/'||p=='%')
return false;
return true;
}
bool Priority(char in,char out) //in代表栈内字符,out代表栈外字符
{
int num1,num2;
switch(in)
{
case'#':num1=0;break;
case'+':
case'-':num1=1;break;
case'*':
case'/':
case'%':num1=2;break;
}
switch(out)
{
case'#':num2=0;break;
case'+':
case'-':num2=1;break;
case'*':
case'/':
case'%':num2=2;break;
}
return (num2>num1);
}
void main()
{
char a[]="a+b-c-a+b/e";
Nibolanshi(a);
}
public class ReversePolishNotation{ /*---成 员变量的声明 ---*/ private final char operatorPriorMatrix[ ][ ] = { { '>' , '>' , '<' , '<' ...
public class EvaluateReversePolishNotation { /** * @param args * 逆波兰式 */ public static int evalRPN(String[] tokens){ Stack<Integer> s=new Stack...
{ var rpn = new ReversePolishNotation(); rpn.AddOperation(new Power('^', "乘方", 5)); rpn.AddOperation(new E('e', "科学记数法", 5)); var ...
后缀表达式又称逆波兰式(RPN,Reverse2.4表达式树Polish Notation)。图1 就是将一个数学表达式用一棵二叉树表示。其中,运算对象作为树的叶子结点(没有后继 的结点...
C 语言课程设计之逆波兰表达式 //逆波兰表达式(后缀表达式)reverse polish notation //程序实现的功能是将中缀表达式转变为后缀表达式,再求出其值 //主要运用的知识...
Reverse Polish notation in constructing the algorithm for polygon triangulation The reverse Polish notation properties are used in the construction of the algori...
2.逆波兰表达式(Reverse Polish Notation) 由 J. Lukasiewicz(12/21/1878 – 02/13/1956)发展而来,在避免 使用括号的情况下,完成表达式的有优先级的运算。RPN ...
后缀表示法也称 逆波兰表示法(reverse Polish notation,RPN),因其使 表达式求值变得轻松,所以被普遍使用。 前缀和后缀表示法有三项公共特征:操作数的顺序与等价的...
? d e ? ? Reverse Polish notation operand operator with the highest precedence operator 8 〖Example〗 6 2 ? 3 ? 4 2 ? ? = ? Get token: 6 (...
后缀表示法也称逆波兰表示法(reverse Polish notation,RPN),因其使表达式求值变得轻松,所以被普遍使用。 Syntax : operand1 operand2 operator Example : AB+C*DEF...
热门文档