-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArithmeticExpressionEvaluator.java
More file actions
139 lines (125 loc) · 5.32 KB
/
Copy pathArithmeticExpressionEvaluator.java
File metadata and controls
139 lines (125 loc) · 5.32 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
package com.company;
import java.util.*;
public class ArithmeticExpressionEvaluator {
String Exp = new String(); //initilaize postfix expression
ArrayList<String> Components = new ArrayList<>();//store postfix operators
public static boolean LowerPrecedence(char op1, char op2)//check precedence of one operator over current
{
if(op1 == op2){
return false;
}
switch (op1)
{
case '+':
case '-':
return !(op2 == '+' || op2 == '-');
case '*':
case '/':
return (op2 == '^' || op2 == '(');
case '^':
return op2 == '(';
case '(':
return true;
default:
return false;
}
}
public boolean isOperator(String x){//check if operator
if (!x.equals("-") && !x.equals("+") && !x.equals("*") && !x.equals("/") && !x.equals("^")){
return false;
}
else{
return true;
}
}
public String InfixtoPostfix(){
System.out.println("Enter your expression, with spaces between all characters that aren't part of the same number, and a space at the end");
System.out.println("Make sure all negative numbers x are represented as ( 0 - x ):");
Scanner scanner = new Scanner(System.in);
String input = scanner.nextLine();
System.out.print("Your infix input was: ");
System.out.println(input);
Stack<String> oper = new Stack<>();//stack for operators
String[] InfixArray = input.split(" ");//split expression into components
for(int i = 0; i < InfixArray.length; i++){
if (isOperator(InfixArray[i])) {//if we encounter an operator
while(!oper.empty() && (!oper.peek().equals("(")) && LowerPrecedence(InfixArray[i].charAt(0),oper.peek().charAt(0))){
Exp = Exp + oper.pop() + " ";//if the oper Stack has content and we do not encounter a parenthesis,
// we add the previous operator the Postfix expression if we encounter a lower precedence operator
}
oper.push(InfixArray[i]);//push the operator on to the stack
}
else if (InfixArray[i].equals("(")){
oper.push(InfixArray[i]);
}
else if (InfixArray[i].equals(")")){//if we encounter a closing parenthesis (which is highest priority, we immediately
//add to the postfix expression
while(!oper.empty() && (!oper.peek().equals("("))){
Exp = Exp + oper.pop() + " ";
}
oper.pop();
}
else {//we have encountered a number, and we add to the postfix expression
Exp = Exp + InfixArray[i] + " ";
}
}
while(!oper.empty()){
Exp = Exp + oper.pop() + " ";
}
String[] Postfixarray = Exp.split(" ");
for (int i = 0; i < Postfixarray.length; i++){
Components.add(Postfixarray[i]);//initialize the postfix ArrayList
}
System.out.println("The postfix output is: ");
System.out.println(Exp);
return Exp;
}
public void EvaluatePostfix(){
Stack<Double> vals = new Stack<Double>();
for (int i = 0; i < Components.size(); i++){//different cases based on operator
if (Components.get(i).equals("+")){
double v = vals.pop();
v = v + vals.pop();
vals.push(v);
}
else if (Components.get(i).equals("-")){
double v = vals.pop();
v = vals.pop() - v;
vals.push(v);
}
else if (Components.get(i).equals("*")){
double v = vals.pop();
v = v * vals.pop();
vals.push(v);
}
else if (Components.get(i).equals("/")){
double v = vals.pop();
v = vals.pop()/v;
vals.push(v);
}
else if (Components.get(i).equals("^")){
double v = vals.pop();
v = Math.pow(vals.pop(),v);
vals.push(v);
}
else {vals.push(Double.parseDouble(Components.get(i)));}//push if encountering operand
}
System.out.println("The evaluated expression is: ");
System.out.print(vals.pop());
}
public static void main(String[] args) {
ArithmeticExpressionEvaluator Simulator = new ArithmeticExpressionEvaluator();
Simulator.InfixtoPostfix();
Simulator.EvaluatePostfix();
}
}
// OUTPUT:/Library/Java/JavaVirtualMachines/jdk-10.0.1.jdk/Contents/Home/bin/java "-javaagent:/Applications/IntelliJ IDEA CE.app/Contents/lib/idea_rt.jar=58459:/Applications/IntelliJ IDEA CE.app/Contents/bin" -Dfile.encoding=UTF-8 -classpath /Users/rohandatta/IdeaProjects/Question2/out/production/Question2 com.company.ArithmeticExpressionEvaluator
//Enter your expression, with spaces between all characters that aren't part of the same number, and a space at the end
//Make sure all negative numbers x are represented as ( 0 - x ):
//( 10 + 2 ) * 3
//Your infix input was: ( 10 + 2 ) * 3
//The postfix output is:
//10 2 + 3 *
//The evaluated expression is:
//36.0
//Process finished with exit code 0