Does anyone have a flowchart for a simple calculator [+/-*, % and Sqr]?
I have figured most of it out (I need to embed one in an app)…
The problem I have is knowing what to display (the users current input vs the data in the accumulator)
Especially so it doesn’t start appending typed digits to a displayed ANSWER, but know to start a new input string.
I have search all over the net, and surprised that I cannot find some as mundane as this.
I do NOT want a pre-packaged widget (unless the source code is included)… as this is a very special need, and must be embedded as part of a larger app
Hi Dave, I certainly don’t have a calculator flowchart, but if you don’t mind explaining what you want in other terms, I am curious and don’t understand what you are asking for.
[quote=125697:@Dave S]The problem I have is knowing what to display (the users current input vs the data in the accumulator)
Especially so it doesn’t start appending typed digits to a displayed ANSWER, but know to start a new input string.[/quote]
I devised one such thing in the ancient age of QuickBasic, so have long since lost everything about it. But I remember having use a cheap electronic calculator to see what to do by playing with it. Basically that was my flowchart, as I faithfully copied the UI from it.
i’m almost certain i’ve seen Xojo (more likely Realbasic) source for something like this, all built into a subclassed textfield,
it will have been on the NUG though. i must have a copy somewhere, i will see what i can find.
I was looking for a CALCULATOR flowchart, not an expression parser which is what the SHUNTING YARD algorithm is…
as a matter of fact that is used quite a bit in the RetroBasic that I wrote, and posted about on this forum sometime last year.
What I needed (and have since figured out) is the flow of events for a 4 function calculator, what happens for each key press, moving data from input field to accumulator etc.
I will post this as a free widget later this evening.
an Infix calculator is easy… most postfix operations are easy…
The hard part if manipulating the display… you don’t want the user appending digits to the ANSWER
Ok… this is an ancient post… yet after a lifetime working as a software developer I still find a four-function calculator to be a mind-bender for some reason. Just 4 functions +, /, * and - … how easy could that be.
Not so much it seems.
I thought it would be…
enter a number
enter an operator
enter a number
calculate a sub-answer
enter an operator
enter another number
calculate new answer
as in
enter 8
enter +
enter 3
sub-answer is 11
enter /
enter 2
new answer is 5.5
WRONG… new answer is 9.5
instead of (8+3)/2 which is what the above steps seem to indicate, it actually calcuated 8+(3/2) (verified on 1/2 dozen calculator apps)
SO… I know I can create a stack and push/pop values and operators… but should the stack only be evaluated when the last operator is either * or / (or the user pressed the = key)?
There are no parend () or functions (sin, cos etc)… just 4 math functions, and various methods to display the results
All then flowcharts I can find are super simplistic and none address the need or use of operator precendence
I i recall from University days (we had to implement a 4 function calculator at some point)
2 stacks
1 for operators
1 for values (entered or intermediate results)
8 + 3 / 2 => 9.5
enter 8
gets pushed on value stack
enter +
get pushed on operator stack
enter 3
gets pushed on value stack // DO NOT PERFOM anything yet
enter /
get pushed on operator stack // high priority than + so need second input #
enter 2
perform top high priority operator
push result on value stack
enter =
now perform rest of operator stack adding and pushing until only 1 value remains on value stack
a second example
8 + 3 / 2 * 3 => 12.5
enter 8
gets pushed on value stack
enter +
get pushed on operator stack
enter 3
gets pushed on value stack // DO NOT PERFOM anything yet
enter /
get pushed on operator stack // high priority than + so need second input #
enter 2
perform top high priority operator
push result on value stack
enter *
get pushed on operator stack // high priority than remaining + so need second input #
enter 3
perform top high priority operator
push result on value stack
basically as long as the new operator is higher precedence than the top of the op stack you keep pushing
as soon as it is the same or lower you can unwind the stack to one that is the same precedence
you can extend this with other operator precedences if you like
yeah that was kind of where I was headed… trying now to figure what a calculator does when you press = multiple times
since the value stack should only have the last answer on it and the operator stack should be empy
if there more than one operator it executes all of the operator stack
if its the only operator it does nothing but show the single value that is on the value stack
In last summer’s Just Code Challenge, I posted a fraction calculator project. https://forum.xojo.com/48801-just-code-challenge-week-4-projects
It’s a postfix calculator, not infix. I had intended to make it operate in either mode, but ran out of time, and postfix seemed easier to do. The logic behind it was a half-baked state machine, done in a hurry to meet the deadline. So, maybe not the best example. With a bit of time a better state machine could be programmed, and that would be the way to do it I think.
Thought you might be interested in seeing where I went with this (note : I had the UI and feature set defined before I saw you post, so it all new code : ) ). This version is written in Swift, but I will be doing a desktop version for both macOS and Windows, using Xojo in a few weeks.