-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand line calculator.java
111 lines (89 loc) · 3.44 KB
/
command line calculator.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
package ExamPackage;
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("--- Command-Line Calculator ---");
for (int i = 0; i < 3; i++) { // Allow 3 expressions
System.out.println("Enter a mathematical expression:");
String expression = scanner.nextLine();
try {
double result = evaluate(expression);
System.out.println("Result: " + result);
} catch (Exception e) {
System.out.println("Invalid expression. Please try again.");
}
}
scanner.close();
}
// Evaluate the expression
private static double evaluate(String expression) {
return parseExpression(expression.replaceAll("\\s", ""), 0).value;
}
// Parse expression and handle operator precedence
private static Result parseExpression(String expr, int index) {
Result current = parseTerm(expr, index);
while (current.index < expr.length() &&
(expr.charAt(current.index) == '+' || expr.charAt(current.index) == '-')) {
char operator = expr.charAt(current.index);
Result next = parseTerm(expr, current.index + 1);
if (operator == '+') {
current.value += next.value;
} else {
current.value -= next.value;
}
current.index = next.index;
}
return current;
}
// Parse term for * and / operators
private static Result parseTerm(String expr, int index) {
Result current = parseFactor(expr, index);
while (current.index < expr.length() &&
(expr.charAt(current.index) == '*' || expr.charAt(current.index) == '/')) {
char operator = expr.charAt(current.index);
Result next = parseFactor(expr, current.index + 1);
if (operator == '*') {
current.value *= next.value;
} else {
current.value /= next.value;
}
current.index = next.index;
}
return current;
}
// Parse factor, including handling parentheses
private static Result parseFactor(String expr, int index) {
if (expr.charAt(index) == '(') {
Result inner = parseExpression(expr, index + 1);
if (inner.index >= expr.length() || expr.charAt(inner.index) != ')') {
throw new IllegalArgumentException("Mismatched parentheses");
}
inner.index++;
return inner;
}
return parseNumber(expr, index);
}
// Parse a number
private static Result parseNumber(String expr, int index) {
int startIndex = index;
while (index < expr.length() &&
(Character.isDigit(expr.charAt(index)) || expr.charAt(index) == '.')) {
index++;
}
if (startIndex == index) {
throw new IllegalArgumentException("Expected a number at index " + startIndex);
}
double value = Double.parseDouble(expr.substring(startIndex, index));
return new Result(value, index);
}
// Helper class to return both value and index
private static class Result {
double value;
int index;
Result(double value, int index) {
this.value = value;
this.index = index;
}
}
}