中缀表达式:形式为op b的表达式。当运算符位于每对操作数之间时。
后缀表达式:ab op形式的表达式。每对操作数都遵循一个运算符。
后缀表示法(也称为反向波兰表示法)是一种数学表达式的语法,其中,数学运算符始终放在操作数之后。尽管后缀表达式可以通过计算机轻松有效地进行评估,但是对于人类来说,它们可能很难阅读。使用标准的带括号的前缀表示法的复杂表达式通常比相应的后缀表达式更具可读性。因此,有时我们希望允许最终用户使用中缀表示法,然后将其转换为后缀表示法以进行计算机处理。此外,有时,表达式是在postfix中存储或生成的,为了阅读和编辑,我们希望将它们转换为infix。
例子:
Input : abc++
Output : (a + (b + c))
Input : ab*c+
Output : ((a*b)+c)
我们已经讨论过Postfix的Infix。以下是Postfix到Infix的算法。
算法
1.虽然还有输入符号
…1.1从输入中读取下一个符号。
2.如果符号是一个操作数
…2.1将其推入堆栈。
3,否则
…3.1符号是一个运算符。
…3.2从堆栈中弹出前2个值。
…3.3将值作为参数的运算符放入字符串。
…3.4将结果字符串推回堆栈。
4,如果堆栈中只有一个值
…4.1堆栈中的值是所需的中缀字符串。
下面是上述方法的实现:
C++
// CPP program to find infix for
// a given postfix.
#include
using namespace std;
bool isOperand(char x)
{
return (x >= 'a' && x <= 'z') ||
(x >= 'A' && x <= 'Z');
}
// Get Infix for a given postfix
// expression
string getInfix(string exp)
{
stack s;
for (int i=0; exp[i]!='\0'; i++)
{
// Push operands
if (isOperand(exp[i]))
{
string op(1, exp[i]);
s.push(op);
}
// We assume that input is
// a valid postfix and expect
// an operator.
else
{
string op1 = s.top();
s.pop();
string op2 = s.top();
s.pop();
s.push("(" + op2 + exp[i] +
op1 + ")");
}
}
// There must be a single element
// in stack now which is the required
// infix.
return s.top();
}
// Driver code
int main()
{
string exp = "ab*c+";
cout << getInfix(exp);
return 0;
}
Java
// Java program to find infix for
// a given postfix.
import java.util.*;
class GFG
{
static boolean isOperand(char x)
{
return (x >= 'a' && x <= 'z') ||
(x >= 'A' && x <= 'Z');
}
// Get Infix for a given postfix
// expression
static String getInfix(String exp)
{
Stack s = new Stack();
for (int i = 0; i < exp.length(); i++)
{
// Push operands
if (isOperand(exp.charAt(i)))
{
s.push(exp.charAt(i) + "");
}
// We assume that input is
// a valid postfix and expect
// an operator.
else
{
String op1 = s.peek();
s.pop();
String op2 = s.peek();
s.pop();
s.push("(" + op2 + exp.charAt(i) +
op1 + ")");
}
}
// There must be a single element
// in stack now which is the required
// infix.
return s.peek();
}
// Driver code
public static void main(String args[])
{
String exp = "ab*c+";
System.out.println( getInfix(exp));
}
}
// This code is contributed by Arnab Kundu
Python3
# Python3 program to find infix for
# a given postfix.
def isOperand(x):
return ((x >= 'a' and x <= 'z') or
(x >= 'A' and x <= 'Z'))
# Get Infix for a given postfix
# expression
def getInfix(exp) :
s = []
for i in exp:
# Push operands
if (isOperand(i)) :
s.insert(0, i)
# We assume that input is a
# valid postfix and expect
# an operator.
else:
op1 = s[0]
s.pop(0)
op2 = s[0]
s.pop(0)
s.insert(0, "(" + op2 + i +
op1 + ")")
# There must be a single element in
# stack now which is the required
# infix.
return s[0]
# Driver Code
if __name__ == '__main__':
exp = "ab*c+"
print(getInfix(exp.strip()))
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)
C#
// C# program to find infix for
// a given postfix.
using System;
using System.Collections;
class GFG
{
static Boolean isOperand(char x)
{
return (x >= 'a' && x <= 'z') ||
(x >= 'A' && x <= 'Z');
}
// Get Infix for a given postfix
// expression
static String getInfix(String exp)
{
Stack s = new Stack();
for (int i = 0; i < exp.Length; i++)
{
// Push operands
if (isOperand(exp[i]))
{
s.Push(exp[i] + "");
}
// We assume that input is
// a valid postfix and expect
// an operator.
else
{
String op1 = (String) s.Peek();
s.Pop();
String op2 = (String) s.Peek();
s.Pop();
s.Push("(" + op2 + exp[i] +
op1 + ")");
}
}
// There must be a single element
// in stack now which is the required
// infix.
return (String)s.Peek();
}
// Driver code
public static void Main(String []args)
{
String exp = "ab*c+";
Console.WriteLine( getInfix(exp));
}
}
// This code is contributed by Arnab Kundu
PHP
stack = array();
$this->limit = $limit;
}
function push($item) {
// trap for stack overflow
if (count($this->stack) < $this->limit) {
// prepend item to the start of the array
array_unshift($this->stack, $item);
} else {
throw new RunTimeException('Stack is full!');
}
}
function pop() {
if ($this->isEmpty()) {
// trap for stack underflow
throw new RunTimeException('Stack is empty!');
} else {
// pop item from the start of the array
return array_shift($this->stack);
}
}
function top() {
return current($this->stack);
}
function isEmpty() {
return empty($this->stack);
}
function Prec($ch)
{
switch ($ch)
{
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
function isOperand($ch)
{
return ($ch >= 'a' && $ch <= 'z') || ($ch >= 'A' && $ch <= 'Z');
}
function isOperator($x) {
switch ($x) {
case '+':
case '-':
case '/':
case '*':
return true;
}
return false;
}
public function getInfix($exp)
{
$this->CreateStack(sizeof($exp));
for ($i=0; $exp[$i]!= null; $i++)
{
// Push operands
if ($this->isOperand($exp[$i]))
{
$op = $exp[$i];
$this->push($op);
}
// We assume that input is
// a valid postfix and expect
// an operator.
else
{
$op1 = $this->top(); $this->pop();
$op2 = $this->top(); $this->pop();
$this->push("(". $op2 . $exp[$i] . $op1 . ")");
//$this->push($temp);
}
}
// There must be a single element
// in stack now which is the required
// infix.
return $this->top();
}
}
$myExample = new Stack();
echo $input = "ab*c+";
$exp = str_split($input,sizeof($input));
echo '
'.$data = $myExample->getInfix($exp);
?>
输出:
((a*b)+c)