PHP convert operators to function calls - Polish notation algorithm
$30-250 USD
已取消
已发布大约 8 年前
$30-250 USD
货到付款
I need you to develop a PHP function, which implements some variant of the Polish notation algorithm or Reverse Polish Notation algorithm, whichever is better suitable (see wikipedia for detais). The function will take the $formula variable as parameter, and it will parse it to the Polish notation, and then it will construct function calls instead of operators.
For example, the input variable will be like this:
$formula="12+4*x+(11/(3+5*y))*exp(10;2)-3"
The following operations must be converted to function calls:
a + b ... PLUS(a;b)
a - b ... MINUS(a;b)
a * b ... MULTIPLY(a;b)
a / b ... DIVIDE(a;b)
a > b ... GT(a;b)
a < b ... LT(a;b)
a >= b ... GE(a;b)
a <= b ... LE(a;b)
a = b ... EQ(a;b)
a != b ... NE(a;b)
a && b ... AND(a;b)
a || b ... OR(a;b)
fn(...) ... fn(...) - keep functions as functions, but remember to process the function arguments
Note that * and / have higher priority than + and -,
Note that comparison operators like < and > have low priority than + - * /
Note that && has higher priority than ||, and both these have higher priority than everything else.
Example results:
Formula like "10+5" will be converted to "PLUS(10;5)".
Formula like "10+9*xyz-5*exp(x+2;3)" will be converted to: MINUS(PLUS(10;MULTIPLY(9;xyz));MULTIPLY(5;EXP(PLUS(x;2);3))).
Formula like "a>b+3" will be converted to "GT(a;PLUS(b;3))".
Formula like "(10*(x+y))" will produce result MULTIPLY(10;PLUS(x;y)) - note the missing unnecessary parenthesis.
Remember that "10+5" is the same as "10 + 5", and also "func(x)" is the same like "func ( x )" (notice added spaces in both examples)
The result will never contain any of the original operands like +-*/etc, and will always be as a string with function calls.
Remember that function name is always followed by open parentheses. If a number or character sequence is followed by something else, then it is considered to be operand, for example 10; 5; xyz; x12; abc; etc
The PHP code must be short and clean. Add comments where necessary, not everywhere.
Thank you for reading so far. Happy bidding! :)
Hello,
I have been working in web development and web design since I began my career in IT following an IT degree and specializing in the web side. I enjoy working on websites and pages and the interaction with the clients to ensure that the website is as they specifically want it.
I am a real problem solver and work in a methodical manner. I concentrate very hard in my work and like to ensure that I meet my deadlines and work to the clients timeframes.
I would welcome the opportunity to discuss this with you further but if you have any questions before hand, please do not hesitate to contact me and I will be happy to answer anything you need to know related to my previous experience.
Thank you ..!!