06-01-2016 10:48 PM
Hi, I am working on a standard calculator project. I need to know how to bring Operator Predence logic inside the program. Any one help! Thanks in advance
06-01-2016 11:05 PM
Inside of what program?
06-01-2016 11:25 PM
I think that the Principle of Data Flow defines operator precedence -- an Operator doesn't proceed until all of its Inputs are satisfied. Thus while 2+3*4 requires a precedence "rule" to know if this is (2+3)*4 or 2+(3*4) (the latter is the usual "rule"), there is no ambiguity in LabVIEW:
Bob Schor
06-02-2016 12:12 AM
In the calculator program. I am storing Values as string in the array. So at the end of the calculation i need to extact vaues. Let me show you a simple example.
1+2*8-6 . This is the data that is shown in the display when i press on the calculator. Naturally, We calculate this exprssion as 1+(2*8)-6 = 11. But since i am storing operators as string, i doubt that there may be a chance for precednce fault when i implement calcuation logic while extracting those datas from array like (((1+2)*8)-6) .Thats why i am asking how to incorporate operator precedence logic.
06-02-2016 02:03 AM
06-02-2016 03:43 AM
"Eval Formula String"
/Y
06-02-2016 09:05 AM
I'd be interested to see any calculator that interprets 1+2*8-6 as 1+(2*8)-6. Calculators solve equations in the order they are input unless you have the ability to enter parentheses into the calculation. IMHO, it's not a good calculator if it makes assumptions about what the operator wants, rather than doing what the operator says.
06-02-2016 09:09 AM
I'm surprised that you are surprised.
Any scientific calculator I've used in the last 30 years uses the normal math order of operations.
Now some cheap calculator that is used by any non-math people to help them balance their checkbook, that may resort to the order of operations being based on order of entry.
06-02-2016 09:13 AM
@aputman wrote:I'd be interested to see any calculator that interprets 1+2*8-6 as 1+(2*8)-6. Calculators solve equations in the order they are input unless you have the ability to enter parentheses into the calculation. IMHO, it's not a good calculator if it makes assumptions about what the operator wants, rather than doing what the operator says.
aputman is correct here.
If I enter 1+2*8-6 im my calculator it responds like this:
I enter 1+2 as soon as I hit * the calculator shows the answer for 1+2 (3)
I hit the 8 and the calculator shows 24
Finish with - 6 and the answer is 18
06-02-2016 09:16 AM
While aputman makes a good point, if you want to actually define and assign operator precedence to such a string, then you need to parse the string by precedence. Find the high priority operators such as * first and evaluate. Then repeat the parsing for the next level of precedence. That kind of process can be extended to as many levels of precedence as you want. It can get pretty messy of you have things like -2^4*7+5*6. Plus you probably need to include parsing of parentheses for situations which are not conveniently encoded in simple strings.
Lynn