Permalink
Browse files

catch illegal matrix operations

  • Loading branch information...
1 parent 3b59ac7 commit 8b9e4a11b59cc2b2db447cc2fbbe0c2c3be0721a @Xlythe committed Feb 4, 2013
Showing with 36 additions and 31 deletions.
  1. +36 −31 src/com/android2/calculator3/Logic.java
@@ -400,44 +400,49 @@ static boolean isOperator(char c) {
}
private String evaluateMatrices(AdvancedDisplay display) throws SyntaxException {
- SimpleMatrix matrix = null;
- boolean add = false;
- boolean multiply = false;
- for(int i = 0; i < display.getChildCount(); i++) {
- View child = display.getChildAt(i);
- if(child instanceof MatrixView) {
- if(!add && !multiply) {
- matrix = ((MatrixView) child).getSimpleMatrix();
+ try {
+ SimpleMatrix matrix = null;
+ boolean add = false;
+ boolean multiply = false;
+ for(int i = 0; i < display.getChildCount(); i++) {
+ View child = display.getChildAt(i);
+ if(child instanceof MatrixView) {
+ if(!add && !multiply) {
+ matrix = ((MatrixView) child).getSimpleMatrix();
+ }
+ else if(add) {
+ add = false;
+ if(matrix == null) throw new SyntaxException();
+ matrix = matrix.plus(((MatrixView) child).getSimpleMatrix());
+ }
+ else if(multiply) {
+ multiply = false;
+ if(matrix == null) throw new SyntaxException();
+ matrix = matrix.mult(((MatrixView) child).getSimpleMatrix());
+ }
}
- else if(add) {
- add = false;
+ else if(child instanceof MatrixTransposeView) {
if(matrix == null) throw new SyntaxException();
- matrix = matrix.plus(((MatrixView) child).getSimpleMatrix());
+ matrix = matrix.transpose();
}
- else if(multiply) {
- multiply = false;
+ else if(child instanceof MatrixInverseView) {
if(matrix == null) throw new SyntaxException();
- matrix = matrix.mult(((MatrixView) child).getSimpleMatrix());
+ matrix = matrix.invert();
+ }
+ else {
+ String text = child.toString();
+ if(text.length() > 1) throw new SyntaxException();
+ else if(text.length() == 0) continue;
+ if(text.startsWith(String.valueOf(MUL))) multiply = true;
+ else if(text.startsWith(String.valueOf(PLUS))) add = true;
+ else throw new SyntaxException();
}
}
- else if(child instanceof MatrixTransposeView) {
- if(matrix == null) throw new SyntaxException();
- matrix = matrix.transpose();
- }
- else if(child instanceof MatrixInverseView) {
- if(matrix == null) throw new SyntaxException();
- matrix = matrix.invert();
- }
- else {
- String text = child.toString();
- if(text.length() > 1) throw new SyntaxException();
- else if(text.length() == 0) continue;
- if(text.startsWith(String.valueOf(MUL))) multiply = true;
- else if(text.startsWith(String.valueOf(PLUS))) add = true;
- else throw new SyntaxException();
- }
+ return MatrixView.matrixToString(matrix);
+ }
+ catch(Exception e) {
+ throw new SyntaxException();
}
- return MatrixView.matrixToString(matrix);
}
private boolean graphChanged(Graph graph, String equation, double minX, double maxX, double minY, double maxY) {

0 comments on commit 8b9e4a1

Please sign in to comment.