Skip to content

Commit 465632f

Browse files
committed
Added ability to enter in a percentage
- This will automatically divide that number by 100 and add it to the stack. For example, you can now enter `15%` and `0.15` will be added to the stack. - Added an automated unit test for the percent number entry. - Updated the [User Guide](https://frossm.github.io/RPNCalc-UserGuide) to reflect this change. - JUnit updated from 5.11.3 -> 5.11.4
1 parent e8a94b0 commit 465632f

File tree

8 files changed

+62
-12
lines changed

8 files changed

+62
-12
lines changed

.idea/misc.xml

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mdbook/src/Chapters/HighLevelUsage.md

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@
22

33
# High Level Usage
44

5-
RPNCalc is a command line application that must be run from a console / command prompt. Executing it with a `-h` (or `-?`) switch, or starting the program and entering the `h` (or `help` or `?`) command will display the in-program help page. This page lists all of the commands and operands that can be used, but it is fairly terse This can be also viewed at the bottom of the `Introduction Chapter` of this guide. This document is meant as a more comprehensive guide.
5+
RPNCalc is a command line application that must be run from a console / command prompt. Executing it with a `-h` (or `-?`) switch, or starting the program and entering
6+
the `h` (or `help` or `?`) command will display the in-program help page. This page lists all the commands and operands that can be used, but it is fairly terse This can be also viewed at the bottom of the `Introduction Chapter` of this guide. This document is meant as a more comprehensive guide.
67

78
There are various command line switches that can be used when starting the program as are detailed in the `Command Line Options Chapter`. They generally exist so that aliases can be used to control several key parameters, most likely the `-l StackName` switch.
89

910
Once inside the program, you'll be presented a prompt where numbers, operands, and commands may be entered. Numbers will be added to the stack which you can think of as an upside down stack of plates. The top stack item (represented by `line1` in the program) is on the bottom. You can think of this stack of plates as a Last In First Out (LIFO) approach.
1011

11-
For example, you could enter `2 [ENTER]` it would be in the `line1` position and would be on the top of the stack. If you then enter `3 [ENTER]` the `2` would move up go `line2` and the `3` would then be on `line1` and be on the top of the stack. You can then enter in an operand, such as `+` to perform the action on the items opn the top of the stack. To continue our example, pressing `+ [ENTER]` would take the top two items off of the stack, add them, and put the result back on top of the stack (`line1`).
12+
For example, you could enter `2 [ENTER]` it would be in the `line1` position and would be on the top of the stack. If you then enter `3 [ENTER]` the `2` would move up go `line2` and the `3` would then be on `line1` and be on the top of the stack. You can then enter an operand, such as `+` to perform the action on the items opn the top of the stack. To continue our example, pressing `+ [ENTER]` would take the top two items off of the stack, add them, and put the result back on top of the stack (`line1`).
1213

13-
I've gone into this in more detail in the `What is an RPN Calculator Chapter` and elsewhere and it's fairly easy and intuitive. Once you get the hang of it, you'll overwhelming regret having to use a standard calculator in the future. ;-)
14+
I've gone into this in more detail in the `What is an RPN Calculator Chapter` and elsewhere and it's fairly easy and intuitive. Once you get the hang of it, you will regret having to use a standard calculator in the future. ;-)
1415

1516
#### Why is the stack "upside down?"
1617

1718
One question I get with RPNCalc is why is the top of the stack on the bottom? The reason is that it's simply more intuitive. The command line is on the bottom. You are usually dealing with the top of the stack so having `line1` directly above makes sense. Also, for some operations, the order is important (think subtraction or division). Having `line1` "underneath" `line2` is easy to understand as that's how we learned to do subtraction. `line1` is subtracted from `line2`.
1819

1920
### Decimals & Fractions
2021

21-
In RPNCalc, the stacks always store numbers as decimals. You can, however, enter in fractions and they will be instantly converted to a decimal equivalent and added to the stack.
22+
In RPNCalc, the stacks always store numbers as decimals. You can, however, enter in fractions, and they will be instantly converted to a decimal equivalent and added to
23+
the stack.
2224

2325
**Example:**
2426

@@ -52,9 +54,14 @@ will display `1 293/6250`. This is a closer approximation than using base 16. T
5254

5355
As of version 5, scientific notation is supported. You can enter in values with the format `1.2345E18` and it will be saved as a value in the stack. There are a few areas where it's not 100% supported (i.e. NumOps at the time of this writing) but just about everything will work with it.
5456

57+
### Percent Entry
58+
59+
As of version 5.4.0, a number, followed by a `%` can be entered. This will be divided by 100 prior to being added onto the stack. This will work with both negative
60+
numbers and scientific notation.
61+
5562
## Operands, Numbers, and Commands
5663

57-
Numbers, whether decimal or fractions, can be entered on the command line and they get added to the stack. That's fairly self explanatory.
64+
Numbers, whether decimal or fractions, can be entered on the command line, and they get added to the stack. That's fairly self-explanatory.
5865

