Conversion Algorithm

1、操作符栈压入"#";

2、依次读入表达式的每个单词;

3、如果是操作数则压入操作数栈;

4、如果是操作符,则将操作符栈顶元素与要读入的操作符进行优先级比较

(4.1)如果读入的是 ')',则将操作符栈中的元素压入操作数栈直至遇到 '(';

(4.2)如果读入的是 '(',压入操作符栈;

(4.3)如果栈顶元素优先级低,压入操作符栈;

(4.4)如果读入的元素不为'#',以及栈顶元素优先级高,则将栈顶元素压入操作数栈,将读入的元素压入操作符栈;

(4.5)如果操作符栈为空或操作符栈栈顶元素为 '(',压入操作符栈;

(4.6)其他,将操作符栈元素压出到操作数栈直至遇到'#';

5.将操作数栈元素逆序输出。

//infix to postfix
#include<iostream>
#include<vector>
using namespace std; struct Node
{
char data;
Node* next;
}; class LinkStack
{
public:
LinkStack()
{
top = new Node;
top = NULL;
}
~LinkStack()
{
delete top;
}
void push(char item);
void pop();
char front() const;
void display()const;
private:
Node*top;
}; void LinkStack::display()const
{
Node*p = top;
vector<char>s = {};
while (p != NULL)
{
s.push_back(p->data);
p = p->next;
}
vector<char>s1 = {};
vector<char>::size_type size = s.size();
for (vector<char>::size_type i = 0; i < size; i++)
s1.push_back(s[size - 1 - i]);
for (auto i : s1)
cout << i;
cout << endl;
} void LinkStack::push(char item)
{
Node*p = new Node;
p->data = item;
p->next = top;
top = p;
} void LinkStack::pop()
{
Node*p = top;
top = top->next;
delete p;
} char LinkStack::front()const
{
return top->data;
} bool isNum(char c)
{
return (c <= '9' && c >= '0');
} char Precede(char f, char c)
{
if (f == '+')
{
if (c == '*' || c == '/' || c == '(')return '<';
else return '>';
}
else if (f == '-')
{
if (c == '*' || c == '/' || c == '(')return '<';
else return '>';
}
else if (f == '*')
{
if (c == '(')return '<';
else return'>';
}
else if (f == '/')
{
if (c == '(')return '<';
else return'>';
}
else if (f == '(')
{
if (c == ')')return '=';
else return '<';
}
else if (f == ')')return '>';
else if (f == '#')
{
if (c == '#')return '=';
else return '<';
}
} void evaluate(LinkStack*SOPTR, LinkStack*SOPND)
{
SOPTR->push('#');
char c;
while (cin >> c)
{
if (isNum(c))
{
SOPND->push(c);
}
else
{
if (c == ')')
{
char c1 = SOPTR->front();
while (c1 != '(')
{
SOPND->push(c1);
SOPTR->pop();
c1 = SOPTR->front();
}
SOPTR->pop();
}
else if (c == '(')
SOPTR->push(c);
else if (Precede(SOPTR->front(), c) == '<')
{
SOPTR->push(c);
}
else if (c!='#'&&Precede(SOPTR->front(), c) == '>')
{
char cha = SOPTR->front();
SOPTR->pop();
SOPTR->push(c);
SOPND->push(cha);
}
else if (SOPTR->front() == '#' || SOPTR->front() == '(')
{
SOPTR->push(c);
}
else
{
char ch = SOPTR->front();
while (ch != '#')
{
SOPND->push(ch);
SOPTR->pop();
ch = SOPTR->front();
}
}
}
}
SOPND->display();
delete SOPND, SOPTR;
} int main()
{
cout << "input the infix expression:(you must input # to stop input)" << endl;
LinkStack* SOPTR = new LinkStack;
LinkStack* SOPND = new LinkStack;
evaluate(SOPTR, SOPND); return 0;
}

  

