-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConverter.java
More file actions
151 lines (111 loc) · 3.6 KB
/
Converter.java
File metadata and controls
151 lines (111 loc) · 3.6 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
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
public class Converter {
public static String toPostFix(String infixString){
char[] charString = infixString.toCharArray();
String[] parsedInfix = parse(charString);
String postfixString ="";
LinkedStack<String> operatorStack = new LinkedStack<String>();
for(int i = 0; i < parsedInfix.length; i++){
if(parsedInfix[i] != null){
//add to postfix if operand
if(isNumeric(parsedInfix[i])){
postfixString = postfixString + parsedInfix[i] + " ";
//System.out.println(postfixString);
} else if(parsedInfix[i].equals("(")){ //check if (
operatorStack.push(parsedInfix[i]);
//System.out.println("pushed " + parsedInfix[i]);
} else if(parsedInfix[i].equals(")")){ //if ), pop and add to postfixString until )
//System.out.println("popping 31" + operatorStack.peek());
while (!operatorStack.isEmpty() && !operatorStack.peek().equals("(")){
postfixString = postfixString + operatorStack.pop() + " ";
//System.out.println(postfixString);
}
//System.out.println("peek 37 " + operatorStack.peek());
operatorStack.pop();
} else {
while(!operatorStack.isEmpty() && weight(operatorStack.peek()) >= weight(parsedInfix[i])){
//System.out.println("popping 43" + operatorStack.peek());
postfixString = postfixString + operatorStack.pop() + " ";
//System.out.println(postfixString);
}
operatorStack.push(parsedInfix[i]);
//System.out.println("pushed " + parsedInfix[i]);
}
}
}
while (!operatorStack.isEmpty()){
//System.out.println("popping last" + operatorStack.peek());
postfixString += operatorStack.pop() + " ";
//System.out.println(postfixString);
}
return postfixString;
}// end of method
private static int weight(String operator){
int result = 0;
switch(operator){
case "+":
//System.out.println("+ detected");
result = 1;
break;
case "-":
//System.out.println("- detected");
result = 1;
break;
case "*":
//System.out.println("* detected");
result = 2;
break;
case "/":
//System.out.println("/ detected");
result = 2;
break;
case "^":
//System.out.println("^ detected");
result = 3;
break;
}
return result;
}
private static String[] parse(char[] input) {
int inputLength = input.length;
LinkedStack<String> expStack = new LinkedStack<String>();
for (int i = 0; i < inputLength; ++i) {
char c = input[i];
if (Character.isDigit(c)) {
String number = input[i] + "";
for (int j = i + 1; j < inputLength; ++j) {
if (Character.isDigit(input[j])) {
number += input[j];
i = j;
} else {
break;
}
}
//System.out.println(number);
//System.out.println("pos "+ i);
expStack.push(number+"");
} else if (c == '*' || c == '/' ||
c == '+' || c == '^' ||
c == '-' || c == '(' || c == ')') {
//System.out.println(c);
//System.out.println("pos "+ i);
expStack.push(c+"");
}
}
String[] parsed = new String[expStack.size()];
for(int i = expStack.size()-1; i > -1; i--){
parsed[i] = expStack.pop();
}
return parsed;
}
//check to see if string is a number
private static boolean isNumeric(String s){
try{
double d = Double.parseDouble(s);
}
catch(NumberFormatException nfe)
{
return false;
}
return true;
}
}