-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostfix.java
More file actions
158 lines (138 loc) · 3.83 KB
/
Postfix.java
File metadata and controls
158 lines (138 loc) · 3.83 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
152
153
154
155
156
157
158
/*
* Postfix Class
*
* Converts a line from infix to postfix
*
* Windows Eclipse IDE
*
* @version 1.0
*
* @author Jarred Hawkins, Kane Egan
*
* Girasol - A fire opal that glows like fire in a certain light
*
* "We all make choices, but in the end our choices make us."
* - Andrew Ryan(1902 - 1960)
*/
public class Postfix {
private static final char[] RECOGNIZED_OPERATIONS = { '-', '–', '+', '/', '*', '^' };
private static final int[] OPERATION_PRIORITIES = { 0, 0, 0, 1, 1, 2 };
private String line;
private Stackjr<Character> stack = new Stackjr<Character>();
char[] inputCharacters;
public Postfix() {
line = "";
}
// ****************************************
/**
*
* @param input
* @return
*/
public String convertToPostfix(String input) {
line = "";
stack.clear();
if (input == null || input.trim().equals("")) {
line = "Error: No input found on this line from the input file.";
return line;
}
inputCharacters = input.replaceAll(" ", "").toCharArray();
if (isOperator(inputCharacters[0]) == true) {
line = "Invalid infix syntax. A variable is required before the first operator.";
return line;
} // End If
for (int i = 0; i < inputCharacters.length; i++) {
if(i > 0) {
if (isOperator(inputCharacters[i]) && isOperator(inputCharacters[i - 1])) {
line = "Invalid infix syntax between " + (i - 1) + " and " + i
+ ". Add a variable between the operators.";
return line;
} // End If
else if (isVariable(inputCharacters[i]) && isVariable(inputCharacters[i - 1])) {
line = "Invalid infix syntax between " + (i - 1) + " and " + i
+ ". Add an operator between the variables.";
return line;
} // End If
else if (inputCharacters[i] == ')' && inputCharacters[i - 1] == '(') {
line = "Invalid infix syntax between " + (i - 1) + " and " + i + ". Remove the empty parenthesis.";
return line;
} // End If
}
if (isOperator(inputCharacters[i])) {
if (stack.isEmpty()) {
stack.push(inputCharacters[i]);
} // End If
else {
while (!stack.isEmpty()
&& (operationPriority(stack.peek()) >= operationPriority(inputCharacters[i]))) {
line += stack.pop() + " ";
} // End While
stack.push(inputCharacters[i]);
} // End Else
} // End If
else if (inputCharacters[i] == '(') {
stack.push(inputCharacters[i]);
} // End If Else
else if (inputCharacters[i] == ')') {
while (stack.peek() != '(') {
line += stack.pop() + " ";
}
stack.pop();
} // End If Else
else {
line += inputCharacters[i] + " ";
} // End Else
}
while (!stack.isEmpty()) {
if (stack.peek() == '(') {
char holder = stack.pop();
if(stack.isEmpty()) {
return "Invalid syntax. Unclosed parenthesis at the end of the infix line.";
}
} // End If
line += stack.pop() + " ";
} // End while
return line;
}
// ****************************************
/**
*
* @param input
* @return
*/
private int operationPriority(char input) {
for (int i = 0; i < RECOGNIZED_OPERATIONS.length; i++) {
if (RECOGNIZED_OPERATIONS[i] == input) {
return OPERATION_PRIORITIES[i];
} // End If
} // End If
return -1;
}
// ****************************************
/**
*
* @param input
* @return
*/
private boolean isOperator(char input) {
for (int i = 0; i < RECOGNIZED_OPERATIONS.length; i++) {
if (RECOGNIZED_OPERATIONS[i] == input) {
return true;
}
}
return false;
}
// ****************************************
/**
*
* @param input
* @return
*/
private boolean isVariable(char input) {
if (!isOperator(input) && input != '(' && input != ')') {
return true;
} else {
return false;
}
}
}