Infix to postfix conversion 中缀表达式转换为后缀表达式的更多相关文章

  1. 利用stack结构,将中缀表达式转换为后缀表达式并求值的算法实现

    #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving with Algorithms and Da ...

  2. 练习3.20 a 将中缀表达式转换为后缀表达式

    //将中缀表达式转换为后缀表达式 int main() { ; ]={,,,,,,,}; char tmp; PtrToStack s; s = CreateStack( MaxSize ); ) { ...

  3. 栈的应用实例——中缀表达式转换为后缀表达式

    声明:本程序读入一个中缀表达式,将该中缀表达式转换为后缀表达式并输出后缀表达式. 注意:支持+.-.*./.(),并且输入时每输入完一个数字或符号都要加一个空格,特别注意的是在整个表达式输入完成时也要 ...

  4. 数据结构Java实现06----中缀表达式转换为后缀表达式

    本文主要内容: 表达式的三种形式 中缀表达式与后缀表达式转换算法 一.表达式的三种形式: 中缀表达式:运算符放在两个运算对象中间,如:(2+1)*3.我们从小做数学题时,一直使用的就是中缀表达式. 后 ...

  5. 中缀表达式转换为后缀表达式(python实现)

    中缀表示式转换为后缀表达式 需要一个存放操作符的栈op_stack,输出结果的列表output 步骤: 从左到右遍历表达式: 1. 若是数字,直接加入到output 2. 若是操作符,比较该操作符和o ...

  6. javascript使用栈结构将中缀表达式转换为后缀表达式并计算值

    1.概念 你可能听说过表达式,a+b,a+b*c这些,但是前缀表达式,前缀记法,中缀表达式,波兰式,后缀表达式,后缀记法,逆波兰式这些都是也是表达式. a+b,a+b*c这些看上去比较正常的是中缀表达 ...

  7. NYOJ--257--郁闷的C小加(一)(中缀表达式变后缀表达式 )

    郁闷的C小加(一) 时间限制:1000 ms  |  内存限制:65535 KB 难度:3   描述 我们熟悉的表达式如a+b.a+b*(c+d)等都属于中缀表达式.中缀表达式就是(对于双目运算符来说 ...

  8. 中缀表达式得到后缀表达式(c++、python实现)

    将中缀表达式转换为后缀表达式的算法思想如下: 从左往右开始扫描中缀表达式 遇到数字加入到后缀表达式 遇到运算符时: 1.若为‘(’,入栈 2.若为’)‘,把栈中的运算符依次加入后缀表达式,直到出现'( ...

  9. 3-06. 表达式转换(25)(中缀表达式转后缀表达式ZJU_PAT)

    题目链接:http://pat.zju.edu.cn/contests/ds/3-06 算术表达式有前缀表示法.中缀表示法和后缀表示法等形式. 日常使用的算术表达式是採用中缀表示法,即二元运算符位于两 ...

随机推荐

  1. 实战JAVA虚拟机 JVM故障诊断与性能优化 pdf

    需要的小伙伴拿走,百度云盘:http://pan.baidu.com/s/1nvm6RHZ

  2. java 生成证书用于https

    在jdk的bin目录下运行: keytool -genkeypair -alias "tomcat" -keyalg "RSA" -keystore " ...

  3. SQL中判断字符串中包含字符的方法

    通过2个函数CHARINDEX和PATINDEX以及通配符的灵活使用 函数:CHARINDEX和PATINDEX CHARINDEX:查某字符(串)是否包含在其他字符串中,返回字符串中指定表达式的起始 ...

  4. iframe自适应高度计算,iframe自适应

    计算页面的实际高度,iframe自适应会用到 IfrHeight: function (iframeId, callback) { var height; function calcPageHeigh ...

  5. android 5.0 -- Palette

    Palette用来从图片资源中获取颜色内容. 下面是个对颜色值使用的工具类: public class PaletteUtils { public static int getColorWithDef ...

  6. day23 框架之基础加强

    day23 框架之基础加强 今日任务 aptana(javascript的eclipse插件):http://www.cnblogs.com/terrylin/archive/2012/06/20/2 ...

  7. 2、创建File类对象

    既然是内置类,那么我们创建对象时自然要看它封装好的构造函数咯,由下图的4中构造函数我们可知有4种办法来创建File对象 具体代码如下 public class Demo { public static ...

  8. Leetcode 289 Game of Life

    According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...

  9. LeetCode OJ 39. Combination Sum

    Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C wher ...

  10. hdu 1407 测试你是否和LTC水平一样高

    Description 大家提到LTC都佩服的不行,不过,如果竞赛只有这一个题目,我敢保证你和他绝对在一个水平线上! 你的任务是: 计算方程x^2+y^2+z^2= num的一个正整数解.  Inpu ...