-
Notifications
You must be signed in to change notification settings - Fork 0
/
Converter.java
211 lines (178 loc) · 8.13 KB
/
Converter.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
public class Converter {
public static int Precedence(char operator) {
if(operator == '(' || operator == ')') return 10;
if(operator == '!' || operator == '~') return 9;
if(operator == '^') return 8;
if(operator == '*' || operator == '/' || operator == '%') return 7;
if(operator == '+' || operator == '-') return 6;
if(operator == '<' || operator == '>') return 5;
if(operator == '&') return 4;
if(operator == '|') return 3;
if(operator == '=') return 1;
else return 0;
}
public static int isValid(char operator) {
if(operator == '!' || operator == '~' || operator == '^' || operator == '*' || operator == '/' || operator == '%' || operator == '+' || operator == '-'
|| operator == '<' || operator == '>' || operator == '&' || operator == '|' || operator == '=') {
return 1;
} else return 0;
}
public static String ONPtoINF(String phrase) {
if(phrase == null) return "error";
StringBuilder builder = new StringBuilder();
int operandCount = 0;
int operatorCount = 0;
for (int i = 0; i < phrase.length(); i++) {
boolean validChar = (phrase.charAt(i) >= 'a' && phrase.charAt(i) <= 'z');
if (isValid(phrase.charAt(i)) == 1 || validChar) {
if (isValid(phrase.charAt(i)) == 1 && phrase.charAt(i) != '!' && phrase.charAt(i) != '~' && phrase.charAt(i) != '^') {
operatorCount++;
}
if (validChar) {
operandCount++;
}
builder.append(phrase.charAt(i));
}
}
if (operatorCount >= operandCount) return "error";
String result = builder.toString();
StringStack stackOfStrings = new StringStack();
IntStack stackOfInts = new IntStack();
CharStack stackOfChars = new CharStack();
for (int i = 0; i < result.length(); i++) {
char currentChar = result.charAt(i);
if (currentChar >= 'a' && currentChar <= 'z') {
stackOfStrings.push(String.valueOf(currentChar));
stackOfInts.push(11);
stackOfChars.push('r');
}
else if (currentChar == '~' || currentChar == '!') {
if (stackOfStrings.isEmpty()) return "error";
String topString = stackOfStrings.pop();
int topPrecedence = stackOfInts.pop();
stackOfChars.pop();
if (Precedence(currentChar) <= topPrecedence) {
stackOfStrings.push(currentChar + " " + topString);
} else {
stackOfStrings.push(currentChar + " ( " + topString + " )");
}
stackOfInts.push(9);
stackOfChars.push('r');
} else {
if (stackOfStrings.size() < 2) return "error";
String rightOperand = stackOfStrings.pop();
int rightPrecedence = stackOfInts.pop();
stackOfChars.pop();
String leftOperand = stackOfStrings.pop();
int leftPrecedence = stackOfInts.pop();
stackOfChars.pop();
int currentPrecedence = Precedence(result.charAt(i));
char currentAssociativity = (result.charAt(i) == '~' || result.charAt(i) == '!' ||
result.charAt(i) == '=' || result.charAt(i) == '^') ? 'r' : 'l';
if (currentPrecedence > rightPrecedence ||
(currentPrecedence == rightPrecedence && currentAssociativity == 'l')) {
rightOperand = "( " + rightOperand + " )";
}
if (currentPrecedence > leftPrecedence) {
leftOperand = "( " + leftOperand + " )";
}
String clean = leftOperand + " " + result.charAt(i) + " " + rightOperand;
stackOfStrings.push(clean);
stackOfInts.push(currentPrecedence);
stackOfChars.push(currentAssociativity);
}
}
if (stackOfStrings.size() != 1) return "error";
phrase = stackOfStrings.top();
return phrase;
}
public static String INFtoONP(String phrase) {
if (phrase == null) return "error";
StringBuilder builder = new StringBuilder();
int state = 0;
int operatorCounter = 0;
int operandCounter = 0;
int parenthesesCounter = 0;
for (int i = 0; i < phrase.length(); i++) {
char currentChar = phrase.charAt(i);
if (!(isValid(currentChar) == 1 || (currentChar >= 'a' && currentChar <= 'z') || currentChar == '(' || currentChar == ')')) {
continue;
}
builder.append(currentChar);
if (currentChar == '~' || currentChar == '!') {
if (state == 1) return "error";
else state = 2;
}
if (isValid(currentChar) == 1 && currentChar != '~' && currentChar != '!') {
if (state == 0 || state == 2) return "error";
else state = 0;
if (currentChar != '^') {
operatorCounter++;
}
}
if (currentChar >= 'a' && currentChar <= 'z') {
if (state == 1) return "error";
else state = 1;
operandCounter++;
}
if (currentChar == '(') {
parenthesesCounter++;
if (state == 1) return "error";
else state = 0;
}
else if (currentChar == ')') {
if (state == 0 || state == 2) return "error";
state = 1;
parenthesesCounter--;
}
}
if ((state != 1) || (parenthesesCounter != 0) || (operatorCounter >= operandCounter)) return "error";
StringBuilder answer = new StringBuilder();
CharStack mainStack = new CharStack();
for (int i = 0; i < phrase.length(); i++) {
if (phrase.charAt(i) >= 'a' && phrase.charAt(i) <= 'z') {
String aux = Character.toString(phrase.charAt(i));
answer.append(aux);
answer.append(" ");
}
else if (phrase.charAt(i) == '(') {
mainStack.push(phrase.charAt(i));
}
else if (isValid(phrase.charAt(i)) == 1 && phrase.charAt(i) != '!' && phrase.charAt(i) != '~' && phrase.charAt(i) != '^' && phrase.charAt(i) != '=') {
while (!mainStack.isEmpty() && Precedence(phrase.charAt(i)) <= Precedence(mainStack.top()) && Precedence(mainStack.top()) != 10) {
String aux = String.valueOf(mainStack.pop());
answer.append(aux);
answer.append(' ');
}
mainStack.push(phrase.charAt(i));
}
else if (phrase.charAt(i) == '!' || phrase.charAt(i) == '~' || phrase.charAt(i) == '^' || phrase.charAt(i) == '=') {
while (!mainStack.isEmpty() && Precedence(phrase.charAt(i)) < Precedence(mainStack.top()) && Precedence(mainStack.top()) != 10) {
String aux = String.valueOf(mainStack.pop());
answer.append(aux);
answer.append(' ');
}
mainStack.push(phrase.charAt(i));
}
else if (phrase.charAt(i) == ')') {
while (mainStack.top() != '(' && !mainStack.isEmpty()) {
String aux = String.valueOf(mainStack.pop());
answer.append(aux);
answer.append(' ');
}
mainStack.pop();
}
}
while (!mainStack.isEmpty()) {
String aux = String.valueOf(mainStack.pop());
answer.append(aux);
answer.append(' ');
}
StringBuilder helper = new StringBuilder();
for (int i = 0; i < answer.length() - 1 ; i++) {
helper.append(answer.charAt(i));
}
phrase = helper.toString();
return phrase;
}
}