Calculator Flowchart

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.

Thanks

Julen

[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.

found it. mathfield.

http://www.benandruby.com/bens_software/

don’t know if it helps, but it cant hurt :wink:

Wikipedia has a good explanation of the shunting yard algorithm that might help.

Shunting Yard

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.

check here --> flowchart examples

No calculator flowchart there. YAS :confused:

I dont see the need of a flowchart for a calculator. I know it is early for me and I havent had my caffeine and that is hurting the brain waves.

every calc I have used, it takes a number then an operator (+ - * / SQR etc) then another number (for some of the operators).

i.e.

4 + 3 (number + operator + number)
4 - 3 (number + operator + number)
4 * 3 (number + operator + number)
4 / 3 (number + operator + number)

2 SQR (number + operator)

please let me know if I am missing the point or missing a key part of the question.

sb

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

but no worries… I have it working

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

3 + 3 = = = =
results in 15

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.

It seems a standard calculator does the following

  • If an operator is entered NOT preceded by a number, it assumes zero… therefore +/- result in the next number entered, / or * result in zero
  • if the equal sign is executed multiple times then the last entered operator and last entered number are entered onto the stack again, and executed
  • if multiple operators are entered in sequence, the results seem to vary based on the app being used as the control test.

This app, while it involes fractions, also works in feet and inches and allows conversions between feet/inch to inch to decimal and is an INFIX mode.

Robert, mine has a fraction entry scheme very similiar to your, I ran across it when reading about some old CASIO calculator the other day.

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.

I wrote a calc once…
First make a routine to convert infix to postfix
Second step make a reverse polish calculator out of a stack.

Thanks Brian… I actually have it all working now, and that is exactly what I did