Added division grouping
This commit is contained in:
parent
c914fac1f5
commit
4bb28c3f39
@ -32,86 +32,91 @@ public class TestGPU {
|
||||
Keyboard.setAdditionalKeyboardListener(new KeyboardEventListener() {
|
||||
@Override
|
||||
public boolean keyPressed(Key k) {
|
||||
switch (k) {
|
||||
case LEFT:
|
||||
c.moveLeft();
|
||||
return true;
|
||||
case RIGHT:
|
||||
c.moveRight();
|
||||
return true;
|
||||
case NUM0:
|
||||
c.typeChar('0');
|
||||
return true;
|
||||
case NUM1:
|
||||
c.typeChar('1');
|
||||
return true;
|
||||
case NUM2:
|
||||
c.typeChar('2');
|
||||
return true;
|
||||
case NUM3:
|
||||
c.typeChar('3');
|
||||
return true;
|
||||
case NUM4:
|
||||
c.typeChar('4');
|
||||
return true;
|
||||
case NUM5:
|
||||
c.typeChar('5');
|
||||
return true;
|
||||
case NUM6:
|
||||
c.typeChar('6');
|
||||
return true;
|
||||
case NUM7:
|
||||
c.typeChar('7');
|
||||
return true;
|
||||
case NUM8:
|
||||
c.typeChar('8');
|
||||
return true;
|
||||
case NUM9:
|
||||
c.typeChar('9');
|
||||
return true;
|
||||
case PLUS:
|
||||
c.typeChar(MathematicalSymbols.SUM);
|
||||
return true;
|
||||
case MINUS:
|
||||
c.typeChar(MathematicalSymbols.SUBTRACTION);
|
||||
return true;
|
||||
case MULTIPLY:
|
||||
c.typeChar(MathematicalSymbols.MULTIPLICATION);
|
||||
return true;
|
||||
case DIVIDE:
|
||||
c.typeChar(MathematicalSymbols.DIVISION);
|
||||
return true;
|
||||
case SQRT:
|
||||
c.typeChar(MathematicalSymbols.SQUARE_ROOT);
|
||||
return true;
|
||||
case PARENTHESIS_OPEN:
|
||||
case SINE:
|
||||
c.typeChar(MathematicalSymbols.PARENTHESIS_OPEN);
|
||||
return true;
|
||||
case PARENTHESIS_CLOSE:
|
||||
case debug_DEG:
|
||||
c.moveRight();
|
||||
return true;
|
||||
case DELETE:
|
||||
c.del();
|
||||
return true;
|
||||
case RESET:
|
||||
c.clear();
|
||||
return true;
|
||||
case POWER:
|
||||
d.destroy();
|
||||
System.exit(0);
|
||||
return true;
|
||||
case EQUAL:
|
||||
Expression expr;
|
||||
try {
|
||||
expr = MathParser.parseInput(new MathContext(), c);
|
||||
System.out.println("Parsed input:" + expr.toString());
|
||||
} catch (final Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
default:
|
||||
break;
|
||||
try {
|
||||
switch (k) {
|
||||
case LEFT:
|
||||
c.moveLeft();
|
||||
return true;
|
||||
case RIGHT:
|
||||
c.moveRight();
|
||||
return true;
|
||||
case NUM0:
|
||||
c.typeChar('0');
|
||||
return true;
|
||||
case NUM1:
|
||||
c.typeChar('1');
|
||||
return true;
|
||||
case NUM2:
|
||||
c.typeChar('2');
|
||||
return true;
|
||||
case NUM3:
|
||||
c.typeChar('3');
|
||||
return true;
|
||||
case NUM4:
|
||||
c.typeChar('4');
|
||||
return true;
|
||||
case NUM5:
|
||||
c.typeChar('5');
|
||||
return true;
|
||||
case NUM6:
|
||||
c.typeChar('6');
|
||||
return true;
|
||||
case NUM7:
|
||||
c.typeChar('7');
|
||||
return true;
|
||||
case NUM8:
|
||||
c.typeChar('8');
|
||||
return true;
|
||||
case NUM9:
|
||||
c.typeChar('9');
|
||||
return true;
|
||||
case PLUS:
|
||||
c.typeChar(MathematicalSymbols.SUM);
|
||||
return true;
|
||||
case MINUS:
|
||||
c.typeChar(MathematicalSymbols.SUBTRACTION);
|
||||
return true;
|
||||
case MULTIPLY:
|
||||
c.typeChar(MathematicalSymbols.MULTIPLICATION);
|
||||
return true;
|
||||
case DIVIDE:
|
||||
c.typeChar(MathematicalSymbols.DIVISION);
|
||||
return true;
|
||||
case SQRT:
|
||||
c.typeChar(MathematicalSymbols.SQUARE_ROOT);
|
||||
return true;
|
||||
case PARENTHESIS_OPEN:
|
||||
case SINE:
|
||||
c.typeChar(MathematicalSymbols.PARENTHESIS_OPEN);
|
||||
return true;
|
||||
case PARENTHESIS_CLOSE:
|
||||
case debug_DEG:
|
||||
c.moveRight();
|
||||
return true;
|
||||
case DELETE:
|
||||
c.del();
|
||||
return true;
|
||||
case RESET:
|
||||
c.clear();
|
||||
return true;
|
||||
case POWER:
|
||||
d.destroy();
|
||||
System.exit(0);
|
||||
return true;
|
||||
case EQUAL:
|
||||
Expression expr;
|
||||
try {
|
||||
expr = MathParser.parseInput(new MathContext(), c);
|
||||
System.out.println("Parsed input:" + expr.toString());
|
||||
} catch (final Error e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return false;
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return false;
|
||||
|
||||
|
@ -32,7 +32,7 @@ public abstract class Block implements GraphicalElement {
|
||||
|
||||
public abstract boolean delBlock(Caret caret);
|
||||
|
||||
public abstract Block getBlock(Caret caret);
|
||||
public abstract BlockReference<?> getBlock(Caret caret);
|
||||
|
||||
@Override
|
||||
public abstract void recomputeDimensions();
|
||||
|
@ -35,7 +35,7 @@ public class BlockChar extends Block {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block getBlock(Caret caret) {
|
||||
public BlockReference<?> getBlock(Caret caret) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -69,6 +69,11 @@ public class BlockContainer implements GraphicalElement {
|
||||
}
|
||||
|
||||
public void addBlock(int position, Block b) {
|
||||
addBlockUnsafe(position, b);
|
||||
recomputeDimensions();
|
||||
}
|
||||
|
||||
public void addBlockUnsafe(int position, Block b) {
|
||||
if (b.isSmall() != small) {
|
||||
b.setSmall(small);
|
||||
}
|
||||
@ -77,7 +82,6 @@ public class BlockContainer implements GraphicalElement {
|
||||
} else {
|
||||
content.add(position, b);
|
||||
}
|
||||
recomputeDimensions();
|
||||
}
|
||||
|
||||
public void appendBlock(Block b) {
|
||||
@ -93,17 +97,26 @@ public class BlockContainer implements GraphicalElement {
|
||||
}
|
||||
|
||||
public void removeBlock(Block b) {
|
||||
content.remove(b);
|
||||
removeBlockUnsafe(b);
|
||||
recomputeDimensions();
|
||||
}
|
||||
|
||||
public void removeBlockUnsafe(Block b) {
|
||||
content.remove(b);
|
||||
}
|
||||
|
||||
public void removeAt(int i) {
|
||||
content.remove(i);
|
||||
recomputeDimensions();
|
||||
}
|
||||
|
||||
public Block getBlockAt(int i) {
|
||||
return content.get(i);
|
||||
public BlockReference<?> getBlockAt(int i) {
|
||||
Block b = content.get(i);
|
||||
return new BlockReference<>(b, i, this);
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return content.size();
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
@ -202,8 +215,8 @@ public class BlockContainer implements GraphicalElement {
|
||||
return removed;
|
||||
}
|
||||
|
||||
public Block getBlock(Caret caret) {
|
||||
Block block = null;
|
||||
public BlockReference<?> getBlock(Caret caret) {
|
||||
BlockReference<?> block = null;
|
||||
|
||||
int pos = 0;
|
||||
for (final Block b : content) {
|
||||
|
@ -57,8 +57,8 @@ public class BlockDivision extends Block {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block getBlock(Caret caret) {
|
||||
Block bl = null;
|
||||
public BlockReference<?> getBlock(Caret caret) {
|
||||
BlockReference<?> bl = null;
|
||||
bl = containerUp.getBlock(caret);
|
||||
if (bl != null) {
|
||||
return bl;
|
||||
|
@ -0,0 +1,17 @@
|
||||
package org.warp.picalculator.gui.expression.blocks;
|
||||
|
||||
import org.warp.picalculator.gui.expression.Caret;
|
||||
import org.warp.picalculator.gui.graphicengine.GraphicEngine;
|
||||
import org.warp.picalculator.gui.graphicengine.Renderer;
|
||||
import org.warp.picalculator.math.MathContext;
|
||||
import org.warp.picalculator.math.MathematicalSymbols;
|
||||
import org.warp.picalculator.math.parser.features.FeatureChar;
|
||||
import org.warp.picalculator.math.parser.features.interfaces.Feature;
|
||||
|
||||
public class BlockNumber extends BlockChar {
|
||||
|
||||
public BlockNumber(char ch) {
|
||||
super(ch);
|
||||
}
|
||||
|
||||
}
|
@ -79,7 +79,7 @@ public abstract class BlockParenthesisAbstract extends Block {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block getBlock(Caret caret) {
|
||||
public BlockReference<?> getBlock(Caret caret) {
|
||||
return containerNumber.getBlock(caret);
|
||||
}
|
||||
|
||||
|
@ -46,7 +46,7 @@ public class BlockPower extends Block {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block getBlock(Caret caret) {
|
||||
public BlockReference<?> getBlock(Caret caret) {
|
||||
return containerExponent.getBlock(caret);
|
||||
}
|
||||
|
||||
|
@ -0,0 +1,54 @@
|
||||
package org.warp.picalculator.gui.expression.blocks;
|
||||
|
||||
public class BlockReference<T extends Block> {
|
||||
private final T block;
|
||||
private final BlockContainer container;
|
||||
private final int blockPosition;
|
||||
|
||||
public BlockReference(T block, int blockPosition, BlockContainer container) {
|
||||
this.block = block;
|
||||
this.blockPosition = blockPosition;
|
||||
this.container = container;
|
||||
}
|
||||
|
||||
public T get() {
|
||||
return block;
|
||||
}
|
||||
|
||||
public BlockContainer getContainer() {
|
||||
return container;
|
||||
}
|
||||
|
||||
public int getIndex() {
|
||||
return blockPosition;
|
||||
}
|
||||
|
||||
public BlockReference<?> getNextBlock() {
|
||||
return getBlockAtSafe(this.blockPosition + 1);
|
||||
}
|
||||
|
||||
public boolean hasNextBlock() {
|
||||
return isInsideBounds(this.blockPosition + 1);
|
||||
}
|
||||
|
||||
public BlockReference<?> getPreviousBlock() {
|
||||
return getBlockAtSafe(this.blockPosition - 1);
|
||||
}
|
||||
|
||||
public boolean hasPreviousBlock() {
|
||||
return isInsideBounds(this.blockPosition - 1);
|
||||
}
|
||||
|
||||
private BlockReference<?> getBlockAtSafe(int i) {
|
||||
if (isInsideBounds(i)) {
|
||||
return container.getBlockAt(i);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private boolean isInsideBounds(int i) {
|
||||
return i < container.getSize() && i >= 0;
|
||||
}
|
||||
|
||||
|
||||
}
|
@ -56,7 +56,7 @@ public class BlockSquareRoot extends Block {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block getBlock(Caret caret) {
|
||||
public BlockReference<?> getBlock(Caret caret) {
|
||||
return containerNumber.getBlock(caret);
|
||||
}
|
||||
|
||||
|
@ -32,7 +32,7 @@ public class BlockUndefined extends Block {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block getBlock(Caret caret) {
|
||||
public BlockReference<?> getBlock(Caret caret) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -100,7 +100,7 @@ public class BlockVariable extends Block {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Block getBlock(Caret caret) {
|
||||
public BlockReference<?> getBlock(Caret caret) {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
@ -11,6 +11,7 @@ import org.warp.picalculator.gui.expression.ExtraMenu;
|
||||
import org.warp.picalculator.gui.expression.InputContext;
|
||||
import org.warp.picalculator.gui.expression.blocks.Block;
|
||||
import org.warp.picalculator.gui.expression.blocks.BlockContainer;
|
||||
import org.warp.picalculator.gui.expression.blocks.BlockReference;
|
||||
import org.warp.picalculator.gui.expression.layouts.InputLayout;
|
||||
import org.warp.picalculator.gui.graphicengine.GraphicEngine;
|
||||
import org.warp.picalculator.gui.graphicengine.Renderer;
|
||||
@ -58,6 +59,10 @@ public abstract class InputContainer implements GraphicalElement, InputLayout, S
|
||||
|
||||
public void typeChar(char c) {
|
||||
final Block b = parseChar(c);
|
||||
typeBlock(b);
|
||||
}
|
||||
|
||||
public void typeBlock(Block b) {
|
||||
if (b != null) {
|
||||
caret.resetRemaining();
|
||||
if (root.putBlock(caret, b)) {
|
||||
@ -89,9 +94,9 @@ public abstract class InputContainer implements GraphicalElement, InputLayout, S
|
||||
closeExtra();
|
||||
}
|
||||
|
||||
public Block getSelectedBlock() {
|
||||
public BlockReference<?> getSelectedBlock() {
|
||||
caret.resetRemaining();
|
||||
Block selectedBlock = root.getBlock(caret);
|
||||
BlockReference<?> selectedBlock = root.getBlock(caret);
|
||||
return selectedBlock;
|
||||
}
|
||||
|
||||
@ -225,9 +230,9 @@ public abstract class InputContainer implements GraphicalElement, InputLayout, S
|
||||
|
||||
public void toggleExtra() {
|
||||
if (extra == null) {
|
||||
Block selectedBlock = getSelectedBlock();
|
||||
BlockReference<?> selectedBlock = getSelectedBlock();
|
||||
if (selectedBlock != null) {
|
||||
extra = selectedBlock.getExtraMenu();
|
||||
extra = selectedBlock.get().getExtraMenu();
|
||||
extra.open();
|
||||
}
|
||||
} else {
|
||||
|
@ -1,11 +1,17 @@
|
||||
package org.warp.picalculator.gui.expression.containers;
|
||||
|
||||
import org.warp.picalculator.Utils;
|
||||
import org.warp.picalculator.gui.expression.Caret;
|
||||
import org.warp.picalculator.gui.expression.CaretState;
|
||||
import org.warp.picalculator.gui.expression.InputContext;
|
||||
import org.warp.picalculator.gui.expression.blocks.Block;
|
||||
import org.warp.picalculator.gui.expression.blocks.BlockChar;
|
||||
import org.warp.picalculator.gui.expression.blocks.BlockContainer;
|
||||
import org.warp.picalculator.gui.expression.blocks.BlockDivision;
|
||||
import org.warp.picalculator.gui.expression.blocks.BlockNumber;
|
||||
import org.warp.picalculator.gui.expression.blocks.BlockParenthesis;
|
||||
import org.warp.picalculator.gui.expression.blocks.BlockPower;
|
||||
import org.warp.picalculator.gui.expression.blocks.BlockReference;
|
||||
import org.warp.picalculator.gui.expression.blocks.BlockSine;
|
||||
import org.warp.picalculator.gui.expression.blocks.BlockSquareRoot;
|
||||
import org.warp.picalculator.gui.expression.blocks.BlockVariable;
|
||||
@ -53,6 +59,7 @@ public class NormalInputContainer extends InputContainer {
|
||||
case MathematicalSymbols.SUM:
|
||||
case MathematicalSymbols.SUM_SUBTRACTION:
|
||||
case MathematicalSymbols.SUBTRACTION:
|
||||
return new BlockChar(c);
|
||||
case '0':
|
||||
case '1':
|
||||
case '2':
|
||||
@ -63,7 +70,7 @@ public class NormalInputContainer extends InputContainer {
|
||||
case '7':
|
||||
case '8':
|
||||
case '9':
|
||||
return new BlockChar(c);
|
||||
return new BlockNumber(c);
|
||||
case MathematicalSymbols.SINE:
|
||||
return new BlockSine();
|
||||
case MathematicalSymbols.PI:
|
||||
@ -81,8 +88,45 @@ public class NormalInputContainer extends InputContainer {
|
||||
@Override
|
||||
public void typeChar(char c) {
|
||||
super.typeChar(c);
|
||||
if (c == MathematicalSymbols.PARENTHESIS_CLOSE) {
|
||||
this.moveRight();
|
||||
switch (c) {
|
||||
case MathematicalSymbols.PARENTHESIS_CLOSE:
|
||||
this.moveRight();
|
||||
case MathematicalSymbols.DIVISION:
|
||||
@SuppressWarnings("unchecked")
|
||||
BlockReference<BlockDivision> ref = (BlockReference<BlockDivision>) this.getSelectedBlock();
|
||||
BlockContainer parentContainer = ref.getContainer();
|
||||
BlockReference<?> currentBlock = ref;
|
||||
boolean groupedBefore = false;
|
||||
int before = 0;
|
||||
while (true) {
|
||||
currentBlock = currentBlock.getPreviousBlock();
|
||||
if (currentBlock == null) break;
|
||||
Block b = currentBlock.get();
|
||||
if (b instanceof BlockNumber || b instanceof BlockVariable) {
|
||||
if (!groupedBefore) {
|
||||
groupedBefore = true;
|
||||
}
|
||||
before++;
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (groupedBefore) {
|
||||
this.moveLeft();
|
||||
for (int i = 0; i < before; i++) {
|
||||
BlockReference<?> b = this.getSelectedBlock();
|
||||
this.del();
|
||||
this.moveRight();
|
||||
this.typeBlock(b.get());
|
||||
this.moveLeft();
|
||||
this.moveLeft();
|
||||
}
|
||||
for (int i = 0; i < before + 1; i++) {
|
||||
this.moveRight();
|
||||
}
|
||||
this.moveRight();// Move to the divisor
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -170,283 +170,288 @@ public class MathInputScreen extends Screen {
|
||||
@Override
|
||||
public boolean keyPressed(Key k) {
|
||||
Utils.out.println(1, k.toString());
|
||||
switch (k) {
|
||||
case OK:
|
||||
userInput.toggleExtra();
|
||||
mustRefresh = true;
|
||||
return true;
|
||||
case HISTORY_BACK:
|
||||
if (userInput.isExtraOpened()) {
|
||||
userInput.closeExtra();
|
||||
currentStep = 0;
|
||||
try {
|
||||
switch (k) {
|
||||
case OK:
|
||||
userInput.toggleExtra();
|
||||
mustRefresh = true;
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
if (userInput.isExtraOpened() && userInput.getExtraKeyboardEventListener().keyPressed(k)) {
|
||||
currentStep = 0;
|
||||
return true;
|
||||
} else {
|
||||
final boolean step = k == Key.STEP;
|
||||
switch (k) {
|
||||
case HISTORY_BACK:
|
||||
if (userInput.isExtraOpened()) {
|
||||
userInput.closeExtra();
|
||||
currentStep = 0;
|
||||
mustRefresh = true;
|
||||
return true;
|
||||
}
|
||||
default:
|
||||
if (userInput.isExtraOpened() && userInput.getExtraKeyboardEventListener().keyPressed(k)) {
|
||||
currentStep = 0;
|
||||
return true;
|
||||
} else {
|
||||
final boolean step = k == Key.STEP;
|
||||
switch (k) {
|
||||
|
||||
case STEP:
|
||||
currentStep++;
|
||||
case SIMPLIFY:
|
||||
if (!step) currentStep = 0;
|
||||
if (DisplayManager.INSTANCE.error != null) {
|
||||
//TODO: make the error management a global API rather than being relegated to this screen.
|
||||
Utils.out.println(1, "Resetting after error...");
|
||||
DisplayManager.INSTANCE.error = null;
|
||||
calc.f = null;
|
||||
calc.f2 = null;
|
||||
calc.resultsCount = 0;
|
||||
return true;
|
||||
} else {
|
||||
if (!computingResult) {
|
||||
computingResult = true;
|
||||
computingThread = new Thread(()-> {
|
||||
try {
|
||||
try {
|
||||
if (!userInput.isAlreadyParsed() && !userInput.isEmpty()) {
|
||||
Expression expr = MathParser.parseInput(calc, userInput);
|
||||
if (calc.f == null | calc.f2 == null) {
|
||||
calc.f = new ObjectArrayList<>();
|
||||
calc.f2 = new ObjectArrayList<>();
|
||||
} else {
|
||||
calc.f.clear();
|
||||
calc.f2.clear();
|
||||
}
|
||||
calc.f.add(expr);
|
||||
Utils.out.println(2, "INPUT: " + expr);
|
||||
MathSolver ms = new MathSolver(expr);
|
||||
ObjectArrayList<ObjectArrayList<Function>> resultSteps = ms.solveAllSteps();
|
||||
resultSteps.add(0, Utils.newArrayList(expr));
|
||||
ObjectArrayList<Function> resultExpressions = resultSteps.get(resultSteps.size() - 1);
|
||||
for (Function rr : resultExpressions) {
|
||||
Utils.out.println(1, "RESULT: " + rr.toString());
|
||||
}
|
||||
ObjectArrayList<ObjectArrayList<Block>> resultBlocks = MathParser.parseOutput(calc, resultExpressions);
|
||||
result.setContentAsMultipleGroups(resultBlocks);
|
||||
// showVariablesDialog(() -> {
|
||||
// currentExpression = newExpression;
|
||||
// simplify();
|
||||
// });
|
||||
}
|
||||
} catch (final InterruptedException ex) {
|
||||
Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_MIN, "Computing thread stopped.");
|
||||
} catch (final Exception ex) {
|
||||
if (StaticVars.debugOn) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
throw new Error(Errors.SYNTAX_ERROR);
|
||||
}
|
||||
} catch (final Error e) {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final PrintWriter pw = new PrintWriter(sw);
|
||||
e.printStackTrace(pw);
|
||||
d.errorStackTrace = sw.toString().toUpperCase().replace("\t", " ").replace("\r", "").split("\n");
|
||||
DisplayManager.INSTANCE.error = e.id.toString();
|
||||
System.err.println(e.id);
|
||||
}
|
||||
computingResult = false;
|
||||
});
|
||||
computingThread.setName("Computing Thread");
|
||||
computingThread.setDaemon(true);
|
||||
computingThread.setPriority(Thread.NORM_PRIORITY + 3);
|
||||
computingThread.start();
|
||||
case STEP:
|
||||
currentStep++;
|
||||
case SIMPLIFY:
|
||||
if (!step) currentStep = 0;
|
||||
if (DisplayManager.INSTANCE.error != null) {
|
||||
//TODO: make the error management a global API rather than being relegated to this screen.
|
||||
Utils.out.println(1, "Resetting after error...");
|
||||
DisplayManager.INSTANCE.error = null;
|
||||
calc.f = null;
|
||||
calc.f2 = null;
|
||||
calc.resultsCount = 0;
|
||||
return true;
|
||||
} else {
|
||||
if (computingThread != null) {
|
||||
computingThread.interrupt();
|
||||
computingResult = false;
|
||||
if (!computingResult) {
|
||||
computingResult = true;
|
||||
computingThread = new Thread(()-> {
|
||||
try {
|
||||
try {
|
||||
if (!userInput.isAlreadyParsed() && !userInput.isEmpty()) {
|
||||
Expression expr = MathParser.parseInput(calc, userInput);
|
||||
if (calc.f == null | calc.f2 == null) {
|
||||
calc.f = new ObjectArrayList<>();
|
||||
calc.f2 = new ObjectArrayList<>();
|
||||
} else {
|
||||
calc.f.clear();
|
||||
calc.f2.clear();
|
||||
}
|
||||
calc.f.add(expr);
|
||||
Utils.out.println(2, "INPUT: " + expr);
|
||||
MathSolver ms = new MathSolver(expr);
|
||||
ObjectArrayList<ObjectArrayList<Function>> resultSteps = ms.solveAllSteps();
|
||||
resultSteps.add(0, Utils.newArrayList(expr));
|
||||
ObjectArrayList<Function> resultExpressions = resultSteps.get(resultSteps.size() - 1);
|
||||
for (Function rr : resultExpressions) {
|
||||
Utils.out.println(1, "RESULT: " + rr.toString());
|
||||
}
|
||||
ObjectArrayList<ObjectArrayList<Block>> resultBlocks = MathParser.parseOutput(calc, resultExpressions);
|
||||
result.setContentAsMultipleGroups(resultBlocks);
|
||||
// showVariablesDialog(() -> {
|
||||
// currentExpression = newExpression;
|
||||
// simplify();
|
||||
// });
|
||||
}
|
||||
} catch (final InterruptedException ex) {
|
||||
Utils.out.println(Utils.OUTPUTLEVEL_DEBUG_MIN, "Computing thread stopped.");
|
||||
} catch (final Exception ex) {
|
||||
if (StaticVars.debugOn) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
throw new Error(Errors.SYNTAX_ERROR);
|
||||
}
|
||||
} catch (final Error e) {
|
||||
final StringWriter sw = new StringWriter();
|
||||
final PrintWriter pw = new PrintWriter(sw);
|
||||
e.printStackTrace(pw);
|
||||
d.errorStackTrace = sw.toString().toUpperCase().replace("\t", " ").replace("\r", "").split("\n");
|
||||
DisplayManager.INSTANCE.error = e.id.toString();
|
||||
System.err.println(e.id);
|
||||
}
|
||||
computingResult = false;
|
||||
});
|
||||
computingThread.setName("Computing Thread");
|
||||
computingThread.setDaemon(true);
|
||||
computingThread.setPriority(Thread.NORM_PRIORITY + 3);
|
||||
computingThread.start();
|
||||
return true;
|
||||
} else {
|
||||
if (computingThread != null) {
|
||||
computingThread.interrupt();
|
||||
computingResult = false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
case NUM0:
|
||||
typeChar('0');
|
||||
return true;
|
||||
case NUM1:
|
||||
typeChar('1');
|
||||
return true;
|
||||
case NUM2:
|
||||
typeChar('2');
|
||||
return true;
|
||||
case NUM3:
|
||||
typeChar('3');
|
||||
return true;
|
||||
case NUM4:
|
||||
typeChar('4');
|
||||
return true;
|
||||
case NUM5:
|
||||
typeChar('5');
|
||||
return true;
|
||||
case NUM6:
|
||||
typeChar('6');
|
||||
return true;
|
||||
case NUM7:
|
||||
typeChar('7');
|
||||
return true;
|
||||
case NUM8:
|
||||
typeChar('8');
|
||||
return true;
|
||||
case NUM9:
|
||||
typeChar('9');
|
||||
return true;
|
||||
case PLUS:
|
||||
typeChar('+');
|
||||
return true;
|
||||
case MINUS:
|
||||
typeChar('-');
|
||||
return true;
|
||||
case PLUS_MINUS:
|
||||
typeChar('±');
|
||||
return true;
|
||||
case MULTIPLY:
|
||||
typeChar('*');
|
||||
return true;
|
||||
case DIVIDE:
|
||||
typeChar('/');
|
||||
return true;
|
||||
case PARENTHESIS_OPEN:
|
||||
typeChar('(');
|
||||
return true;
|
||||
case PARENTHESIS_CLOSE:
|
||||
typeChar(')');
|
||||
return true;
|
||||
case DOT:
|
||||
typeChar('.');
|
||||
return true;
|
||||
case EQUAL:
|
||||
typeChar('=');
|
||||
return true;
|
||||
case SQRT:
|
||||
typeChar('Ⓐ');
|
||||
return true;
|
||||
case ROOT:
|
||||
typeChar('√');
|
||||
return true;
|
||||
case POWER_OF_2:
|
||||
typeChar(MathematicalSymbols.POWER_OF_TWO);
|
||||
return true;
|
||||
case POWER_OF_x:
|
||||
typeChar(MathematicalSymbols.POWER);
|
||||
return true;
|
||||
case PI:
|
||||
typeChar(MathematicalSymbols.PI);
|
||||
return true;
|
||||
case LETTER_X:
|
||||
typeChar(MathematicalSymbols.variables[23]);
|
||||
return true;
|
||||
case LETTER_Y:
|
||||
typeChar(MathematicalSymbols.variables[24]);
|
||||
return true;
|
||||
case SINE:
|
||||
typeChar(MathematicalSymbols.SINE);
|
||||
return true;
|
||||
case COSINE:
|
||||
typeChar(MathematicalSymbols.COSINE);
|
||||
return true;
|
||||
case TANGENT:
|
||||
typeChar(MathematicalSymbols.TANGENT);
|
||||
return true;
|
||||
case ARCSINE:
|
||||
typeChar(MathematicalSymbols.ARC_SINE);
|
||||
return true;
|
||||
case ARCCOSINE:
|
||||
typeChar(MathematicalSymbols.ARC_COSINE);
|
||||
return true;
|
||||
case ARCTANGENT:
|
||||
typeChar(MathematicalSymbols.ARC_TANGENT);
|
||||
return true;
|
||||
case DELETE:
|
||||
userInput.del();
|
||||
currentStep = 0;
|
||||
mustRefresh = true;
|
||||
return true;
|
||||
case LEFT:
|
||||
userInput.moveLeft();
|
||||
mustRefresh = true;
|
||||
return true;
|
||||
case RIGHT:
|
||||
userInput.moveRight();
|
||||
mustRefresh = true;
|
||||
return true;
|
||||
case RESET:
|
||||
userInput.clear();
|
||||
result.clear();
|
||||
currentStep = 0;
|
||||
if (DisplayManager.INSTANCE.error != null) {
|
||||
Utils.out.println(1, "Resetting after error...");
|
||||
DisplayManager.INSTANCE.error = null;
|
||||
}
|
||||
return true;
|
||||
case SURD_MODE:
|
||||
calc.exactMode = !calc.exactMode;
|
||||
result.clear();
|
||||
currentStep = 0;
|
||||
Keyboard.keyPressed(Key.SIMPLIFY);
|
||||
return true;
|
||||
case debug1:
|
||||
DisplayManager.INSTANCE.setScreen(new EmptyScreen());
|
||||
return true;
|
||||
case HISTORY_BACK:
|
||||
// if (DisplayManager.INSTANCE.canGoBack()) {
|
||||
// if (currentExpression != null && currentExpression.length() > 0 & DisplayManager.INSTANCE.sessions[DisplayManager.INSTANCE.currentSession + 1] instanceof MathInputScreen) {
|
||||
// newExpression = currentExpression;
|
||||
// try {
|
||||
// interpreta(true);
|
||||
// } catch (final Error e) {}
|
||||
// }
|
||||
// }
|
||||
return false;
|
||||
case HISTORY_FORWARD:
|
||||
// if (DisplayManager.INSTANCE.canGoForward()) {
|
||||
// if (currentExpression != null && currentExpression.length() > 0 & DisplayManager.INSTANCE.sessions[DisplayManager.INSTANCE.currentSession - 1] instanceof MathInputScreen) {
|
||||
// newExpression = currentExpression;
|
||||
// try {
|
||||
// interpreta(true);
|
||||
// } catch (final Error e) {}
|
||||
// }
|
||||
// }
|
||||
return false;
|
||||
case debug_DEG:
|
||||
if (calc.angleMode.equals(AngleMode.DEG) == false) {
|
||||
calc.angleMode = AngleMode.DEG;
|
||||
case NUM0:
|
||||
typeChar('0');
|
||||
return true;
|
||||
case NUM1:
|
||||
typeChar('1');
|
||||
return true;
|
||||
case NUM2:
|
||||
typeChar('2');
|
||||
return true;
|
||||
case NUM3:
|
||||
typeChar('3');
|
||||
return true;
|
||||
case NUM4:
|
||||
typeChar('4');
|
||||
return true;
|
||||
case NUM5:
|
||||
typeChar('5');
|
||||
return true;
|
||||
case NUM6:
|
||||
typeChar('6');
|
||||
return true;
|
||||
case NUM7:
|
||||
typeChar('7');
|
||||
return true;
|
||||
case NUM8:
|
||||
typeChar('8');
|
||||
return true;
|
||||
case NUM9:
|
||||
typeChar('9');
|
||||
return true;
|
||||
case PLUS:
|
||||
typeChar('+');
|
||||
return true;
|
||||
case MINUS:
|
||||
typeChar('-');
|
||||
return true;
|
||||
case PLUS_MINUS:
|
||||
typeChar('±');
|
||||
return true;
|
||||
case MULTIPLY:
|
||||
typeChar('*');
|
||||
return true;
|
||||
case DIVIDE:
|
||||
typeChar('/');
|
||||
return true;
|
||||
case PARENTHESIS_OPEN:
|
||||
typeChar('(');
|
||||
return true;
|
||||
case PARENTHESIS_CLOSE:
|
||||
typeChar(')');
|
||||
return true;
|
||||
case DOT:
|
||||
typeChar('.');
|
||||
return true;
|
||||
case EQUAL:
|
||||
typeChar('=');
|
||||
return true;
|
||||
case SQRT:
|
||||
typeChar('Ⓐ');
|
||||
return true;
|
||||
case ROOT:
|
||||
typeChar('√');
|
||||
return true;
|
||||
case POWER_OF_2:
|
||||
typeChar(MathematicalSymbols.POWER_OF_TWO);
|
||||
return true;
|
||||
case POWER_OF_x:
|
||||
typeChar(MathematicalSymbols.POWER);
|
||||
return true;
|
||||
case PI:
|
||||
typeChar(MathematicalSymbols.PI);
|
||||
return true;
|
||||
case LETTER_X:
|
||||
typeChar(MathematicalSymbols.variables[23]);
|
||||
return true;
|
||||
case LETTER_Y:
|
||||
typeChar(MathematicalSymbols.variables[24]);
|
||||
return true;
|
||||
case SINE:
|
||||
typeChar(MathematicalSymbols.SINE);
|
||||
return true;
|
||||
case COSINE:
|
||||
typeChar(MathematicalSymbols.COSINE);
|
||||
return true;
|
||||
case TANGENT:
|
||||
typeChar(MathematicalSymbols.TANGENT);
|
||||
return true;
|
||||
case ARCSINE:
|
||||
typeChar(MathematicalSymbols.ARC_SINE);
|
||||
return true;
|
||||
case ARCCOSINE:
|
||||
typeChar(MathematicalSymbols.ARC_COSINE);
|
||||
return true;
|
||||
case ARCTANGENT:
|
||||
typeChar(MathematicalSymbols.ARC_TANGENT);
|
||||
return true;
|
||||
case DELETE:
|
||||
userInput.del();
|
||||
currentStep = 0;
|
||||
mustRefresh = true;
|
||||
return true;
|
||||
case LEFT:
|
||||
userInput.moveLeft();
|
||||
mustRefresh = true;
|
||||
return true;
|
||||
case RIGHT:
|
||||
userInput.moveRight();
|
||||
mustRefresh = true;
|
||||
return true;
|
||||
case RESET:
|
||||
userInput.clear();
|
||||
result.clear();
|
||||
currentStep = 0;
|
||||
if (DisplayManager.INSTANCE.error != null) {
|
||||
Utils.out.println(1, "Resetting after error...");
|
||||
DisplayManager.INSTANCE.error = null;
|
||||
}
|
||||
return true;
|
||||
case SURD_MODE:
|
||||
calc.exactMode = !calc.exactMode;
|
||||
result.clear();
|
||||
currentStep = 0;
|
||||
Keyboard.keyPressed(Key.SIMPLIFY);
|
||||
return true;
|
||||
case debug1:
|
||||
DisplayManager.INSTANCE.setScreen(new EmptyScreen());
|
||||
return true;
|
||||
case HISTORY_BACK:
|
||||
// if (DisplayManager.INSTANCE.canGoBack()) {
|
||||
// if (currentExpression != null && currentExpression.length() > 0 & DisplayManager.INSTANCE.sessions[DisplayManager.INSTANCE.currentSession + 1] instanceof MathInputScreen) {
|
||||
// newExpression = currentExpression;
|
||||
// try {
|
||||
// interpreta(true);
|
||||
// } catch (final Error e) {}
|
||||
// }
|
||||
// }
|
||||
return false;
|
||||
case HISTORY_FORWARD:
|
||||
// if (DisplayManager.INSTANCE.canGoForward()) {
|
||||
// if (currentExpression != null && currentExpression.length() > 0 & DisplayManager.INSTANCE.sessions[DisplayManager.INSTANCE.currentSession - 1] instanceof MathInputScreen) {
|
||||
// newExpression = currentExpression;
|
||||
// try {
|
||||
// interpreta(true);
|
||||
// } catch (final Error e) {}
|
||||
// }
|
||||
// }
|
||||
return false;
|
||||
case debug_DEG:
|
||||
if (calc.angleMode.equals(AngleMode.DEG) == false) {
|
||||
calc.angleMode = AngleMode.DEG;
|
||||
currentStep = 0;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
case debug_RAD:
|
||||
if (calc.angleMode.equals(AngleMode.RAD) == false) {
|
||||
calc.angleMode = AngleMode.RAD;
|
||||
currentStep = 0;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
case debug_GRA:
|
||||
if (calc.angleMode.equals(AngleMode.GRA) == false) {
|
||||
calc.angleMode = AngleMode.GRA;
|
||||
currentStep = 0;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
case DRG_CYCLE:
|
||||
if (calc.angleMode.equals(AngleMode.DEG) == true) {
|
||||
calc.angleMode = AngleMode.RAD;
|
||||
} else if (calc.angleMode.equals(AngleMode.RAD) == true) {
|
||||
calc.angleMode = AngleMode.GRA;
|
||||
} else {
|
||||
calc.angleMode = AngleMode.DEG;
|
||||
}
|
||||
currentStep = 0;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
case debug_RAD:
|
||||
if (calc.angleMode.equals(AngleMode.RAD) == false) {
|
||||
calc.angleMode = AngleMode.RAD;
|
||||
currentStep = 0;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
case debug_GRA:
|
||||
if (calc.angleMode.equals(AngleMode.GRA) == false) {
|
||||
calc.angleMode = AngleMode.GRA;
|
||||
currentStep = 0;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
case DRG_CYCLE:
|
||||
if (calc.angleMode.equals(AngleMode.DEG) == true) {
|
||||
calc.angleMode = AngleMode.RAD;
|
||||
} else if (calc.angleMode.equals(AngleMode.RAD) == true) {
|
||||
calc.angleMode = AngleMode.GRA;
|
||||
} else {
|
||||
calc.angleMode = AngleMode.DEG;
|
||||
}
|
||||
currentStep = 0;
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user