文档下载 文档下载导航
设为首页 | 加入收藏
搜索 请输入内容:  
 导航当前位置: 文档下载 > 所有分类 > IT/计算机 > 计算机软件及应用 > ReversePolishNotation
侵权投诉

ReversePolishNotation

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);
}

第1页

你可能喜欢

热门文档

相关文档

  • 逆波兰式的生成算法

    public class ReversePolishNotation{ /*---成 员变量的声明 ---*/ private final char operatorPriorMatrix[ ][ ] = { { '>' , '>' , '<' , '<' ...

  • leetcode

    public class EvaluateReversePolishNotation { /** * @param args * 逆波兰式 */ public static int evalRPN(String[] tokens){ Stack<Integer> s=new Stack...

  • C#.NET逆波兰算法求解普通算式(支持自定义运算符)

    { var rpn = new ReversePolishNotation(); rpn.AddOperation(new Power('^', "乘方", 5)); rpn.AddOperation(new E('e', "科学记数法", 5)); var ...

  • 表达式与二叉树的相互转换

    后缀表达式又称逆波兰式(RPN,Reverse2.4表达式树Polish Notation)。图1 就是将一个数学表达式用一棵二叉树表示。其中,运算对象作为树的叶子结点(没有后继 的结点...

  • C语言之逆波兰表达式完整代码(附算法)

    C 语言课程设计之逆波兰表达式 //逆波兰表达式(后缀表达式)reverse polish notation //程序实现的功能是将中缀表达式转变为后缀表达式,再求出其值 //主要运用的知识...

  • Reverse Polish notation in constructing the algorit...

    Reverse Polish notation in constructing the algorithm for polygon triangulation The reverse Polish notation properties are used in the construction of the algori...

  • RPN在实际应用中处理

    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...

站点地图 | 文档上传 | 侵权投诉 | 手机版
本站所有资源均来自互联网,本站只负责收集和整理,均不承担任何法律责任,如有侵权等其它行为请联系我们.
文档下载 Copyright 2013 doc.guandang.net All Rights Reserved.  -  email