-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEquation.java
100 lines (96 loc) · 2.47 KB
/
Equation.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
//+3x +3y -4z = 2
import java.util.*;
class Equation
{
private String EQ;
Variable FVar;
double satSol;
public Equation()
{
FVar = null;
satSol = 0.0d;
EQ = "";
}
public void input()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter Your Equation: ");
EQ = sc.nextLine();
EQ = EQ.trim();
}
public void standardizeEq()
{
for(int i = 1; i < EQ.length(); i++)
{
if(Character.isLetter(EQ.charAt(i)) == true)
{
if(Character.isDigit(EQ.charAt(i - 1)) == false)
{
EQ = EQ.substring(0, i) + "1" + EQ.substring(i);
}
}
}
if(Character.isLetter(EQ.charAt(0)) == true)
EQ = "+1" + EQ;
else if(Character.isDigit(EQ.charAt(0)) == true)
EQ = "+" + EQ;
else if(Character.isLetter(EQ.charAt(1)) == true) //Doubt.!
EQ = EQ.charAt(0) + "1" + EQ.substring(2);
//Putting Proper Spaces.
for(int i = 0; i < EQ.length() - 2; i++)
{
if(Character.isLetter(EQ.charAt(i)) == true)
{
if(EQ.charAt(i + 1) != ' ')
{
EQ = EQ.substring(0, i + 1) + " " + EQ.substring(i + 1);
}
}
}
}
public void createEq()
{
StringTokenizer st = new StringTokenizer(EQ);
int c = st.countTokens();
for(int i = 1; i <= c - 1; i++)
{
String v = st.nextToken();
String coef = v.substring(1, v.length() - 1);
Variable V = new Variable(v.charAt(0), Double.parseDouble(coef), v.charAt(v.length() - 1));
addVar(V);
}
}
public void addVar(Variable V)
{
Variable p = FVar;
if(FVar == null)
FVar = V;
else
{
while(p.nextVar != null)
p = p.nextVar;
p.nextVar = V;
}
}
public void getSatSol()
{
int i;
for(i = 0; i < EQ.length(); i++)
{
if(EQ.charAt(i) == '=')
break;
}
satSol = Double.parseDouble(EQ.substring(i + 1));
}
public void display()
{
System.out.println(EQ);
}
public void getEquation()
{
input();
standardizeEq();
createEq();
getSatSol();
}
}