Skip to content

Commit 480bc2a

Browse files
committed
Added relative numbers to delete cmd
Added additional unit tests to check this Useful for UDFs or when the number of items on the stack is unknown Updated User Manual to reflect this change
1 parent 5634c1f commit 480bc2a

File tree

5 files changed

+34
-12
lines changed

5 files changed

+34
-12
lines changed

mdbook/src/Chapters/CalculatorCommands.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ names and abbreviations for the same command. This is just to make it easier to
1919
| c <br> clear | **CLEAR**<br>Clear the current stack and the screen. Memory data is retained and you can undo the clear with the undo `u` command |
2020
| cl <br> clean | **CLEAN SCREEN**<br>Clear the current screen, but keep the stack. After cleaning, the stack will be displayed at the top of the screen. Used to remove the clutter |
2121
| copy [#] <br> dup [#] | **COPY**<br>With no number provided, copy will duplicate the top stack item / `line1` and place it on the stack. If the optional line number is given, the value at that line will be duplicated to the top of the stack. A negative number may also be provided which will be a relative reference. For example, if `-1` is provided, the `line2` value will be copied. This is very useful when the command is used during the creation of a User Defined Function and the amount of numbers on the stack is unknown |
22-
| d<br>d [#]<br>d [#-#]<br>delete [#-#] | **DELETE**<br>If `d` is given without any parameters, it will delete `line1`. If a line number is given after the `d`, that line number will be deleted. A range can be given as well, such as `d 1-3` and RPNCalc will delete those lines and everything in between. `del` can also be used |
22+
| d<br>d [#]<br>d [#-#]<br>delete [#-#] | **DELETE**<br>If `d` is given without any parameters, it will delete `line1`. If a line number is given after the `d`, that line number will be deleted. A range can be given as well, such as `d 1-3` and RPNCalc will delete those lines and everything in between. A negative number can also be provided which is a relative reference. `-1` will delete the value one behind the current top of the stack value. Very useful for user defined functions where the stack may be unknown. `del` may also be used |
2323
| dice XdY<br>roll XdY | **DICE ROLL**<br>Roll a Y sided die X times and add the results to the stack. Default is 1d6 which roll a six sided dice one time. This is a more specific version of `random`. While not a normal calculator function, it's fun! (Especially if you're an old school gamer) |
2424
| fact <br>factorial | **FACTORIAL**<br>Takes the factorial of `line1`. The factorial target number must be an integer, so if there is a decimal component, it will be dropped (not rounded) as with the `int` command |
2525
| f <br> flip | **FLIP SIGN**<br>Flip the sign on the top stack item (`line1`). This is effectively multiplying `line1` by `-1` |

pom.xml

Lines changed: 1 addition & 1 deletion
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.5.0</version>
6+
<version>5.6.0</version>
77
<packaging>jar</packaging>
88

99
<name>rpncalc</name>

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.5.0'
2+
version: '5.6.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/StackCommands.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ public static void cmdCopy(StackObj calcStack, String arg) {
230230
* @param arg Method argument
231231
*/
232232
public static void cmdDelete(StackObj calcStack, String arg) {
233-
int startLine = -1;
234-
int endLine = -1;
233+
int startLine = 0;
234+
int endLine = 0;
235235

236236
// Ensure we have at least one item on the stack
237237
if (calcStack.isEmpty()) {
@@ -240,17 +240,23 @@ public static void cmdDelete(StackObj calcStack, String arg) {
240240
}
241241

242242
// Remove any spaces from arg
243-
arg = arg.replace(" ", "");
243+
arg = arg.trim().replace(" ", "");
244244

245245
// Determine line to delete by looking at arg
246246
try {
247247
// If this doesn't have an exception, user has entered a single number integer
248-
// Simple delete that line
249248
startLine = Integer.parseInt(arg);
249+
250+
// If it's a negative number, it's relative
251+
if (startLine <= -1) {
252+
startLine = 1 + java.lang.Math.abs(startLine);
253+
}
254+
255+
// Set the endLine to the same as the start so we just delete that line
250256
endLine = startLine;
251257

252258
} catch (NumberFormatException ex) {
253-
// If a dash is present, user entered a range
259+
// If a dash is present elsewhere, user entered a range to delete
254260
if (arg.contains("-")) {
255261
try {
256262
startLine = Integer.parseInt(arg.split("-")[0]);
@@ -261,13 +267,13 @@ public static void cmdDelete(StackObj calcStack, String arg) {
261267
}
262268
}
263269

264-
// An invalid argument was provided
265-
if (!arg.isBlank() && startLine == -1) {
270+
// An invalid or no argument was provided
271+
if (!arg.isBlank() && startLine == 0) {
266272
Output.printColorln(Ansi.Color.RED, "Invalid line number provided: '" + arg + "'");
267273
return;
268274

269-
} else if (startLine == -1) {
270275
// No argument was provided - delete the item on the top of the stack
276+
} else if (startLine == 0) {
271277
startLine = 1;
272278
endLine = 1;
273279
}

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

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,22 @@ void testCmdDelete1Line() {
333333
StackCommands.cmdAddAll(stk, "keep");
334334
assertEquals(5, stk.size());
335335
assertEquals(19, stk.peek().doubleValue());
336+
337+
// Delete -2
338+
stk.clear();
339+
stk.push(5.0);
340+
stk.push(4.0);
341+
stk.push(3.0);
342+
stk.push(2.0);
343+
stk.push(1.0);
344+
StackCommands.cmdDelete(stk, "-2");
345+
assertEquals(4, stk.size());
346+
assertEquals("2.0", stk.get(2).toString());
347+
348+
// Delete -3
349+
StackCommands.cmdDelete(stk, "-3");
350+
assertEquals(3, stk.size());
351+
assertEquals("4.0", stk.get(0).toString());
336352
}
337353

338354
/**
@@ -394,7 +410,7 @@ void testCmdDeleteParmError() {
394410
assertEquals(5, stk.size());
395411
StackCommands.cmdDelete(stk, "5-52a");
396412
assertEquals(5, stk.size());
397-
StackCommands.cmdDelete(stk, "-1");
413+
StackCommands.cmdDelete(stk, "x-1");
398414
assertEquals(5, stk.size());
399415
StackCommands.cmdDelete(stk, "4-1000");
400416
assertEquals(5, stk.size());

0 commit comments

Comments
 (0)