-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
196a41e
commit 153122f
Showing
9 changed files
with
561 additions
and
8 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
engine/src/main/java/mscalc/engine/commands/CBinaryCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package mscalc.engine.commands; | ||
|
||
public class CBinaryCommand implements IBinaryCommand { | ||
private int command; | ||
|
||
public CBinaryCommand(int command) { | ||
this.command = command; | ||
} | ||
|
||
@Override | ||
public void setCommand(int command) { | ||
this.command = command; | ||
} | ||
|
||
@Override | ||
public int getCommand() { | ||
return command; | ||
} | ||
|
||
@Override | ||
public CommandType getCommandType() { | ||
return CommandType.BinaryCommand; | ||
} | ||
|
||
@Override | ||
public void accept(ISerializeCommandVisitor commandVisitor) { | ||
commandVisitor.visit(this); | ||
} | ||
} |
218 changes: 218 additions & 0 deletions
218
engine/src/main/java/mscalc/engine/commands/COpndCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,218 @@ | ||
package mscalc.engine.commands; | ||
|
||
import mscalc.engine.Rational; | ||
import mscalc.engine.cpp.uint; | ||
import mscalc.engine.ratpack.RatPack; | ||
import mscalc.engine.ratpack.RatPack.NumberFormat; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
import static mscalc.engine.Commands.*; | ||
|
||
public class COpndCommand implements IOpndCommand { | ||
private static final char chNegate = '-'; | ||
private static final char chExp = 'e'; | ||
private static final char chPlus = '+'; | ||
|
||
private final List<Integer> commands = new ArrayList<>(); | ||
boolean fNegative; | ||
boolean fSciFmt; | ||
boolean fDecimal; | ||
boolean fInitialized; | ||
private final StringBuilder token = new StringBuilder(); | ||
private Rational value; | ||
|
||
public COpndCommand(List<Integer> commands, boolean fNegative, boolean fDecimal, boolean fSciFmt) { | ||
this.commands.addAll(commands); | ||
this.fNegative = fNegative; | ||
this.fDecimal = fDecimal; | ||
this.fSciFmt = fSciFmt; | ||
this.fInitialized = false; | ||
this.value = new Rational(); | ||
} | ||
|
||
public void initialize(Rational r) { | ||
this.value = r; | ||
this.fInitialized = true; | ||
} | ||
|
||
@Override | ||
public List<Integer> getCommands() { | ||
return Collections.unmodifiableList(this.commands); | ||
} | ||
|
||
@Override | ||
public void appendCommand(int command) { | ||
if (fSciFmt) | ||
{ | ||
ClearAllAndAppendCommand(CalculationManagerCommand.fromCommandInt(command)); | ||
} | ||
else | ||
{ | ||
commands.add(command); | ||
} | ||
|
||
if (command == IDC_PNT) | ||
{ | ||
fDecimal = true; | ||
} | ||
} | ||
|
||
void ClearAllAndAppendCommand(CalculationManagerCommand command) | ||
{ | ||
commands.clear(); | ||
commands.add(command.toCommandInt()); | ||
fSciFmt = false; | ||
fNegative = false; | ||
fDecimal = false; | ||
} | ||
|
||
@Override | ||
public void toggleSign() { | ||
for (int nOpCode : commands) | ||
{ | ||
if (nOpCode != IDC_0) | ||
{ | ||
fNegative = !fNegative; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void removeFromEnd() { | ||
if (fSciFmt) | ||
{ | ||
ClearAllAndAppendCommand(CalculationManagerCommand.Command0); | ||
} | ||
else | ||
{ | ||
int nCommands = commands.size(); | ||
|
||
if (nCommands == 1) | ||
{ | ||
ClearAllAndAppendCommand(CalculationManagerCommand.Command0); | ||
} | ||
else | ||
{ | ||
int nOpCode = commands.get(nCommands - 1); | ||
|
||
if (nOpCode == IDC_PNT) | ||
{ | ||
fDecimal = false; | ||
} | ||
|
||
commands.removeLast(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isNegative() { | ||
return fNegative; | ||
} | ||
|
||
@Override | ||
public boolean isSciFmt() { | ||
return fSciFmt; | ||
} | ||
|
||
@Override | ||
public boolean isDecimalPresent() { | ||
return fDecimal; | ||
} | ||
|
||
@Override | ||
public String getToken(char decimalSymbol) { | ||
final char chZero = '0'; | ||
|
||
int nCommands = commands.size(); | ||
token.setLength(0); | ||
|
||
for (int i = 0; i < nCommands; i++) | ||
{ | ||
int nOpCode = commands.get(i); | ||
|
||
if (nOpCode == IDC_PNT) | ||
{ | ||
token.append(decimalSymbol); | ||
} | ||
else if (nOpCode == IDC_EXP) | ||
{ | ||
token.append(chExp); | ||
int nextOpCode = commands.get(i + 1); | ||
if (nextOpCode != IDC_SIGN) | ||
{ | ||
token.append(chPlus); | ||
} | ||
} | ||
else if (nOpCode == IDC_SIGN) | ||
{ | ||
token.append(chNegate); | ||
} | ||
else | ||
{ | ||
char num = (char)(nOpCode - IDC_0); | ||
token.append(num); | ||
} | ||
} | ||
|
||
// Remove zeros | ||
for (int i = 0; i < token.length(); i++) | ||
{ | ||
if (token.charAt(i) != chZero) | ||
{ | ||
if (token.charAt(i) == decimalSymbol) | ||
{ | ||
// token.erase(0, i - 1); | ||
token.delete(0, i-1); | ||
} | ||
else | ||
{ | ||
// token.erase(0, i); | ||
token.delete(0, i); | ||
} | ||
|
||
if (fNegative) | ||
{ | ||
token.insert(0, chNegate); | ||
} | ||
|
||
return token.toString(); | ||
} | ||
} | ||
|
||
token.setLength(0); | ||
token.append(chZero); | ||
|
||
return token.toString(); | ||
} | ||
|
||
@Override | ||
public void setCommands(List<Integer> commands) { | ||
this.commands.clear(); | ||
this.commands.addAll(commands); | ||
} | ||
|
||
@Override | ||
public CommandType getCommandType() { | ||
return CommandType.OperandCommand; | ||
} | ||
|
||
public String getString(uint radix, int precision) | ||
{ | ||
if (fInitialized) | ||
{ | ||
return value.toString(radix, NumberFormat.Float, precision); | ||
} | ||
|
||
return ""; | ||
} | ||
|
||
@Override | ||
public void accept(ISerializeCommandVisitor commandVisitor) { | ||
commandVisitor.visit(this); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
engine/src/main/java/mscalc/engine/commands/CParentheses.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package mscalc.engine.commands; | ||
|
||
public class CParentheses implements IParenthesisCommand { | ||
private final int command; | ||
|
||
public CParentheses(int command) { | ||
this.command = command; | ||
} | ||
|
||
@Override | ||
public int getCommand() { | ||
return command; | ||
} | ||
|
||
@Override | ||
public CommandType getCommandType() { | ||
return CommandType.Parentheses; | ||
} | ||
|
||
@Override | ||
public void accept(ISerializeCommandVisitor commandVisitor) { | ||
commandVisitor.visit(this); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
engine/src/main/java/mscalc/engine/commands/CUnaryCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package mscalc.engine.commands; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class CUnaryCommand implements IUnaryCommand { | ||
private final List<Integer> command = new ArrayList<>(); | ||
|
||
public CUnaryCommand(int command) { | ||
this.command.add(command); | ||
} | ||
|
||
public CUnaryCommand(int command1, int command2) { | ||
this.command.add(command1); | ||
this.command.add(command2); | ||
} | ||
|
||
@Override | ||
public List<Integer> getCommands() { | ||
return Collections.unmodifiableList(command); | ||
} | ||
|
||
@Override | ||
public void setCommands(int command1, int command2) { | ||
this.command.clear(); | ||
this.command.add(command1); | ||
this.command.add(command2); | ||
} | ||
|
||
@Override | ||
public void setCommand(int command) { | ||
this.command.clear(); | ||
this.command.add(command); | ||
} | ||
|
||
@Override | ||
public CommandType getCommandType() { | ||
return CommandType.UnaryCommand; | ||
} | ||
|
||
@Override | ||
public void accept(ISerializeCommandVisitor commandVisitor) { | ||
commandVisitor.visit(this); | ||
} | ||
} |
Oops, something went wrong.