5966
Operands perform basic match functions on those numbers.
6067

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>org.fross</groupId>
55
<artifactId>rpncalc</artifactId>
6-
<version>5.3.6</version>
6+
<version>5.4.0</version>
77
<packaging>jar</packaging>
88

99
<name>rpncalc</name>
@@ -279,7 +279,7 @@
279279
<dependency>
280280
<groupId>org.junit.jupiter</groupId>
281281
<artifactId>junit-jupiter</artifactId>
282-
<version>5.11.3</version>
282+
<version>5.11.4</version>
283283
<scope>test</scope>
284284
</dependency>
285285

snap/snapcraft.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: rpncalc
2-
version: '5.3.6'
2+
version: '5.4.0'
33
summary: The command line Reverse Polish Notation (RPN) calculator
44
description: |
55
RPNCalc is an easy to use command line based Reverse Polish

src/main/java/org/fross/rpncalc/CommandParser.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,26 @@ public static void Parse(StackObj calcStack, StackObj calcStack2, String cmdInpu
508508
Output.debugPrintln("Placing the number '" + cmdInputCmd + "' onto the stack");
509509
calcStack.push(new BigDecimal(cmdInputCmd));
510510

511+
// If the number entered ends with a "%" then divide by 100 and add that to the stack
512+
} else if (cmdInputCmd.matches("^\\S*\\d%$")) {
513+
// Save current calcStack to the undoStack
514+
calcStack.saveUndo();
515+
516+
String num = "";
517+
try {
518+
num = cmdInputCmd.substring(0, cmdInputCmd.indexOf('%'));
519+
calcStack.push(new BigDecimal(num));
520+
calcStack.push("100");
521+
Math.Divide(calcStack);
522+
523+
Output.debugPrintln("A percent number was entered: " + num + "%");
524+
525+
} catch (IndexOutOfBoundsException ex) {
526+
Output.printColorln(Ansi.Color.RED, "Unable to parse '" + cmdInputCmd + "'");
527+
} catch (ArithmeticException | NullPointerException ex) {
528+
Output.printColorln(Ansi.Color.RED, "Error dividing " + num + " / 100");
529+
}
530+
511531
// Handle NumOps - numbers with a single operand at the end (*, /, +, -, ^)
512532
} else if (cmdInputCmd.matches("^-?\\d*\\.?\\d*[Ee]?\\d*[*+\\-/^]")) {
513533
// Save current calcStack to the undoStack

src/main/java/org/fross/rpncalc/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public static void main(String[] args) {
239239
}
240240

241241
// While in debug mode, show the entered text along with the broken up command and parameter
242-
Output.debugPrintln("Full cmdInput: '" + cmdInput + "' | cmdInputCommand: '" + cmdInputCmd + "' | cmdInputParameter: '" + cmdInputParam + "'");
242+
Output.debugPrintln("Full cmdInput: '" + cmdInput + "' | cmdInputCmd: '" + cmdInputCmd + "' | cmdInputParam: '" + cmdInputParam + "'");
243243

244244
// If recording is enabled, send the user input to be recorded
245245
if (UserFunctions.recordingIsEnabled()) {

src/main/java/org/fross/rpncalc/UserFunctions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ public static void FunctionRun(StackObj calcStack, StackObj calcStack2, String f
283283
String param = "";
284284

285285
try {
286-
// If the number of spaces in the full command is zero, don't execute a split so we don't get an ArrayIndexOutOfBoundsExceptionn
286+
// If the number of spaces in the full command is zero, don't execute a split so we don't get an ArrayIndexOutOfBoundsException
287287
// Reference: https://stackoverflow.com/questions/275944/how-do-i-count-the-number-of-occurrences-of-a-char-in-a-string
288288
if (fullCommand.codePoints().filter(ch -> ch == ' ').count() == 0) {
289289
command = fullCommand.toLowerCase().trim();

src/test/java/org/fross/rpncalc/CommandParserTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,28 @@ void testScientificNotationNumberEntry() {
225225

226226
}
227227

228+
// Test the entry of a percentage (15%, 2%, etc.)
229+
@Test
230+
void testPercentEntry() {
231+
StackObj stk = new StackObj();
232+
233+
CommandParser.Parse(stk, stk, "15%", "15%", "");
234+
assertEquals(1, stk.size());
235+
assertEquals("0.15", stk.peek().toString());
236+
237+
CommandParser.Parse(stk, stk, "1%", "1%", "");
238+
assertEquals(2, stk.size());
239+
assertEquals("0.01", stk.peek().toString());
240+
241+
CommandParser.Parse(stk, stk, "-85%", "-85%", "");
242+
assertEquals(3, stk.size());
243+
assertEquals("-0.85", stk.peek().toString());
244+
245+
// Not sure if this makes a lot of sense, but let's test scientific notation percentages
246+
CommandParser.Parse(stk, stk, "1.1e3%", "1.1e3%", "");
247+
assertEquals(4, stk.size());
248+
assertEquals("11", stk.peek().toString());
249+
250+
}
251+
228252
}

0 commit comments

Comments
 (0)