-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinearEquationSolver.java
More file actions
79 lines (79 loc) · 3.11 KB
/
Copy pathLinearEquationSolver.java
File metadata and controls
79 lines (79 loc) · 3.11 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
/* A linear equation is an equation taht may be put in the form a * x + b = c, where x is the
variable a,b,c are the coefficients, which are often real numbers.
the solution of such an equation is the value that, when substituted to the unknown (the x), make
the equality true.
input:
2x - 3 = 1
output:
x = 2
input:
3 = 1 + 2x
output:
x = 1
input:
3x - 4 = 2
output:
x = 2
*/
import java.util.*;
public class LinearEquationSolver{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String str = in.nextLine();
str = str.replaceAll("[^0-9x\\-\\+=]", "");
String strsplit[] = str.split("=");
if (strsplit[0].contains("+")||strsplit[0].contains("-")||strsplit[0].contains("*")||strsplit[0].contains("/")) {
if (strsplit[0].contains("+")) {
String subsplit[] = strsplit[0].split("+");
int res;
res = Integer.parseInt(subsplit[1])-Integer.parseInt(strsplit[1]);
subsplit[0] = subsplit[0].replaceAll("x", "");
System.out.println("x = "+res/Integer.parseInt(subsplit[0]));
}else if (strsplit[0].contains("-")) {
String subsplit[] = strsplit[0].split("-");
int res;
res = Integer.parseInt(subsplit[1])+Integer.parseInt(strsplit[1]);
subsplit[0] = subsplit[0].replaceAll("x", "");
System.out.println("x = "+res/Integer.parseInt(subsplit[0]));
}else if (strsplit[0].contains("*")) {
String subsplit[] = strsplit[0].split("*");
int res;
res = Integer.parseInt(subsplit[1])/Integer.parseInt(strsplit[1]);
subsplit[0] = subsplit[0].replaceAll("x", "");
System.out.println("x = "+res/Integer.parseInt(subsplit[0]));
}else if (strsplit[0].contains("/")) {
String subsplit[] = strsplit[0].split("/");
int res;
res = Integer.parseInt(subsplit[1])*Integer.parseInt(strsplit[1]);
subsplit[0] = subsplit[0].replaceAll("x", "");
System.out.println("x = "+res/Integer.parseInt(subsplit[0]));
}
}else{
if (strsplit[1].contains("+")) {
String subsplit[] = strsplit[1].split("\\+");
int res;
res = Integer.parseInt(strsplit[0])-Integer.parseInt(subsplit[0]);
subsplit[1] = subsplit[1].replaceAll("x", "");
System.out.println("x = "+res/Integer.parseInt(subsplit[1]));
}else if (strsplit[1].contains("-")) {
String subsplit[] = strsplit[1].split("\\-");
int res;
res = Integer.parseInt(strsplit[0])+Integer.parseInt(subsplit[0]);
subsplit[1] = subsplit[1].replaceAll("x", "");
System.out.println("x = "+res/Integer.parseInt(subsplit[1]));
}else if (strsplit[1].contains("*")) {
String subsplit[] = strsplit[1].split("\\*");
int res;
res = Integer.parseInt(strsplit[0])/Integer.parseInt(subsplit[0]);
subsplit[1] = subsplit[1].replaceAll("x", "");
System.out.println("x = "+res/Integer.parseInt(subsplit[1]));
}else if (strsplit[1].contains("/")) {
String subsplit[] = strsplit[1].split("\\/");
int res;
res = Integer.parseInt(strsplit[0])*Integer.parseInt(subsplit[0]);
subsplit[1] = subsplit[1].replaceAll("x", "");
System.out.println("x = "+res/Integer.parseInt(subsplit[1]));
}
}
}
}