LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Operator Precedence

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

 

Best Regards,
Dhans 😉
Kudos are welcome 😉
Aspirant Labview Programmer (Labview 14) 😉
0 Kudos
Message 1 of 20
(4,323 Views)

Inside of what program?

0 Kudos
Message 2 of 20
(4,318 Views)

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:

Precedence.png

 

Bob Schor

0 Kudos
Message 3 of 20
(4,313 Views)

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.

Best Regards,
Dhans 😉
Kudos are welcome 😉
Aspirant Labview Programmer (Labview 14) 😉
0 Kudos
Message 4 of 20
(4,300 Views)
Do you have to use that notation? With RPN the solution would be trivial.

Mike...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 5 of 20
(4,281 Views)

"Eval Formula String"

/Y

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 6 of 20
(4,269 Views)

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
0 Kudos
Message 7 of 20
(4,246 Views)

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.

0 Kudos
Message 8 of 20
(4,238 Views)

@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

 

========================
=== Engineer Ambiguously ===
========================
0 Kudos
Message 9 of 20
(4,234 Views)

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

0 Kudos
Message 10 of 20
(4,230 Views)