Use different multiplication signs in JSCL
This commit is contained in:
parent
0e4960dc4e
commit
27ee71a179
@ -10,23 +10,19 @@ import javax.annotation.Nonnull;
|
||||
* Date: 10/27/11
|
||||
* Time: 2:45 PM
|
||||
*/
|
||||
class MultiplyOrDivideFactor implements Parser<Generic> {
|
||||
class DivideFactor implements Parser<Generic> {
|
||||
|
||||
public static final Parser<Generic> multiply = new MultiplyOrDivideFactor(true);
|
||||
public static final Parser<Generic> parser = new DivideFactor();
|
||||
|
||||
public static final Parser<Generic> divide = new MultiplyOrDivideFactor(false);
|
||||
|
||||
boolean multiplication;
|
||||
|
||||
private MultiplyOrDivideFactor(boolean multiplication) {
|
||||
this.multiplication = multiplication;
|
||||
private DivideFactor() {
|
||||
}
|
||||
|
||||
public Generic parse(@Nonnull Parameters p, Generic previousSumElement) throws ParseException {
|
||||
int pos0 = p.position.intValue();
|
||||
final int pos0 = p.position.intValue();
|
||||
|
||||
ParserUtils.skipWhitespaces(p);
|
||||
if (p.position.intValue() < p.expression.length() && p.expression.charAt(p.position.intValue()) == (multiplication ? '*' : '/')) {
|
||||
final int pos1 = p.position.intValue();
|
||||
if (pos1 < p.expression.length() && p.expression.charAt(pos1) == '/') {
|
||||
p.position.increment();
|
||||
} else {
|
||||
ParserUtils.throwParseException(p, pos0, Messages.msg_10, '*', '/');
|
34
jscl/src/main/java/jscl/text/MultiplyFactor.java
Normal file
34
jscl/src/main/java/jscl/text/MultiplyFactor.java
Normal file
@ -0,0 +1,34 @@
|
||||
package jscl.text;
|
||||
|
||||
import jscl.math.Generic;
|
||||
import jscl.text.msg.Messages;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
/**
|
||||
* User: serso
|
||||
* Date: 10/27/11
|
||||
* Time: 2:45 PM
|
||||
*/
|
||||
class MultiplyFactor implements Parser<Generic> {
|
||||
|
||||
public static final Parser<Generic> parser = new MultiplyFactor();
|
||||
|
||||
private static boolean isMultiplication(char c) {
|
||||
return c == '*' || c == '×' || c == '∙';
|
||||
}
|
||||
|
||||
public Generic parse(@Nonnull Parameters p, Generic previousSumElement) throws ParseException {
|
||||
final int pos0 = p.position.intValue();
|
||||
|
||||
ParserUtils.skipWhitespaces(p);
|
||||
final int pos1 = p.position.intValue();
|
||||
if (pos1 < p.expression.length() && isMultiplication(p.expression.charAt(pos1))) {
|
||||
p.position.increment();
|
||||
} else {
|
||||
ParserUtils.throwParseException(p, pos0, Messages.msg_10, '*', '/');
|
||||
}
|
||||
|
||||
return ParserUtils.parseWithRollback(Factor.parser, pos0, previousSumElement, p);
|
||||
}
|
||||
}
|
@ -27,12 +27,12 @@ class TermParser implements Parser<Generic> {
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
Generic b = MultiplyOrDivideFactor.multiply.parse(p, null);
|
||||
Generic b = MultiplyFactor.parser.parse(p, null);
|
||||
result = result.multiply(s);
|
||||
s = b;
|
||||
} catch (ParseException e) {
|
||||
try {
|
||||
Generic b = MultiplyOrDivideFactor.divide.parse(p, null);
|
||||
Generic b = DivideFactor.parser.parse(p, null);
|
||||
if (s.compareTo(JsclInteger.valueOf(1)) == 0)
|
||||
s = new Inverse(GenericVariable.content(b, true)).expressionValue();
|
||||
else
|
||||
|
Loading…
Reference in New Issue
Block a user