-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpost-infix
57 lines (49 loc) · 950 Bytes
/
post-infix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// 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