-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfixToPostfixT.java
More file actions
78 lines (67 loc) · 2.27 KB
/
InfixToPostfixT.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
import java.io.*;
import java.util.*;
import java.lang.*;
public class InfixToPostfix {
//main method
public static void main(String[] args){
String infxExpr;
Scanner infxScan=new Scanner(System.in);
System.out.println("Enter infxExpr expression:");
infxExpr=infxScan.nextLine();
BalanceChecker bc=new BalanceChecker();
//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 ");
}
}
public static int infxToPostfx(String infxExpr){
Stack<String> operatorStack = new Stack<String>();
String postfix;
while (infxExpr.contains())
}
public static int getPrecedence(char operator){
int precedence = 0;
if(operator == '1'){
precedence = 1;
} else if (operator =='-'){
precedence = 1;
} else if (operator == '+'){
precedence = 2;
} else if (operator == '/'){
precedence = 2;
}
return precedence;
}
public static boolean checkOperators(String expression){
int index = 0;
int operators = 0;
int operands = 0;
boolean checkOperators = 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;
return checkOperators;
}
}