-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImprovedCalculator.java
More file actions
172 lines (163 loc) · 6.42 KB
/
ImprovedCalculator.java
File metadata and controls
172 lines (163 loc) · 6.42 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
163
164
165
166
167
168
169
170
171
172
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package improvedcalculator;
import java.util.Scanner;
/**
*
* @author shanm
*/
public class ImprovedCalculator {
private static final String[] op = new String[]{"{", "}", "(", ")", "^", "*", "/", "+", "-"};
/**
* private constructor to prevent user from making objects of this class
*/
private ImprovedCalculator() {}
/**
* Method that preps the inputted string and calculates the expression using other helper methods
* @param s, a mathematical equation that needs to be solved
* @return a double which is the answer to the equation
*/
public static double calculate(String s) {
String str = "";
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (operator(c)) {
str += " " + c + " ";
} else {
str += c;
}
}
str = str.trim();
String output = postFix(str);
double result = evaluatePostfix(output);
return result;
}
/**
* Helper method which checks if the parameter is an operator
* @param c, the character being checked if its an operator or not
* @return true if the character is an operator
*/
private static boolean operator(char c) {
for (String operator : op) {
if (operator.charAt(0) == c) {
return true;
}
}
return false;
}
/**
* Helper method that converts inFix notation to postFix notation
* @param str which represents the infix notation
* @return a String which represents the postfix notation
*/
private static String postFix(String str) {
MyStack<String> operators = new MyStack<>();
String output = "";
Scanner sc = new Scanner(str);
while (sc.hasNext()) {
String nxt = sc.next().trim();
if (isNumber(nxt)) {
output += nxt + " ";
} else if (nxt.equals("(")) {
operators.push(nxt);
} else if (nxt.equals(")")) {
while (!operators.isEmpty() && !operators.peek().equals("(")) {
output += operators.pop() + " ";
}
if (!operators.isEmpty()) {
operators.pop();
}
} else if (!nxt.isEmpty() && operator(nxt.charAt(0))) {
while (!operators.isEmpty() && comparePrecedence(operators.peek(), nxt)) {
output += operators.pop() + " ";
}
operators.push(nxt);
}
}
while (!operators.isEmpty()) {
output += operators.pop() + " ";
}
return output;
}
/**
* Helper method which checks if a string is a number
* @param s, the string being checked for a number
* @return true if the string represents a number
*/
private static boolean isNumber(String s) {
if (s == null || s.isEmpty()) {
return false;
}
boolean deci = false;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == '.') {
deci = true;
}
else if (Character.isDigit(s.charAt(i)) == false) {
return false;
}
}
return true;
}
/**
* A helper method that compares the precedence of two operators
* @param top which is the last operator on the stack
* @param current is the current operator being evaluated
* @return
*/
private static boolean comparePrecedence(String top, String current) {
if (top.equals("^") && current.equals("^")) {
return false;
}
if (top.equals("^")) {
return true;
}
if (top.equals("*") || top.equals("/")) {
if (current.equals("+") || current.equals("-")) {
return true;
}
if (current.equals("*") || current.equals("/")) {
return true;
}
}
if (top.equals("+") || top.equals("-")) {
if (current.equals("+") || current.equals("-")) {
return true;
}
}
return false;
}
/**
* Helper method which solves the postFix equation returned by the postFix method
* @param postfix, A string that represents a postfix expression
* @return a double which represents the calculated value of the postFix expression
*/
private static double evaluatePostfix(String postfix) {
MyStack<Double> stack = new MyStack<>();
Scanner sc = new Scanner(postfix);
while (sc.hasNext()) {
String operator = sc.next();
if (isNumber(operator)) {
stack.push(Double.parseDouble(operator));
} else {
double b = stack.pop();
double a = stack.pop();
if (operator.equals("+")) {
stack.push(a + b);
} else if (operator.equals("-")) {
stack.push(a - b);
} else if (operator.equals("*")) {
stack.push(a * b);
} else if (operator.equals("/")) {
stack.push(a / b);
} else if (operator.equals("^")) {
stack.push(Math.pow(a, b));
}
}
}
return stack.pop();
}
}