![]() Number = + // using Regex to describe terminal Op = // cheating use regex to describe terminal The normal method of evaluating Polish Notation expressions doesn't require recursion, you use a stack (like Forth or RPN) and evaluate as you go.Īn easy way to create a recursive version is to consider the expression language BNF then crafting a recursive descent parser from the grammer.įor example, a possible BNF would be: expr = op arg arg I don't necessarily need a code for this, maybe an explanation or if somebody could point me to the right direction where I can read about some algorithm / math formula or something that could help me better understand how to implement this. What I'm struggling is to figure out a way to do the operations if there is a math sign in between the numbers (like in the first an the second example). (I just separate the operator list and inverse it and I get two separate lists: one containing the operation signs and the other containing the numbers). I figured out how to make it if all the operations sings are in front of the digits in the input. This is a small console calculator where the input is inserted on a single line in the console.įor example the following input + / * + 65 32 46 2 - 1 1.25 should be translated into a math operation looking like this => ((65 + 32) * 46) / 2 + (1 - 1.25)Īnother example would be * + 3 2 - 9.5 6.5: this should be calculated in the following order 3 + 2 * (9.5 - 6.5).Īnother example / + 5 3 2 equals with => (5 + 3) / 2 I'm trying to figure out from the past several days a way to implement the following logic into a c# program. ![]() The polish notation articles posted in the comments helped me the most to understand the logic behind this Remove to substract from the list the numbers and the operator which I just used and after I continued with the recursive function on the new list obtained starting with the index from 0 again. Insert to insert the result at the current index, afterwards I used. ![]() After every "Encounter" of a operation sign (+-/*) I used the index - 1 and - 2 to get the previous 2 numbers and make the operation. I ended up using a List from the from the collections.Generic. I was limited to using System and using. ![]()
0 Comments
Leave a Reply. |