-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostfix.java
More file actions
140 lines (113 loc) · 5.23 KB
/
Postfix.java
File metadata and controls
140 lines (113 loc) · 5.23 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
// These are the packages needed
import java.util.Scanner;
import java.util.LinkedList;
import java.util.Stack;
// The Postfix class
public class Postfix {
static public void main(String[] args) {
Scanner scan = new Scanner(System.in);
String ask = "1", equation;
// Using a while loop
while (Integer.parseInt(ask) == 1) {
System.out.print("\nEnter an infix equation: ");
equation = scan.nextLine().replaceAll(" ", ""); // replaces white spaces
convertPostfix(equation);
do { // to check the input if valid or invalid
// Asks the user if wants to try more
System.out.println("\nDo you want to try more? \n[1] Yes \n[2] No");
ask = scan.nextLine().replaceAll(" ", ""); // replaces white spaces
if (invalidInputYN(ask)) { System.out.println("Invalid input! Please select again..."); }
} while (invalidInputYN(ask)); // let the user enter again if invalid
}
// Prints exit when done
System.out.println("\nExit!");
scan.close();
}
// The convertPostfix method
public static void convertPostfix(String equation) { // an equation is the parameter
LinkedList<String> result = new LinkedList<String>();
Stack<Character> operator = new Stack<>();
String value = ""; // the variable for multiple-digit values
// Reads each character of an equation
for (int x = 0; x < equation.length(); x++) {
char ch = equation.charAt(x); // the variable for a character
// Checks if the character is a digit
if (Character.isDigit(ch) || dotOp(ch)) {
value = value.concat(Character.toString(ch));
System.out.println("Value: " + value);
if ((x < equation.length()-1 && !Character.isDigit(equation.charAt(x+1)) && !dotOp(equation.charAt(x+1))) || x == equation.length()-1) {
result.addLast(value);
value = "";
if (x >= 2) {
if (equation.charAt(x-2) == ')') {
if (checkPrecedence(operator.peek()) == 2)
result.addLast(String.valueOf(operator.pop()));
}
}
}
}
// Checks if the character is an open parenthesis
else if (ch == '(')
operator.push(ch);
// Checks if the character is a close parenthesis
else if (ch == ')') {
while (operator.peek() != '(') {
result.addLast(String.valueOf(operator.pop())); // adds operator to result
}
operator.pop();
}
// Checks if the character is an operator
else if (isOperator(ch)) {
if (x != 0 && operator.isEmpty()) { operator.push(ch); }
// Checks if the operator is a negative sign
else if ((x == 0) || (x >= 1 && equation.charAt(x-1) == '(') || (isOperator(equation.charAt(x-1)))) {
value = value.concat(Character.toString(ch));
}
else {
while (!operator.isEmpty() && checkPrecedence(ch) <= checkPrecedence(operator.peek())) {
result.addLast(String.valueOf(operator.pop())); // adds operator to result
}
operator.push(ch);
}
}
// To check if all the characters are separated correctly
System.out.println("\nCurrent Character: " + ch);
System.out.println("Result: " + result);
System.out.println("Operator: " + operator);
}
System.out.println();
// Adds the left character in operators to operands
while (!operator.isEmpty()) result.addLast(String.valueOf(operator.pop()));
System.out.print("Postfix: ");
for (String x : result)
System.out.print(x + " ");
System.out.println();
}
// The isOperator method checks if the character is an operator
public static boolean isOperator(char ch) {
return ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '^';
}
// The dotOp method checks if the character is a dot
public static boolean dotOp(char ch) {
return ch == '.';
}
// The checkPrecedence method checks if the operator has high/low precedence
public static int checkPrecedence(char operator) {
switch (operator) {
case '^':
return 3;
case '*':
case '/':
return 2;
case '+':
case '-':
return 1;
default:
return 0;
}
}
// invalidInputYN method with an input as a parameter
public static boolean invalidInputYN(String _input) {
return ((_input.length() != 1 || !Character.isDigit(_input.charAt(0))) || (Integer.parseInt(_input) != 1 && Integer.parseInt(_input) != 2));
}
}