forked from kant003/JavaPracticeHacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStackApplications.java
161 lines (145 loc) · 3.81 KB
/
StackApplications.java
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import java.util.*;
class Applications
{
public static boolean ValidParanthesis(String paranthesis)
{
Stack<Character> st = new Stack<Character>();
int i;char val;
for(i=0; i<paranthesis.length(); i++)
{
val=paranthesis.charAt(i);
if(val == '('|| val=='[' ||val=='{')
st.add(val);
else
{
if(st.isEmpty())
return false;
else if(val == ')' ){
if(st.peek()=='(')
st.pop();
else
return false;
}
else if(val == '}' ){
if(st.peek()=='{')
st.pop();
else
return false;
}
else if(val == ']' ){
if(st.peek()=='[')
st.pop();
else
return false;
}
}
}
if(st.isEmpty())
return true;
else
return false;
}
public static int Prec(char ch)
{
switch (ch) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
case '^':
return 3;
}
return -1;
}
public static void InfixtoPostfix(String expr)
{
int i;
String result = new String("");
Stack<Character> st = new Stack<Character>();
for ( i = 0; i < expr.length(); ++i) {
char ch = expr.charAt(i);
if (Character.isLetterOrDigit(ch))
result += ch;
else if (ch == '(')
st.push(ch);
else if (ch == ')') {
while (!st.isEmpty() && st.peek() != '(') {
result += st.peek();
st.pop();
}
if(!st.isEmpty())
st.pop();
}
else {
while (!st.isEmpty() && Prec(ch) <= Prec(st.peek())) {
result += st.peek();
st.pop();
}
st.push(ch);
}
}
while (!st.isEmpty()) {
if (st.peek() == '(') {
System.out.println("Invalid Expression");return;}
result += st.peek();
if(!st.isEmpty())
st.pop();
}
System.out.println(result);
}
public static int Postfixevaluation(String expr)
{
int res=0; int i;
Stack<Integer> st = new Stack();
for(i=0; i<expr.length(); i++)
{
char ch= expr.charAt(i);
if(Character.isDigit(ch)){
st.push(ch-'0');
}
else
{
int v1=st.pop();
int v2=st.pop();
switch(ch)
{
case '+':
st.push(v1+v2);
break;
case '-':
st.push(v2-v1);
break;
case '*':
st.push(v1*v2);
break;
case '/':
st.push(v2/v1);
break;
}
}
}
res=st.pop();
return res;
}
}
public class StackApplications {
public static void main(String args[])
{
Applications obj = new Applications();
Scanner sc = new Scanner(System.in);
String paranthesis="";
String itop="";
String pe="";
System.out.println("Enter the paranthsesis string");
paranthesis = sc.nextLine();
System.out.println("Enter the infixtopostfix conversion string");
itop = sc.nextLine();
System.out.println("Enter the postfix evaluation string");
pe = sc.nextLine();
boolean isvalidd= obj.ValidParanthesis(paranthesis);
int result =obj.Postfixevaluation(pe);
obj.InfixtoPostfix(itop);
}
}