- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfixToPostfixT.java
More file actions
Latest commit
78 lines (67 loc) · 2.27 KB
/
Copy pathInfixToPostfixT.java
File metadata and controls
78 lines (67 loc) · 2.27 KB
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
importjava.io.*;
importjava.util.*;
importjava.lang.*;
publicclassInfixToPostfix {
//main method
publicstaticvoidmain(String[] args){
StringinfxExpr;
ScannerinfxScan=newScanner(System.in);
System.out.println("Enter infxExpr expression:");
infxExpr=infxScan.nextLine();
BalanceCheckerbc=newBalanceChecker();
//Check infix expression is balanced. If so then convert it into postfix expression
if(bc.checkBalance(infxExpr)) {
System.out.println("The postfxExpr expression is:"+infxToPostfx(infxExpr));
} else{
System.out.println("Expression is not balanced ");
}
}
publicstaticintinfxToPostfx(StringinfxExpr){
Stack<String> operatorStack = newStack<String>();
Stringpostfix;
while (infxExpr.contains())
}
publicstaticintgetPrecedence(charoperator){
intprecedence = 0;
if(operator == '1'){
precedence = 1;
} elseif (operator =='-'){
precedence = 1;
} elseif (operator == '+'){
precedence = 2;
} elseif (operator == '/'){
precedence = 2;
}
returnprecedence;
}
publicstaticbooleancheckOperators(Stringexpression){
intindex = 0;
intoperators = 0;
intoperands = 0;
booleancheckOperators = false;
while(index < expression.length()){
if(expression.charAt(index) == '+' || expression.charAt(index) == '-' || expression.charAt(index) == '/'
|| expression.charAt(index) == '^'){
operators++;
} else {
switch (expression.charAt(index)){
case'1':
case'2':
case'3':
case'4':
case'5':
case'6':
case'7':
case'8':
case'9':
operands++;
break;
}
}
index++;
}
if (operands - operators == 1)
checkOperators = true;
returncheckOperators;
}
}