Skip to content

Commit

Permalink
NumOp support has been added for scientific notation
Browse files Browse the repository at this point in the history
Also added unit testing for this as well as adding a few normal number
entry tests
  • Loading branch information
frossm committed May 31, 2023
1 parent 976a110 commit 6fffe73
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

<groupId>org.fross</groupId>
<artifactId>rpncalc</artifactId>
<version>5.0.5</version>
<version>5.0.6</version>
<packaging>jar</packaging>

<name>rpncalc</name>
<description>RPNCalc is an easy to use console based RPN calculator</description>
<url>https://fross.org</url>
<url>https://github.com/frossm/rpncalc</url>

<developers>
<developer>
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: rpncalc
version: '5.0.5'
version: '5.0.6'
summary: The command line Reverse Polish Notation (RPN) calculator
description: |
RPNCalc is an easy to use command line based Reverse Polish
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/fross/rpncalc/CommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -464,15 +464,15 @@ public static void Parse(StackObj calcStack, StackObj calcStack2, String cmdInpu
}

// Number entered, add to stack.
} else if (cmdInputCmd.matches("^-?\\d*\\.?\\d*")) {
} else if (cmdInputCmd.replaceAll(" ", "").matches("^-?\\d*\\.?\\d*")) {
// Save current calcStack to the undoStack
calcStack.saveUndo();

Output.debugPrint("Placing the number '" + cmdInputCmd + "' onto the stack");
calcStack.push(new BigDecimal(cmdInputCmd));
calcStack.push(new BigDecimal(cmdInputCmd.replaceAll(" ", "")));

// Handle numbers with a single operand at the end (a NumOp)
} else if (cmdInputCmd.matches("^-?\\d*(\\.)?\\d* ?[\\*\\+\\-\\/\\^]")) {
} else if (cmdInputCmd.replaceAll(" ", "").matches("^-?\\d*\\.?\\d*[Ee]?\\d*[\\*\\+\\-\\/\\^]")) {
// Save current calcStack to the undoStack
calcStack.saveUndo();

Expand Down
40 changes: 39 additions & 1 deletion src/test/java/org/fross/rpncalc/CommandParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,18 @@ void testNormalNumberEntry() {
CommandParser.Parse(stk1, stk2, "123.321", "123.321", "");
assertEquals(123.321, stk1.peek().doubleValue());
assertEquals(3, stk1.size());

CommandParser.Parse(stk1, stk2, ".234", ".234", "");
assertEquals(.234, stk1.peek().doubleValue());
assertEquals(4, stk1.size());

CommandParser.Parse(stk1, stk2, "123.321e12", "123.321e12", "");
assertEquals("123.321E+12", stk1.peek().toEngineeringString());
assertEquals(5, stk1.size());

CommandParser.Parse(stk1, stk2, "-.1E44", "-.1E44", "");
assertEquals("-10E+42", stk1.peek().toEngineeringString());
assertEquals(6, stk1.size());
}

// Test fractional inputs
Expand Down Expand Up @@ -143,7 +155,33 @@ void testNumOp() {
assertEquals(922.6115, stk1.peek().doubleValue());

// Test scientific notation with NumOps
// TODO
stk1.clear();
stk1.push(-123.456e12);
CommandParser.Parse(stk1, stk2, "123.456e12/", "123.456e12/", "");
assertEquals(1, stk1.size());
StackCommands.cmdRound(stk1, "4");
assertEquals("-1.0000", stk1.peek().toString());

CommandParser.Parse(stk1, stk2, "3.3E14*", "3.3E14*", "");
assertEquals(1, stk1.size());
StackCommands.cmdRound(stk1, "4");
assertEquals("-330000000000000.0000", stk1.peek().toString());

CommandParser.Parse(stk1, stk2, "2/", "2/", "");
assertEquals(1, stk1.size());
StackCommands.cmdRound(stk1, "4");
assertEquals("-165000000000000.0000", stk1.peek().toString());

CommandParser.Parse(stk1, stk2, "2.34e12/", "2.234e12/", "");
assertEquals(1, stk1.size());
StackCommands.cmdRound(stk1, "9");
assertEquals("-70.512820513", stk1.peek().toString());

CommandParser.Parse(stk1, stk2, ".02e2^", ".02e2^", "");
assertEquals(1, stk1.size());
StackCommands.cmdRound(stk1, "8");
assertEquals("4972.05785670", stk1.peek().toString());

}

// Test the entry of a scientific notation number
Expand Down

0 comments on commit 6fffe73

Please sign in to comment.