-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfraction.sql
331 lines (303 loc) · 9.98 KB
/
fraction.sql
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
create or replace and compile java source named "org/quinto/math/Fraction" as
package org.quinto.math;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Fraction implements Comparable
{
private static final Pattern DOUBLE_MINUS_PATTERN = Pattern.compile( "--" );
private static final Pattern PLUS_MINUS_PATTERN = Pattern.compile( "\\+-" );
private static final Pattern SPACE_PATTERN = Pattern.compile( "\\p{Space}" );
private static final Pattern FLOAT_NUMBER_PATTERN = Pattern.compile( "(\\d++)\\.(\\d++)" );
private long top;
private long bottom;
private Integer hash = null;
public Fraction()
{
this( 0 );
}
public Fraction( long top )
{
this.top = top;
bottom = 1;
}
public Fraction( long top, long bottom )
{
this.top = top;
this.bottom = bottom;
shorten();
}
public long getBottom()
{
return bottom;
}
public long getTop()
{
return top;
}
public double doubleValue()
{
return ( double )top / bottom;
}
public String toString()
{
return top + "/" + bottom;
}
public String toStringAfterDivision()
{
return top + "*" + bottom;
}
private void shorten()
{
if ( bottom == 0 ) top = top > 0 ? 1 : top < 0 ? -1 : 0;
else if ( top == 0 ) bottom = 1;
else
{
long gcd = gcd( top, bottom );
top /= gcd;
bottom /= gcd;
if ( bottom < 0 )
{
bottom = -bottom;
top = -top;
}
}
}
public Object clone()
{
return new Fraction( top, bottom );
}
public boolean equals( Object obj )
{
if ( obj == null ) return false;
if ( obj instanceof Fraction )
{
Fraction rd = ( Fraction )obj;
return top == rd.top && bottom == rd.bottom;
}
if ( obj instanceof Number )
{
Number n = ( Number )obj;
return n.doubleValue() == doubleValue();
}
return false;
}
public int compareTo( Object obj )
{
if ( obj == null ) return 1;
if ( obj instanceof Fraction )
{
Fraction rd = ( Fraction )obj;
return Double.compare( doubleValue(), rd.doubleValue() );
}
if ( obj instanceof Number )
{
Number n = ( Number )obj;
return Double.compare( doubleValue(), n.doubleValue() );
}
return 0;
}
public int hashCode()
{
if ( hash == null ) hash = new Integer( ( int )( top + bottom ) );
return hash.intValue();
}
public static Fraction multiply( Fraction a, Fraction b )
{
if ( a == null || b == null ) return null;
return new Fraction( a.top * b.top, a.bottom * b.bottom );
}
public static Fraction divide( Fraction a, Fraction b )
{
if ( a == null || b == null ) return null;
return new Fraction( a.top * b.bottom, a.bottom * b.top );
}
public static Fraction minus( Fraction a, Fraction b )
{
if ( a == null || b == null ) return null;
return new Fraction( a.top * b.bottom - a.bottom * b.top, a.bottom * b.bottom );
}
public static Fraction plus( Fraction a, Fraction b )
{
if ( a == null || b == null ) return null;
return new Fraction( a.top * b.bottom + a.bottom * b.top, a.bottom * b.bottom );
}
public static Fraction minus( Fraction a )
{
if ( a == null ) return null;
return new Fraction( -a.top, a.bottom );
}
public static Fraction reverse( Fraction a )
{
if ( a == null ) return null;
return new Fraction( a.bottom, a.top );
}
public static long gcd( long a, long b )
{
if ( a == 0 || b == 0 ) return 1;
if ( a < 0 ) a = -a;
if ( b < 0 ) b = -b;
while ( b > 0 )
{
long c = a % b;
a = b;
b = c;
}
return a;
}
public static long lcm( long a, long b )
{
return Math.abs( a * b ) / gcd( a, b );
}
private static String replace( String s, int start, int end, String t )
{
StringBuffer sb = new StringBuffer( s.substring( 0, start ) );
sb.append( t );
sb.append( s.substring( end + 1 ) );
return sb.toString();
}
public static Double calculate( String expression )
{
Fraction ret = eval( expression );
if ( ret == null ) return null;
return new Double( ret.doubleValue() );
}
public static Fraction eval( String expression )
{
if ( expression == null ) return null;
expression = SPACE_PATTERN.matcher( expression ).replaceAll( "" );
expression = expression.replace( ',', '.' );
Matcher floatMatcher = FLOAT_NUMBER_PATTERN.matcher( expression );
int start = 0;
while ( floatMatcher.find( start ) )
{
start = floatMatcher.end();
if ( floatMatcher.groupCount() >= 2 )
{
String integralString = floatMatcher.group( 1 );
long integral = Long.parseLong( integralString );
String fractionalString = floatMatcher.group( 2 );
long fractional = Long.parseLong( fractionalString );
int pow = fractionalString.length();
long fraction = 1;
for ( int i = 0; i < pow; i++ ) fraction *= 10;
Fraction rd = new Fraction( integral * fraction + fractional, fraction );
String representation = rd.toString();
StringBuffer sb = new StringBuffer( representation.length() + 2 );
sb.append( '(' );
sb.append( representation );
sb.append( ')' );
expression = floatMatcher.replaceFirst( sb.toString() );
}
floatMatcher.reset( expression );
}
int idx = expression.indexOf( '(' );
while ( idx >= 0 )
{
int idx3 = expression.indexOf( ')', idx + 1 );
if ( idx3 < 0 ) return null;
int idx2 = expression.indexOf( '(', idx + 1 );
if ( idx2 >= 0 && idx3 > idx2 )
{
idx = idx2;
continue;
}
Fraction rd = evalNoBrackets( expression.substring( idx + 1, idx3 ) );
boolean afterDivision = idx > 0 && expression.charAt( idx - 1 ) == '/';
expression = replace( expression, idx, idx3, afterDivision ? rd.toStringAfterDivision() : rd.toString() );
idx = expression.indexOf( '(' );
}
return evalNoBrackets( expression );
}
private static Fraction evalNoBrackets( String expression )
{
while ( expression.startsWith( "--" ) ) expression = expression.substring( 2 );
expression = DOUBLE_MINUS_PATTERN.matcher( expression ).replaceAll( "+" );
expression = PLUS_MINUS_PATTERN.matcher( expression ).replaceAll( "-" );
int length = expression.length();
StringBuffer sb = new StringBuffer();
ArrayList mds = new ArrayList();
ArrayList ops = new ArrayList();
boolean prevCharWasDigit = false;
for ( int i = 0; i < length; i++ )
{
char c = expression.charAt( i );
if ( c == '+' || ( prevCharWasDigit && c == '-' ) )
{
if ( sb.length() == 0 ) return null;
mds.add( sb.toString() );
sb = new StringBuffer();
ops.add( new Character( c ) );
}
else
{
sb.append( c );
prevCharWasDigit = Character.isDigit( c );
}
}
if ( sb.length() == 0 ) return null;
mds.add( sb.toString() );
Fraction ret = evalMultiplyAndDivide( ( String )mds.get( 0 ) );
for ( int i = 1; i < mds.size(); i++ )
{
Fraction rd = evalMultiplyAndDivide( ( String )mds.get( i ) );
ret = ( ( Character )ops.get( i - 1 ) ).charValue() == '-' ? Fraction.minus( ret, rd ) : Fraction.plus( ret, rd );
}
return ret;
}
private static Fraction evalMultiplyAndDivide( String expression )
{
int length = expression.length();
int idx = expression.indexOf( '*' );
int idx2 = expression.indexOf( '/' );
idx = Math.min( idx < 0 ? length : idx, idx2 < 0 ? length : idx2 );
String firstNumber = expression.substring( 0, idx );
long number;
try
{
number = Long.parseLong( firstNumber );
}
catch ( NumberFormatException e )
{
return null;
}
Fraction ret = new Fraction( number );
if ( idx == length ) return ret;
StringBuffer sb = new StringBuffer();
char op = expression.charAt( idx );
for ( int i = idx + 1; i < length; i++ )
{
char c = expression.charAt( i );
if ( c == '/' || c == '*' )
{
if ( sb.length() == 0 ) return null;
try
{
number = Long.parseLong( sb.toString() );
}
catch ( NumberFormatException e )
{
return null;
}
sb = new StringBuffer();
Fraction rd = new Fraction( number );
ret = op == '/' ? Fraction.divide( ret, rd ) : Fraction.multiply( ret, rd );
op = c;
}
else sb.append( c );
}
if ( sb.length() == 0 ) return null;
try
{
number = Long.parseLong( sb.toString() );
}
catch ( NumberFormatException e )
{
return null;
}
Fraction rd = new Fraction( number );
ret = op == '/' ? Fraction.divide( ret, rd ) : Fraction.multiply( ret, rd );
return ret;
}
}
/