DIY Calculator

Posted: 11/22/2019 12:23:21 AM
dewster

From: Northern NJ, USA

Joined: 2/17/2012

Progress!

The terminal calc is mostly done, here are some screenshots:


The above shows decimal mode.


The above shows hex float mode.


The above shows 32.32 hex float mode.


The above shows the stack / memory highlight and the function key highlight in different locations.

Most of my goals have been met!  Numeric base is a display mode rather than a functional mode, so you can switch all you like without affecting anything.  The stack depth is unlimited, but only the first 10 entries are displayed.  Memory has 10 just slots, all displayed.  There is a highlight you can move around the stacks via SHIFT and the arrow keys to select things for copying / deleting / storing / recalling.  There is a similar highlight for the function keys you move up and down with CTRL and the up / down arrow keys to select the function bank.  You can just type the functions in too, which is usually faster for me.  White space alone dups the last stack entry (or the thing with the highlight).  DEL alone deletes the last stack entry (or the thing with the highlight).  Previous operation and status is shown above the command line.  Four angle modes: radians, degrees, grads, fractional (0 to 1 for full circle, like binary SIN2, etc.).

TODO:
- Kill trailing zeros in the hex float mode.
- Make the help file.
- File I/O?

[EDIT] Made the help file, typing "help" on the command line brings it up.  Moved all the hyperbolics to their own function bank.  Added "help" and "fix" to the last function bank.  There are amazingly few basic math functions even in a fairly advanced scientific calculator.  An undo of some sort would be helpful, not sure I want to go that far (HP often gives you a "last x" which is partially useful).

Posted: 11/22/2019 4:50:47 PM
dewster

From: Northern NJ, USA

Joined: 2/17/2012

Tangled Up In Blue

The green motif was a little too "restful" so I switched it to blue:

I really don't know what to do with the conflict between entering a negative number with the preceding '-' sign and using the '-' key to do subtraction immediately.  Right now I've got it so you have to enter white space after '+', '-', '*', and '/' to initiate the math function, which is awkward.  Not allowing direct negative number entry (entering numbers as non-negative and using the "chs" function) is how calculators get around this, perhaps I will end up doing the same.  But I don't really like either method.

Posted: 11/22/2019 6:35:58 PM
ILYA

From: Theremin Motherland

Joined: 11/13/2005

How about 'ctrl'  +  '-'  ?

Posted: 11/22/2019 7:59:22 PM
dewster

From: Northern NJ, USA

Joined: 2/17/2012

"How about 'ctrl'  +  '-'  ?"  - ILYA

Excellent idea!  Unfortunately I get no key codes for CTRL+ or CTRL-.  And CTRL* and CTRL/ give the same key codes as * and /.  Same for SHIFT.  ALT gives the same key codes (for +-*/) as no ALT. This is all in the Linux console, not sure about MS, but I would like for it to be portable.

I had +-*/ assigned to a bank of the function keys, but it wasn't very intuitive nor easy to use.

So many OS / SW layers between me and the keys, all doing their own thing.

Tried the CHS solution and it kinda sucks.

It seems as though I could use a more robust command line parser feeding the tokenizer, sort of a mini assembler.  But even that won't solve the leading '-' problem.

Posted: 11/22/2019 10:57:18 PM
dewster

From: Northern NJ, USA

Joined: 2/17/2012

Linux Launcher

So the way to make shortcuts to executable files in Linux Mint is to right click on the desktop and select "Create new launcher here..." then open and edit it with a text editor.  If it's a console app you can set the size of the console, and if the app needs access to local files you can set the working directory.  Here is the launcher for my calculator:

Code:
[Desktop Entry]
Name=term
Exec=gnome-terminal --geometry 80x20 -x  /home/demo/Documents/CPP/calc/_SRC_/calc
Comment=
Terminal=false
Icon=calc
Type=Application
Path=/home/demo/Documents/CPP/calc/_SRC_/
Name[en_US]=CALC

Works like a champ!  Note the explicit setting of the terminal dimensions via the "geometry" keyword.

[EDIT] Just edited things so that when single operand functions are performed stack[0] is first copied to memory[0].  Two operand functions also first copy stack[1] to memory[1].  So you can undo one level if things go pear shaped.

Posted: 11/25/2019 4:30:03 AM
dewster

From: Northern NJ, USA

Joined: 2/17/2012

Swapped Mem & Stack Columns

Rearranging things slightly. It's trivial I know, but the stack and input seem a bit more natural on the right, perhaps because the number pad on the keyboard is on the right?:

For me it's this aesthetic stuff at the end that often has the most payoff - simple to do but can really impact usability.

Posted: 12/7/2019 2:47:50 PM
dewster

From: Northern NJ, USA

Joined: 2/17/2012

Input Logic Done

Finally figured out input logic I can live with.  One trick was to intercept key strokes up-front and translate keys like '+' to mnemonics like "add" so that calling them back up via the command prompt history won't re-interpret them again.  This also keeps them from mixing back in with any number values on the same line.  So now I can type in a number and name an operation with a operation symbol key without having to separate the number and operation via white space.  Neither the last number nor the operation need to be on the stack to do their thing (though you can put them there if you want), thus saving a bunch of keystrokes, and thus behaving much like old HP calculators.  The biggest downside is you have to use the "chs" function to enter negative numbers, but you can use the '-' key to enter negative exponents as the logic is aware of input exponents in the various input modes.

Above is the current state of the calc.  I went ahead and moved all the trig functions over to the left.

I don't 100% like the way I've implemented floating point hex display - it should round to the display precision, kill trailing zeros, and automatically switch to / from exponential.  But I'm not completely sanguine with the way decimal float display is implemented in C++ either, as it's rather schizophrenic about what "fix" actually means, so it often rounds decimals when it should be switching to exponential to preserve the display of significant digits.  To me, "fix" should be all about significant digits, rather than decimal places.  But this is a fairly tough problem to solve in hex, and really tough in base 10.  (Why, oh why, did we "pick" base 10 for our arithmetic?  If I were king of the world I'd switch us all over to base 4 - kids learning their multiplication tables would thank me!  And you can "see" 4 of something at a glance without counting, not so with 10 of something.)

You must be logged in to post a reply. Please log in or register for a new account.