-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solve.java
196 lines (181 loc) · 4.89 KB
/
Solve.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
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
public class Solve {
String expression, operator = "", holdOperator = "", logStr = "";
StringBuffer expBuff;
String[] numStr, opStr;
double num = 0, holdNum = 0, runningAnswer = 0, prevAnswer = 0;
int passCalc = 0;
public void getNegatives()
{
int index;
for (int i = 0; i < expBuff.length(); i++) {
if (i == 0) {
if (expBuff.charAt(0) == '-') {
expBuff.deleteCharAt(0);
expBuff.insert(0, "$");
}
}
else {
switch (expBuff.charAt(i)) {
case '*': if (expBuff.charAt(i+1) == '-') {
index = i+1;
expBuff.deleteCharAt(i+1);
expBuff.insert(index, "$");
}
break;
case '-': if (expBuff.charAt(i+1) == '-') {
index = i+1;
expBuff.deleteCharAt(i+1);
expBuff.insert(index, "$");
}
break;
case '+': if (expBuff.charAt(i+1) == '-') {
index = i+1;
expBuff.deleteCharAt(i+1);
expBuff.insert(index, "$");
}
break;
case '/': if (expBuff.charAt(i+1) == '-') {
index = i+1;
expBuff.deleteCharAt(i+1);
expBuff.insert(index, "$");
}
break;
case '^': if (expBuff.charAt(i+1) == '-') {
index = i+1;
expBuff.deleteCharAt(i+1);
expBuff.insert(index, "$");
}
break;
}
}
}
getArrays();
}
public void getArrays()
{
int index = 0;
passCalc = 0;
expression = expBuff.toString();
numStr = expression.split( "-|\\+|\\*|/|\\^" );
opStr = new String[ numStr.length - 1 ];
for (int v = 0; v < numStr.length; v++) {
if ( numStr[v].charAt(0) == '$') {
StringBuffer buff = new StringBuffer(numStr[v]);
buff.deleteCharAt(0);
buff.insert(0, "-");
numStr[v] = buff.toString();
}
if ( numStr[v].equals("pi") ) {
numStr[v] = Double.toString(Math.PI);
}
if ( numStr[v].length() > 1 ) {
if (numStr[v].charAt(0) == 'l') {
logStr = numStr[v];
numStr[v] = Double.toString( log() );
}
//Fix this:
else if (numStr[v].charAt(1) == 'l') {
logStr = numStr[v].substring(1);
double logResult = log();
logStr = Double.toString(logResult);
numStr[v] = logStr;
System.out.println(numStr[v]);
}
}
}
for (int m = 0; m < expression.length(); m++) {
if ( expression.charAt( m ) == '+' ) {
opStr[ index ] = "+";
index++;
}
else if ( expression.charAt( m ) == '-' ) {
opStr[ index ] = "-";
index++;
}
else if ( expression.charAt( m ) == '*' ) {
opStr[ index ] = "*";
index++;
}
else if ( expression.charAt( m ) == '/' ) {
opStr[ index ] = "/";
index++;
}
else if ( expression.charAt( m ) == '^' ) {
opStr[ index ] = "^";
index++;
}
}
sortArrays();
}
public void sortArrays()
{
ModForExponents modex = new ModForExponents();
ModPrimaryOperators mpe = new ModPrimaryOperators();
modex.modify(numStr, opStr);
numStr = modex.newNumArray;
opStr = modex.newOpArray;
mpe.modify(numStr, opStr);
numStr = mpe.newNumArray;
opStr = mpe.newOpArray;
/*//DISPLAY CONTENT OF ARRAYS
int c = 0;
while (c < numStr.length) {
System.out.print(numStr[c] + " ");
c++;
}
c = 0;
System.out.println("\n");
while (c < opStr.length) {
System.out.print(opStr[c] + " ");
c++;
}
System.out.println("\n");
//------------------------------//*/
solveArrays();
}
public void solveArrays()
{
for ( int i = 0; i < numStr.length; i++ ) {
if ( passCalc == 0 ) {
if ( numStr[i] == "n" ) {
break;
}
runningAnswer = Double.parseDouble( numStr[ i ] );
passCalc++;
}
else {
if ( numStr[i] == "n" )
break;
try {
num = Double.parseDouble( numStr[ i ] );
holdOperator = opStr[ i - 1 ];
calculate();
}
catch ( NumberFormatException e ) {
holdOperator = opStr[ i - 1 ];
calculate();
}
}
}
}
public double log()
{
String logString = logStr.substring( logStr.indexOf( 'g' ) + 1 );
double logNum = Double.parseDouble( logString );
return Math.log10( logNum );
}
public void calculate()
{
if ( holdOperator.equals( "+" ) )
runningAnswer += num;
else if ( holdOperator.equals( "-" ) )
runningAnswer = runningAnswer - num;
else if ( holdOperator.equals( "*" ) )
runningAnswer = runningAnswer * num;
else if ( holdOperator.equals( "/" ) )
runningAnswer = runningAnswer / num;
else if ( holdOperator.equals( "^" ) )
runningAnswer = Math.pow( runningAnswer, num );
return;
}
}