-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInfixCalculator.java
More file actions
125 lines (112 loc) · 3.91 KB
/
InfixCalculator.java
File metadata and controls
125 lines (112 loc) · 3.91 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
//This class evaluate the RPN expression (postfix).
/* Extra Credit:do not require blanks (i.e. allow users to input "5+7*2" as well as "5 + 7 * 2" ) 5% must
be documented - it's your responsibility to point it out)
Extra Credit : support negative and positive numbers - unary +/- (i.e.allow users to input " -5 + -7 *
+2" ) 5% must be documented - it's your responsibility to point it out)
Extra Credit : modulo (use "%" operator same association as in JAVA ) 5% must be documented - it's
your responsibility to point it out
*/
import java.util.Scanner;
public interface InfixCalculator{
// Below is the user input and the result:
public static void main(String[] args) {
Scanner equation = new Scanner(System.in);
System.out.println("Enter the equation: ");
String equ = equation.nextLine();
if(equ.charAt(0)=='+' || equ.charAt(0)=='-') { // This is the second extra credit that support negative and positive numbers.
System.out.println("The result of this equation is "+ evalExp2(equ));
}
else { // This is only for positive number.
System.out.println("The result of this equation is "+ evalExp(equ));
}
equation.close();
}
/* exp is the expression string input from the keyboard and the
Stirng returned is the display of the calculator
*/
//I changed this evalExp method into static method.
public static String evalExp(String exp) {
Queue<Object> postfix= InfixtoPostfix.InfixToPostfix(exp);
Stack<Double> stack = new Stack<>();
while(!postfix.isEmpty()){
Object token = postfix.dequeue();
if(token.equals('+')) {
double num1 = stack.pop();
double num2 = stack.pop();
stack.push(num2+num1);
}
else if(token.equals('-')){
double num1 = stack.pop();
double num2 = stack.pop();
stack.push(num2-num1);
}
else if(token.equals('*')){
double num1 = stack.pop();
double num2 = stack.pop();
stack.push(num2*num1);
}
else if(token.equals('/')){
double num1 = stack.pop();
double num2 = stack.pop();
stack.push(num2/num1);
}
else if(token.equals('^')){
double num1 = stack.pop();
double num2 = stack.pop();
stack.push(Math.pow(num2, num1));
}
else if(token.equals('%')) { // This is the third extra credit: modulo (%).
double num1 = stack.pop();
double num2 = stack.pop();
stack.push(num2%num1);
}
else {
stack.push(Double.valueOf((String) token));
}
}
return String.valueOf(stack.peek());
}
/****************************************************************************************************************/
//This is for the second extra credit that support negative and positive numbers.
public static String evalExp2(String exp) {
Queue<Object> postfix= InfixtoPostfix.IntoPost2(exp);
Stack<Double> stack = new Stack<>();
while(!postfix.isEmpty()){
Object token = postfix.dequeue();
if(token.equals('+')) {
double num1 = stack.pop();
double num2 = stack.pop();
stack.push(num2+num1);
}
else if(token.equals('-')){
double num1 = stack.pop();
double num2 = stack.pop();
stack.push(num2-num1);
}
else if(token.equals('*')){
double num1 = stack.pop();
double num2 = stack.pop();
stack.push(num2*num1);
}
else if(token.equals('/')){
double num1 = stack.pop();
double num2 = stack.pop();
stack.push(num2/num1);
}
else if(token.equals('^')){
double num1 = stack.pop();
double num2 = stack.pop();
stack.push(Math.pow(num2, num1));
}
else if(token.equals('%')) { // This is the third extra credit: modulo (%).
double num1 = stack.pop();
double num2 = stack.pop();
stack.push(num2%num1);
}
else {
stack.push(Double.valueOf((String) token));
}
}
return String.valueOf(stack.peek());
}
}