Skip to content

Commit

Permalink
Copy can have line num. Added several aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
frossm committed Oct 22, 2021
1 parent 4e3dcf2 commit 952ea7a
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 29 deletions.
17 changes: 9 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,21 @@ When the second enter is pressed, 2 will be removed from the stack. Added toge
|f \| flip | **FLIP SIGN:** Flip the sign on the top stack item (line 1). This is simply done by multiplying by -1|
|c \| clear| **CLEAR SCREEN:** Clear the screen, and empty the current stack. Memory data is retained and you can undo the clear with the undo command `u`|
|cl \| clean| **CLEAN SCREEN:** Clear the current screen, but keep the stack. After cleaning, the stack will be displayed at the top of the screen|
|d \| delete [Linenumber]| **DELETE LINE(s):** Delete the top stack item (line 1) with just a `d` command or, optionally, delete the line number provided with `d <linenumber>`|
|s \| swap [Line1] [Line2]| **SWAP LINES:** Swap the position of the top two stack items (line 1 & 2) with `s`. You can swap any two line items in your stack by providing the two line numbers `s # #`|
|%| **PERCENT:** Assumes line 1 contains a percent. This converts that into a number by simply multiplying the last value by 0.01. For example, if you want to take 50.123% of a number, you could just enter in `50.123 [ENTER] % [ENTER] *`|
|d \[#\] \| del \[#\] \| drop \[#\]| **DELETE:** Delete the top stack item (line 1) or, optionally, delete the line number provided with `d <linenumber>`|
|s \[#\] \[#\] \| swap \[#\] \[#\]| **SWAP LINES:** Swap the position of the top two stack items (line 1 & 2) with `s`. You can swap any two line numbers in your stack by providing the two line numbers `s # #`|
|%| **PERCENT:** Assumes line1 contains a percent. This converts that into a number by simply multiplying the last value by 0.01. For example, if you want to take 50.123% of a number, you could just enter in `50.123 [ENTER] % [ENTER] *`|
|sqrt| **SQUARE ROOT:** Perform a [square root](https://en.wikipedia.org/wiki/Square_root) of the top item in the stack|
|round [n]| **ROUND:** Round the top stack item to [n] decimal places. If [n] is not given, round to the nearest integer (zero decimal places). Example1: `3.14159` `round` would round to `3`. Example2: `3.14159` `round 4` would round to `3.1416`|
|aa [keep]| **ADD ALL:** Add all stack items together and return the result to the stack. If the optional `keep` command is sent, the elements added will be retained and the total will be added to the top of the stack. The entire `keep` command is not necessary, anything that starts with `k` will work|
|aa [keep]| **ADD ALL:** Add all stack items together and return the result to the stack. If the optional `keep` parameter is sent, the elements added will be retained on teh stack and the total will be added to the top of the stack. The entire `keep` command is not necessary, anything that starts with `k` will work|
|mod| **MODULUS:** Modulus is the remainder after a division. This command will perform a division of the top two stack items using the `/` operand and return the remainder only back to the stack|
|avg [keep]|**AVERAGE:** Calculate the average of the numbers on the stack. The stack will be replaced with the average value. If `keep` is provided, the stack will be retained and the average will be added on top. `avg` can also be called with the `average` or the `mean` command|
|sd [keep]|**STANDARD DEVIATION:** Calculate the standard deviation of the items in the stack. The stack items will be replaced by the result. If `keep` is provided, the the standard deviation will simple be added to the top of the stack in line1|
|copy|**COPY:** Adds a copy of the top stack item (line 1) back on the stack. The result is you'll have two of the same items on top of the stack|
|log, log10|**LOGARITHM:** Calculates the [natural logarithm (base e)](https://en.wikipedia.org/wiki/Natural_logarithm) or the [base10 logarithm](https://en.wikipedia.org/wiki/Common_logarithm)|
|int| **INTEGER:** Converts the top stack item (line 1) to it's integer value. This will discard the decimal portion regardless of it's value. For example: `4.34` will result in `4`. `4.999` will also result in `4`|
|copy [#] \| dup [#]|**COPY:** Adds a copy of the top stack item (line 1) back on the stack by default. If the optional line number is given, it will copy that value to the stack|
|log|**LOGARITHM BASE e:** Calculates the [natural logarithm (base e)](https://en.wikipedia.org/wiki/Natural_logarithm)|
|log10|**LOGARITHM BASE 10:** Calculates the [base10 logarithm](https://en.wikipedia.org/wiki/Common_logarithm)|
|int| **INTEGER:** Converts the top stack item (line 1) to it's integer value. This will discard the decimal portion regardless of it's value. For example: `4.34` will result in `4`. `4.999` will also result in `4`. If rounding is desired, execute the `round` command prior to `int`|
|abs| **ABSOLUTE VALUE:** Takes the [absolute value](https://en.wikipedia.org/wiki/Absolute_value#:~:text=In%20mathematics%2C%20the%20absolute%20value,and%20%7C0%7C%20%3D%200) of line 1. The returns the positive value of the number|
|rand [low] [high] | **RANDOM NUMBER GENERATION:** Generate a random integer number between the provided [l]ow and [h]igh numbers inclusive to both. If no numbers are provided, then the random number will be between 1 and 100 inclusive|
|rand \[low\] \[high\] | **RANDOM NUMBER GENERATION:** Generate a random integer number between the provided [l]ow and [h]igh numbers inclusive to both. If no numbers are provided, then the random number will be between 1 and 100 inclusive|
|dice XdY| **DICE ROLL:** Roll a Y sided die X times and add the results to the stack. Default is 1d6. While not a normal calculator function, I find it fun|

## Conversions
Expand Down
Binary file modified graphics/ScreenShot.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

<groupId>org.fross</groupId>
<artifactId>rpncalc</artifactId>
<version>2.6.2</version>
<version>2.7.0</version>
<packaging>jar</packaging>

<name>rpncalc</name>
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: '2.6.2'
version: '2.7.0'
summary: The command line Reverse Polish Notation (RPN) calculator
description: |
RPNCalc is an easy to use command line based Reverse Polish
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/org/fross/rpncalc/Help.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,21 @@ public static void Display() {
Output.printColorln(Ansi.Color.WHITE, " u Undo last action");
Output.printColorln(Ansi.Color.WHITE, " f Flip the sign of the element at line1");
Output.printColorln(Ansi.Color.WHITE, " c Clear the screen and empty current stack");
Output.printColorln(Ansi.Color.WHITE, " clean Clear screen but keep the stack values");
Output.printColorln(Ansi.Color.WHITE, " d [#] Delete the line1 value or the line number provided");
Output.printColorln(Ansi.Color.WHITE, " cl[ean] Clear screen but keep the stack values");
Output.printColorln(Ansi.Color.WHITE, " d [#] Delete line1 or the line number provided");
Output.printColorln(Ansi.Color.WHITE, " s [#] [#] Swap the last two elments in the stack or the lines provided");
Output.printColorln(Ansi.Color.WHITE, " % Convert line1 into a percentage by multipling it by 0.01");
Output.printColorln(Ansi.Color.WHITE, " sqrt Perform a square root on line1");
Output.printColorln(Ansi.Color.WHITE, " sqrt Perform a square root of the line1 value");
Output.printColorln(Ansi.Color.WHITE, " round [n] Round to n decimal places. Default is 0 decimals");
Output.printColorln(Ansi.Color.WHITE, " aa [keep] Add all stack items. Adding 'keep' will keep existing elements");
Output.printColorln(Ansi.Color.WHITE, " mod Modulus. Perform a division and return the remainder");
Output.printColorln(Ansi.Color.WHITE, " avg [keep] Replace stack with average of values. 'keep' will retain stack");
Output.printColorln(Ansi.Color.WHITE, " sd [keep] Standard deviation of stack items. 'keep' will retain stack");
Output.printColorln(Ansi.Color.WHITE, " copy Copy line1 and add it to the stack");
Output.printColorln(Ansi.Color.WHITE, " copy [#] Copy line1 or the optional line number and add it to the stack");
Output.printColorln(Ansi.Color.WHITE, " log | log10 Calculate the natural (base e) or base10 logarithm");
Output.printColorln(Ansi.Color.WHITE, " int Convert line1 to an integer by discarding after the decimal");
Output.printColorln(Ansi.Color.WHITE, " int Convert line1 to an integer. No rounding is performed");
Output.printColorln(Ansi.Color.WHITE, " abs Take the absolute value of line1");
Output.printColorln(Ansi.Color.WHITE, " rand [L] [H] Random integer between X and Y inclusive. Default is 1-100");
Output.printColorln(Ansi.Color.WHITE, " rand [L] [H] Random integer between L and H inclusive. Default is 1-100");
Output.printColorln(Ansi.Color.WHITE, " dice XdY Roll a Y sided die X times. Default is 1d6");

Output.printColorln(Ansi.Color.YELLOW, "\nConversions:");
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/fross/rpncalc/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,9 +308,9 @@ public static void main(String[] args) {
break;

// Delete
case "delete":
case "del":
case "d":
case "drop":
// If Parameter is empty, delete the value on the top of the stack
if (cmdInputParam.isEmpty())
StackOps.cmdDelete("1");
Expand Down Expand Up @@ -365,7 +365,8 @@ public static void main(String[] args) {

// Copy Item
case "copy":
StackOps.cmdCopy();
case "dup":
StackOps.cmdCopy(cmdInputParam);
break;

// Natural (base e) Logarithm
Expand Down
49 changes: 38 additions & 11 deletions src/main/java/org/fross/rpncalc/StackOps.java
Original file line number Diff line number Diff line change
Expand Up @@ -537,33 +537,60 @@ public static void cmdStdDeviation(String arg) {
for (int i = 0; i < Main.calcStack.size(); i++) {
stdArray[i] = java.lang.Math.pow((Main.calcStack.get(i) - mean1), 2);
}

// Step3: Work out the mean of those squared differences
Double mean2 = Math.Mean(stdArray);
Output.debugPrint("Secondary mean of (number-mean)^2: " + mean2);
if (keepFlag == false)

if (keepFlag == false)
Main.calcStack.clear();

// Step4: Take the square root of that result and push onto the stack
Double result = java.lang.Math.sqrt(mean2);
Main.calcStack.push(result);
}

/**
* cmdCopy(): Copy the item at the top of the stack
* cmdCopy(): Copy the item at the top of the stack or the line number provided
*
*/
@SuppressWarnings("unchecked")
public static void cmdCopy() {
public static void cmdCopy(String arg) {
int lineNum = 1;

// Ensure we have at least one number to copy
if (Main.calcStack.size() < 1) {
Output.printColorln(Ansi.Color.RED, "Error: The stack must contain at least one number to copy");
return;
}

// Determine line number to copy
try {
lineNum = Integer.parseInt(arg);
} catch (NumberFormatException ex) {
if (!arg.isBlank()) {
Output.printColorln(Ansi.Color.RED, "ERROR: '" + arg + "' is not a valid line number");
return;
}
}

// Save to undo stack
Main.undoStack.push((Stack<Double>) Main.calcStack.clone());

Output.debugPrint("Copying the item at the top of the stack");
if (Main.calcStack.size() >= 1) {
Main.calcStack.add(Main.calcStack.lastElement());
} else {
Output.printColorln(Ansi.Color.RED, "ERROR: Must be an item in the stack to copy it");
Output.debugPrint("Copying line" + lineNum + " to line1");

// Copy the provided number if it's valid
try {
// Ensure the number entered is is valid
if (lineNum < 1 || lineNum > Main.calcStack.size()) {
Output.printColorln(Ansi.Color.RED, "Invalid line number entered: " + lineNum);
} else {
// Perform the copy
Main.calcStack.push(Main.calcStack.get(Main.calcStack.size() - lineNum));
}
} catch (Exception e) {
Output.printColorln(Ansi.Color.RED, "Error parsing line number for element copy: '" + lineNum + "'");
Output.debugPrint(e.getMessage());
}
}

Expand Down

0 comments on commit 952ea7a

Please sign in to comment.