Skip to content

Commit 040f84e

Browse files
Infix to postfix
1 parent 51b1611 commit 040f84e

File tree

3 files changed

+321
-0
lines changed

3 files changed

+321
-0
lines changed

stack/postfix/Demo.java

+133
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package postfix;
7+
8+
import java.util.Scanner;
9+
10+
public class Demo
11+
{
12+
public static void main(String[] args)
13+
{
14+
String infix;
15+
16+
Scanner scan = new Scanner(System.in);
17+
18+
System.out.print("Enter infix expression : ");
19+
infix = scan.nextLine();
20+
21+
String postfix = infixToPostfix(infix);
22+
23+
System.out.println("Postfix expression is : " + postfix);
24+
25+
System.out.println("Value of expression : " + evaluatePostfix(postfix));
26+
27+
scan.close();
28+
}
29+
30+
public static String infixToPostfix(String infix)
31+
{
32+
String postfix = new String();
33+
34+
StackChar st = new StackChar(20);
35+
36+
char next,symbol;
37+
for(int i=0; i<infix.length(); i++)
38+
{
39+
symbol=infix.charAt(i);
40+
41+
if(symbol==' ' || symbol=='\t') /*ignore blanks and tabs*/
42+
continue;
43+
44+
switch(symbol)
45+
{
46+
case '(':
47+
st.push(symbol);
48+
break;
49+
case ')':
50+
while((next=st.pop())!='(')
51+
postfix = postfix + next;
52+
break;
53+
case '+':
54+
case '-':
55+
case '*':
56+
case '/':
57+
case '%':
58+
case '^':
59+
while( !st.isEmpty() && precedence(st.peek())>= precedence(symbol) )
60+
postfix = postfix + st.pop();
61+
st.push(symbol);
62+
break;
63+
default: /*operand*/
64+
postfix = postfix + symbol;
65+
}
66+
}
67+
while(!st.isEmpty())
68+
postfix = postfix + st.pop();
69+
return postfix;
70+
}
71+
72+
public static int precedence(char symbol)
73+
{
74+
switch(symbol)
75+
{
76+
case '(':
77+
return 0;
78+
case '+':
79+
case '-':
80+
return 1;
81+
case '*':
82+
case '/':
83+
case '%':
84+
return 2;
85+
case '^':
86+
return 3;
87+
default :
88+
return 0;
89+
}
90+
}
91+
92+
public static int evaluatePostfix(String postfix)
93+
{
94+
StackInt st = new StackInt(20);
95+
96+
int x,y;
97+
for(int i=0; i<postfix.length(); i++)
98+
{
99+
if(Character.isDigit(postfix.charAt(i)))
100+
st.push(Character.getNumericValue(postfix.charAt(i)));
101+
else
102+
{
103+
x=st.pop();
104+
y=st.pop();
105+
switch(postfix.charAt(i))
106+
{
107+
case '+':
108+
st.push(y+x); break;
109+
case '-':
110+
st.push(y-x); break;
111+
case '*':
112+
st.push(y*x); break;
113+
case '/':
114+
st.push(y/x); break;
115+
case '%':
116+
st.push(y%x); break;
117+
case '^':
118+
st.push(power(y,x));
119+
}
120+
}
121+
}
122+
return st.pop();
123+
}
124+
125+
public static int power(int b,int a)
126+
{
127+
int i,x=1;
128+
for(i=1; i<=a; i++)
129+
x=x*b;
130+
return x;
131+
}
132+
}
133+

stack/postfix/StackChar.java

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package postfix;
7+
8+
import java.util.EmptyStackException;
9+
10+
public class StackChar
11+
{
12+
private char[] stackArray;
13+
private int top;
14+
15+
public StackChar()
16+
{
17+
stackArray = new char[10];
18+
top = -1;
19+
}
20+
21+
public StackChar(int maxSize)
22+
{
23+
stackArray = new char[maxSize];
24+
top=-1;
25+
}
26+
public int size()
27+
{
28+
return top+1;
29+
}
30+
31+
public boolean isEmpty()
32+
{
33+
return (top==-1);
34+
}
35+
36+
public boolean isFull()
37+
{
38+
return (top==stackArray.length-1);
39+
}
40+
41+
public void push(char x)
42+
{
43+
if(isFull())
44+
{
45+
System.out.println("Stack Overflow\n");
46+
return;
47+
}
48+
top=top+1;
49+
stackArray[top]=x;
50+
}
51+
52+
public char pop()
53+
{
54+
char x;
55+
if(isEmpty())
56+
{
57+
System.out.println("Stack Underflow\n");
58+
throw new EmptyStackException();
59+
}
60+
x=stackArray[top];
61+
top=top-1;
62+
return x;
63+
}
64+
65+
public char peek()
66+
{
67+
if(isEmpty())
68+
{
69+
System.out.println("Stack Underflow\n");
70+
throw new EmptyStackException();
71+
}
72+
return stackArray[top];
73+
}
74+
75+
76+
public void display()
77+
{
78+
int i;
79+
80+
System.out.println("top= " + top);
81+
82+
if(isEmpty())
83+
{
84+
System.out.println("Stack is empty");
85+
return;
86+
}
87+
System.out.println("Stack is : ");
88+
for(i=top; i>=0; i--)
89+
System.out.println(stackArray[i] + " ");
90+
System.out.println();
91+
}
92+
93+
94+
}

stack/postfix/StackInt.java

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
Copyright (C) Deepali Srivastava - All Rights Reserved
3+
This code is part of DSA course available on CourseGalaxy.com
4+
*/
5+
6+
package postfix;
7+
8+
import java.util.EmptyStackException;
9+
10+
public class StackInt
11+
{
12+
private int[] stackArray;
13+
private int top;
14+
15+
public StackInt()
16+
{
17+
stackArray = new int[10];
18+
top = -1;
19+
}
20+
21+
public StackInt(int maxSize)
22+
{
23+
stackArray = new int[maxSize];
24+
top=-1;
25+
}
26+
public int size()
27+
{
28+
return top+1;
29+
}
30+
31+
public boolean isEmpty()
32+
{
33+
return (top==-1);
34+
}
35+
36+
public boolean isFull()
37+
{
38+
return (top==stackArray.length-1);
39+
}
40+
41+
public void push(int x)
42+
{
43+
if(isFull())
44+
{
45+
System.out.println("Stack Overflow\n");
46+
return;
47+
}
48+
top=top+1;
49+
stackArray[top]=x;
50+
}
51+
52+
public int pop()
53+
{
54+
int x;
55+
if(isEmpty())
56+
{
57+
System.out.println("Stack Underflow\n");
58+
throw new EmptyStackException();
59+
}
60+
x=stackArray[top];
61+
top=top-1;
62+
return x;
63+
}
64+
65+
public int peek()
66+
{
67+
if(isEmpty())
68+
{
69+
System.out.println("Stack Underflow\n");
70+
throw new EmptyStackException();
71+
}
72+
return stackArray[top];
73+
}
74+
75+
76+
public void display()
77+
{
78+
int i;
79+
80+
System.out.println("top= " + top);
81+
82+
if(isEmpty())
83+
{
84+
System.out.println("Stack is empty");
85+
return;
86+
}
87+
System.out.println("Stack is : ");
88+
for(i=top; i>=0; i--)
89+
System.out.println(stackArray[i] + " ");
90+
System.out.println();
91+
}
92+
93+
94+
}

0 commit comments

Comments
 (0)