-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInfixEvaluator.java
124 lines (98 loc) · 3.75 KB
/
InfixEvaluator.java
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
import java.util.*;
public class InfixEvaluator {
public static double evaluateInfix(String infix) {
Stack<Double> valueStack = new Stack<>();
Stack<Character> operatorStack = new Stack<>();
for (int i = 0; i < infix.length(); i++) {
char token = infix.charAt(i);
if (Character.isDigit(token) || token == '.') {
StringBuilder numBuilder = new StringBuilder();
while (i < infix.length() && (Character.isDigit(infix.charAt(i)) || infix.charAt(i) == '.')) {
numBuilder.append(infix.charAt(i));
i++;
}
i--;
String numStr = numBuilder.toString();
double num = Double.parseDouble(numStr);
valueStack.push(num);
} else if (isOperator(token)) {
while (!operatorStack.isEmpty() && precedence(token) <= precedence(operatorStack.peek())) {
evaluateTop(valueStack, operatorStack);
}
operatorStack.push(token);
}
}
while (!operatorStack.isEmpty()) {
evaluateTop(valueStack, operatorStack);
}
if (valueStack.size() != 1) {
throw new IllegalArgumentException(".....Invalid expression.....");
}
return valueStack.pop();
}
private static void evaluateTop(Stack<Double> valueStack, Stack<Character> operatorStack) {
if (valueStack.size() < 2 || operatorStack.isEmpty() || operatorStack.peek() == '(') {
throw new IllegalArgumentException(".....Invalid expression.....");
}
char operator = operatorStack.pop();
double operand2 = valueStack.pop();
double operand1 = valueStack.pop();
double result;
switch (operator) {
case '+':
result = operand1 + operand2;
break;
case '-':
result = operand1 - operand2;
break;
case '*':
result = operand1 * operand2;
break;
case '/':
if (operand2 == 0) {
throw new ArithmeticException("Division by zero");
}
result = operand1 / operand2;
break;
default:
throw new IllegalArgumentException("Unsupported operator");
}
valueStack.push(result);
}
private static int precedence(char operator) {
switch (operator) {
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default:
return 0;
}
}
private static boolean isOperator(char token) {
return "+-*/".indexOf(token) != -1;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Welcome to the Command-Line Calculator!");
System.out.println("Enter an arithmetic expression (e.g., 2+3/4*2), or type 'exit' to quit.");
while (true) {
System.out.println();
System.out.print("Expression: ");
String input = sc.nextLine().trim();
if (input.equalsIgnoreCase("exit")) {
System.out.println("Goodbye!");
break;
}
try {
double result = evaluateInfix(input);
System.out.println("Result: " + result);
} catch (Exception e) {
System.out.println("Invalid expression. Please try again.");
}
}
sc.close();
}
}