-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
278 lines (234 loc) · 6.38 KB
/
Main.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Jonathan Le
* Jxl200056
*/
import java.util.Scanner;
import java.io.*;
public class Main
{
public static void main(String [] args) throws FileNotFoundException
{
Scanner scan = new Scanner(System.in);
File file;
//Make sure the correct file is placed
do
{
String filename = scan.nextLine();
file = new File(filename);
} while ( !( file.exists()) );
scan.close();
Scanner scnr = new Scanner(file); // work with file
//Instantiate the binary Tree object
BinTree<Term> bt = new BinTree<>();
//Read the file
while ( scnr.hasNext() )
{
//Local variables for the definite integral
boolean definiteIntegral = false;
int a = 0, b = 0;
int counter = 0;
//Save entire expression in a line
String line = scnr.nextLine();
//System.out.println("* " + line);
//Parse the string
int index = line.indexOf('|'); // find the index of the integral:
int spaceIndex = line.indexOf(' ', index);
double definiteValue = 0;
if ( (line.charAt(index + 1) != ' ') )
{
// Check if this value is a definite integral. In this case, it would be
definiteIntegral = true;
// Parse the string for the bounds
// First check for negative
spaceIndex = line.indexOf(' ', index); // find the first index of the space
//Go backwards and find the bounds of the integral - for b
for ( int i = spaceIndex - 1; i >= index; i--)
{
if ( Character.isDigit( line.charAt(i)))
{
int digit = Character.getNumericValue( line.charAt(i) );
b += digit * Math.pow(10, counter);
counter++;
} else if (line.charAt(i) == '-')
{
b *= (-1);
}
}
// for the bounds of a
counter = 0;
for ( int i = index; i >= 0; i--)
{
if ( Character.isDigit( line.charAt(i)))
{
int digit = Character.getNumericValue( line.charAt(i) );
a += digit * Math.pow(10, counter);
counter++;
} else if ( line.charAt(i) == '-')
{
a *= (-1);
}
}
}
line = line.substring(spaceIndex + 1); // remove the integral
//Now, need to parse the expression
while ( !(line.isEmpty() ))
{
//local variables;
int exp = 0;
double coef = 0;
int begIndex = 0;
int eIndex = 0;
int endIndex = 0;
counter = 0;
int indX = line.indexOf('x'); // find the index of the first x;
//for dx
if ( (indX >= 1) && line.charAt(indX - 1) == 'd')
{
//remove that part
line = line.substring(0, indX - 1);
if( !(line.isEmpty()))
{
//there is a constant inside
counter = 0;
for(int i = line.length() - 1; i >=0; i --)
{
if (Character.isDigit( line.charAt(i)) )
{
//Found coefficient
int digit = Character.getNumericValue( line.charAt(i));
coef += digit * Math.pow(10, counter );
counter++;
} else if ( line.charAt(i) == '-')
{
coef *= -1;
break;
}
}
//remove string
line = "";
}
}
//Find the coefficient
int positive = 1;
if ( !(line.isEmpty()))
{
for (int i = indX - 1; i >= 0; i--)
{
begIndex = i; // for substring
if ( Character.isDigit( line.charAt(i) ) )
{
int digit = Character.getNumericValue( line.charAt(i));
//There is a digit
coef += digit * Math.pow( 10, (indX - 1) - i);
} else if ( line.charAt(i) == '-')
{
coef = coef * (-1);
positive = -1;
break; // leave for loop
} else if ( line.charAt(i) == '+')
{
break;
}
}
if ( coef == 0)
{
coef = 1 * positive;
}
//Find the exponent
//go to the right side
int negative = 1;
endIndex = findSignIndex(line, indX);
//May not have a + or -
if ( endIndex == -1)
{
endIndex = line.indexOf(' ', indX); // find the next space
}
if ( line.charAt(indX + 1) != '^')
{
exp = 1;
line = line.substring(0, begIndex) + line.substring(endIndex);
}else
{
// There is a carrot hat, so need to find the exponent value
if ( line.charAt(indX + 2) == '-')
{
negative = -1;
endIndex = findSignIndex(line, indX + 3);
//May not have a + or -
if ( endIndex == -1)
{
endIndex = line.indexOf(' ', indX + 3); // find the next space
}
}
eIndex = endIndex;
while ( endIndex > indX)
{
if (Character.isDigit( line.charAt(endIndex)))
{
//found digit
int digit = Character.getNumericValue( line.charAt(endIndex) );
exp += digit * Math.pow(10, counter);
counter++;
} else if ( line.charAt(endIndex) == '^')
{
break;
}
endIndex--;
}
exp *= negative;
//Remove that part of the string
line = line.substring(0, begIndex) + line.substring(eIndex);
}
}
//Do the integral of the expression
if ( coef != 0)
{
if ( exp != -1 || coef != 0)
{
exp = exp + 1;
//do the coef calculation inside term
}
//Check for expressions with duplicate exponents that will need to add
//Call the search function in binary tree and see if there is a duplicate exponent
Term t = new Term(coef, exp, a, b);
definiteValue += t.getdefInt(); // calculate the definite integral
//Create a new node object
Node<Term> n = new Node<Term>(t); // to be used with the search function
if ( bt.Search(n, bt.getRoot() ) != null)
{
//depulicate value -- Update the node
( (bt.Search(n, bt.getRoot() )).getData() ).addCoefficient(t.getNumer() ); // update the coefficient. //exponent stay the same
} else
{
//Create a new node and save it into the binary tree
bt.insert(t, bt.getRoot(), bt.getRoot() );
}
}
}
//print
bt.print( bt.getRoot() );
if (definiteIntegral)
{
//Do calculation
System.out.print(", " + a + "|" + b + " = " + String.format("%.3f", definiteValue) ) ;
} else
{
System.out.print(" + C");
}
System.out.println(); // format
}
scnr.close(); //closes the scanner
}
public static int findSignIndex(String s, int ind)
{
//Loop through the string to find the first index of '-' or '+'
for (int i = ind; i < s.length(); i++)
{
if ( s.charAt(i) == '-' || s.charAt(i) == '+')
{
return i;
}
}
return -1; // null
}
}