-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulSolverHandler.java
More file actions
155 lines (126 loc) · 5.24 KB
/
Copy pathSimulSolverHandler.java
File metadata and controls
155 lines (126 loc) · 5.24 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
import Scientific.SimulSolver;
import java.util.*;
public class SimulSolverHandler {
private final Scanner scanner;
public SimulSolverHandler(Scanner scanner) {
this.scanner = scanner;
}
public void startSimulSolver() {
boolean retry = true;
while (retry) {
clearScreen();
printBanner();
try {
System.out.println("1. 2x2 System of Equations");
System.out.println("2. 3x3 System of Equations");
System.out.println("3. Return to Main Menu");
System.out.print("Choose an option (1-3): ");
int choice = Integer.parseInt(scanner.nextLine().trim());
switch (choice) {
case 1:
solve2x2System();
break;
case 2:
solve3x3System();
break;
case 3:
System.out.println("Returning to Main Menu...");
retry = false;
break;
default:
System.out.println("Invalid option. Please choose 1-3.");
pauseScreen();
}
} catch (Exception e) {
System.err.println("Invalid input. Please try again.");
pauseScreen();
}
}
}
private void solve2x2System() {
clearScreen();
System.out.println("\n\033[1;34mEnter two equations in the form: ax + by = c\033[0m\n");
double[][] A = new double[2][2];
double[][] B = new double[2][1];
for (int i = 0; i < 2; i++) {
System.out.printf("Enter equation %d: ", i + 1);
String equation = scanner.nextLine().trim();
Map<String, Double> coefficients = parseEquation(equation, new String[]{"x", "y"});
A[i][0] = coefficients.getOrDefault("x", 0.0);
A[i][1] = coefficients.getOrDefault("y", 0.0);
B[i][0] = coefficients.get("constant");
}
try {
double[] solution = SimulSolver.solve2x2(A, B);
System.out.printf("Solution:\n x = %.6f, y = %.6f\n", solution[0], solution[1]);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
pauseScreen();
}
private void solve3x3System() {
clearScreen();
System.out.println("\n\033[1;34mEnter three equations in the form: ax + by + cz = d\033[0m\n");
double[][] A = new double[3][3];
double[][] B = new double[3][1];
for (int i = 0; i < 3; i++) {
System.out.printf("Enter equation %d: ", i + 1);
String equation = scanner.nextLine().trim();
Map<String, Double> coefficients = parseEquation(equation, new String[]{"x", "y", "z"});
A[i][0] = coefficients.getOrDefault("x", 0.0);
A[i][1] = coefficients.getOrDefault("y", 0.0);
A[i][2] = coefficients.getOrDefault("z", 0.0);
B[i][0] = coefficients.get("constant");
}
try {
double[] solution = SimulSolver.solve3x3(A, B);
System.out.printf("Solution:\n x = %.6f, y = %.6f, z = %.6f\n", solution[0], solution[1], solution[2]);
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
pauseScreen();
}
private Map<String, Double> parseEquation(String equation, String[] variables) {
Map<String, Double> coefficients = new HashMap<>();
coefficients.put("constant", 0.0);
equation = equation.replace(" ", "");
String[] parts = equation.split("=");
if (parts.length != 2) {
throw new IllegalArgumentException("Invalid equation format. Use 'ax + by + cz = d'.");
}
String lhs = parts[0];
double rhs = Double.parseDouble(parts[1]);
String[] terms = lhs.split("(?=[+-])");
for (String term : terms) {
term = term.trim();
if (term.isEmpty()) continue;
boolean isVariable = false;
for (String var : variables) {
if (term.contains(var)) {
isVariable = true;
String coefficient = term.replace(var, "");
double value = coefficient.isEmpty() || coefficient.equals("+") ? 1.0
: coefficient.equals("-") ? -1.0 : Double.parseDouble(coefficient);
coefficients.put(var, coefficients.getOrDefault(var, 0.0) + value);
break;
}
}
if (!isVariable) {
coefficients.put("constant", coefficients.get("constant") + Double.parseDouble(term));
}
}
coefficients.put("constant", coefficients.get("constant") - rhs);
return coefficients;
}
private void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
private void printBanner() {
System.out.println("\033[1;32m\n===== Simultaneous Equation Solver =====\033[0m");
}
private void pauseScreen() {
System.out.println("\nPress Enter to continue...");
scanner.nextLine();
}
}