-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedStackTest.java
More file actions
162 lines (133 loc) · 5.63 KB
/
LinkedStackTest.java
File metadata and controls
162 lines (133 loc) · 5.63 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
159
160
161
162
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import org.junit.Test;
public class LinkedStackTest {
/** Converts the input infix expression to a postfix expression.
* @param infix The infix input to be converted to postfix.
* @return char[] The converted postfix expression.
*/
public static String convertToPostfix(String infix) {
// create a new stack, whose data are of the character type
StackInterface<Character> operatorStack = new LinkedStack<>();
String postfix = "";
char topOperator = ' ';
char[] infixChar = new char[25];
int infixLength = 0;
int currentPosition = 0;
int nextCharPreced = 0;
int peekPreced = 0;
if (infix != null && !infix.isEmpty()) {
infixChar = infix.toCharArray();
infixLength = infix.length();
} else {
System.out.println("Input string infix is either empty/null!");
}
while (currentPosition < infixLength) {
//postfixChar = postfix.toCharArray();
char nextCharacter = infixChar[currentPosition];
// determine the precedence of the operator at the top of the stack
if (!operatorStack.isEmpty()) {
peekPreced = getPrecedence(operatorStack.peek());
}
// determine the precedence of nextCharacter
nextCharPreced = getPrecedence(nextCharacter);
// determine what operator is next, and perform actions accordingly
if (nextCharacter >= 'a' && nextCharacter <= 'z') {
postfix += nextCharacter;
currentPosition++;
} else if (nextCharacter == '^' || nextCharacter == '(') {
operatorStack.push(nextCharacter);
currentPosition++;
} else if (nextCharacter == '+' || nextCharacter == '-' || nextCharacter == '*' || nextCharacter == '/') {
while (!operatorStack.isEmpty() && nextCharPreced <= peekPreced) {
postfix += operatorStack.peek();
operatorStack.pop();
}
operatorStack.push(nextCharacter);
currentPosition++;
} else if (nextCharacter == ')') {
topOperator = operatorStack.pop();
while (topOperator != '(') {
postfix = postfix + topOperator;
topOperator = operatorStack.pop();
}
currentPosition++;
} else {
currentPosition++;
}
}
while (!operatorStack.isEmpty()) {
topOperator = operatorStack.pop();
postfix = postfix + topOperator;
}
return postfix;
} // end of ConvertToPostfix
/**
* Determines the precedence of a given operator.
* @param operator The operator whose precedence will be determined.
* @return Integer value of the operator's precedence.
*/
// Unit Testing for this method is in this file, since it is private
private static int getPrecedence(char operator) {
int precedence = 0;
switch (operator) {
case '*':
precedence = 2;
break;
case '/':
precedence = 2;
break;
case '+':
precedence = 1;
break;
case '-':
precedence = 1;
break;
default:
break;
}
return precedence;
} // end of getPrecedence
public static void main(String[] args) {
/**
* Demo the functionality of the algorithm convertToPostfix
*/
// infix expressions to be converted to postfix for demo
String infix1 = "a/b*(c+(d-e))";
String infix2 = "a*b/(c-a)+d*e";
String postfix1 = convertToPostfix(infix1);
String postfix2 = convertToPostfix(infix2);
// outputing results formated to show the starting input and the output of algorithm
System.out.println("Coverting infix to postfix: \n");
System.out.println("Infix: " + infix1 + " -> Postfix: " + postfix1 + "\n");
System.out.println("Infix: " + infix2 + " -> Postfix: " + postfix2 + "\n");
}
/**
* Testing getPrecedence (Private method)
*/
// Testing getPrecedence with valid operator input
@Test
public void getPrecedence_ValidInput() {
char[] operatorArray = {'+','-','*','/','(',')'};
int expectedValue1 = 1;
int expectedValue2 = 2;
int expectedValue3 = 0;
assertEquals(expectedValue1, LinkedStackTest.getPrecedence(operatorArray[0]));
assertEquals(expectedValue1, LinkedStackTest.getPrecedence(operatorArray[1]));
assertEquals(expectedValue2, LinkedStackTest.getPrecedence(operatorArray[2]));
assertEquals(expectedValue2, LinkedStackTest.getPrecedence(operatorArray[3]));
assertEquals(expectedValue3, LinkedStackTest.getPrecedence(operatorArray[4]));
assertEquals(expectedValue3, LinkedStackTest.getPrecedence(operatorArray[5]));
}
// Testing getPrecedence with invalid input
@Test
public void getPrecedence_InvalidInput() {
char invalidOperator1 = '=';
char invalidOperator2 = '`';
char invalidOperator3 = 'b';
int expectedResult = 0;
assertEquals(expectedResult,getPrecedence(invalidOperator1));
assertEquals(expectedResult,getPrecedence(invalidOperator2));
assertEquals(expectedResult,getPrecedence(invalidOperator3));
}
}