skullJ Posted April 21, 2010 Share Posted April 21, 2010 Im working on univercity project and a part of my script is to calculate some maths. All I need is to input varables into array and a mathematical action, and get the resolt as output. For example: <?php $variables['l1'] = 100; $variables['l2'] = 50; $variables['x'] = 60; $variables['y'] = 30; $phrase = "c = (l1 ^ 2 - l2 ^ 2 + x ^ 2 + y ^ 2) / 2"; $result = do_maths($phrase); echo $result; // the result is 6000 php?> I have some ideas and i can show you some code but all these drives me on deadend! Quote Link to comment https://forums.phpfreaks.com/topic/199258-iam-looking-for-function-to-do-all-maths/ Share on other sites More sharing options...
Mchl Posted April 21, 2010 Share Posted April 21, 2010 1. There's no such thing as php?> 2. Look into eval 3. Look up RPN on Google Quote Link to comment https://forums.phpfreaks.com/topic/199258-iam-looking-for-function-to-do-all-maths/#findComment-1045808 Share on other sites More sharing options...
JonnoTheDev Posted April 21, 2010 Share Posted April 21, 2010 I get a result of 72. Play about with it. <?php $variables['l1'] = 100; $variables['l2'] = 50; $variables['x'] = 60; $variables['y'] = 30; $result = ((($variables['l1'] ^ 2) - ($variables['l2'] ^ 2)) + (($variables['x'] ^ 2) + ($variables['y'] ^ 2))) / 2; echo $result; ?> Quote Link to comment https://forums.phpfreaks.com/topic/199258-iam-looking-for-function-to-do-all-maths/#findComment-1045824 Share on other sites More sharing options...
PFMaBiSmAd Posted April 21, 2010 Share Posted April 21, 2010 That's because ^ is a logical xor in php, not a pow() function. Quote Link to comment https://forums.phpfreaks.com/topic/199258-iam-looking-for-function-to-do-all-maths/#findComment-1045827 Share on other sites More sharing options...
ignace Posted April 21, 2010 Share Posted April 21, 2010 3. Look up RPN on Google How do you then handle 354 + 3? Or is it all separated by a space, but having an Assembler background I don't see how that could work as how would I know I had to read 1 byte, 2 bytes, 3 bytes? Quote Link to comment https://forums.phpfreaks.com/topic/199258-iam-looking-for-function-to-do-all-maths/#findComment-1045829 Share on other sites More sharing options...
JonnoTheDev Posted April 21, 2010 Share Posted April 21, 2010 That's because ^ is a logical or in php, not a pow() function. You can tell i'm crap at maths! Ah bitwise operator. Never used that before. PFMaBiSmAd, rewrite it buddy. Quote Link to comment https://forums.phpfreaks.com/topic/199258-iam-looking-for-function-to-do-all-maths/#findComment-1045832 Share on other sites More sharing options...
Mchl Posted April 21, 2010 Share Posted April 21, 2010 @ignace: read until delimiter or operation symbol Quote Link to comment https://forums.phpfreaks.com/topic/199258-iam-looking-for-function-to-do-all-maths/#findComment-1045835 Share on other sites More sharing options...
Daniel0 Posted April 21, 2010 Share Posted April 21, 2010 3. Look up RPN on Google How do you then handle 354 + 3? Or is it all separated by a space, but having an Assembler background I don't see how that could work as how would I know I had to read 1 byte, 2 bytes, 3 bytes? It's stack based: 354 - adds 354 to the stack 3 - adds 3 to the stack, now it contains: 354 3 + - pops an element from the stack and adds it. the stack now contains 357 If you traverse an abstract syntax tree in pre-order and just output the nodes to the screen instead of doing something with them, you'll get reverse polish notation. Quote Link to comment https://forums.phpfreaks.com/topic/199258-iam-looking-for-function-to-do-all-maths/#findComment-1045855 Share on other sites More sharing options...
skullJ Posted April 21, 2010 Author Share Posted April 21, 2010 1. There's no such thing as php?> 2. Look into eval 3. Look up RPN on Google so you propose to preg_replace the variables into numbers and then make maths with eval. Right? Quote Link to comment https://forums.phpfreaks.com/topic/199258-iam-looking-for-function-to-do-all-maths/#findComment-1045884 Share on other sites More sharing options...
Ken2k7 Posted April 21, 2010 Share Posted April 21, 2010 If you are using preg_replace, be careful with the regexp you're going to use. Make sure you are only replacing the variable. For example, if I have 2 variables: x and dxt, make sure you replace the x, but not also the x in dxt. Quote Link to comment https://forums.phpfreaks.com/topic/199258-iam-looking-for-function-to-do-all-maths/#findComment-1045888 Share on other sites More sharing options...
skullJ Posted April 21, 2010 Author Share Posted April 21, 2010 If you are using preg_replace, be careful with the regexp you're going to use. Make sure you are only replacing the variable. For example, if I have 2 variables: x and dxt, make sure you replace the x, but not also the x in dxt. that's exactly is that iam afraid of!!!! Im already searching for patterns...and i hate them Quote Link to comment https://forums.phpfreaks.com/topic/199258-iam-looking-for-function-to-do-all-maths/#findComment-1045889 Share on other sites More sharing options...
Mchl Posted April 21, 2010 Share Posted April 21, 2010 so you propose to preg_replace the variables into numbers and then make maths with eval. Right? This too. But also you will have to replace 'x ^ y' with pow(x,y). That's why I hinted on converting the formula to RPN first. IIRC there are PEAR libriaries that you can use. Quote Link to comment https://forums.phpfreaks.com/topic/199258-iam-looking-for-function-to-do-all-maths/#findComment-1045906 Share on other sites More sharing options...
skullJ Posted April 21, 2010 Author Share Posted April 21, 2010 What libraries? Can you explain this to me? Quote Link to comment https://forums.phpfreaks.com/topic/199258-iam-looking-for-function-to-do-all-maths/#findComment-1045935 Share on other sites More sharing options...
Mchl Posted April 21, 2010 Share Posted April 21, 2010 http://pear.php.net/package/Math_RPN Quote Link to comment https://forums.phpfreaks.com/topic/199258-iam-looking-for-function-to-do-all-maths/#findComment-1045936 Share on other sites More sharing options...
Daniel0 Posted April 21, 2010 Share Posted April 21, 2010 Here is my take on a very simple one: <?php function math_rpn($expr) { $stack = array(); $getOps = function() use (&$stack) { if (count($stack) < 2) { throw new Exception('Invalid expression: not enough elements on stack'); } $op2 = array_pop($stack); $op1 = array_pop($stack); return array($op1, $op2); }; $tok = strtok($expr, ' '); do { switch ($tok) { case '+': array_push($stack, array_sum($getOps())); break; case '-': list($op1, $op2) = $getOps(); array_push($stack, $op1 - $op2); break; case '*': list($op1, $op2) = $getOps(); array_push($stack, $op1 * $op2); break; case '/': list($op1, $op2) = $getOps(); array_push($stack, $op1 / $op2); break; case '^': list($op1, $op2) = $getOps(); array_push($stack, pow($op1, $op2)); break; default: array_push($stack, $tok); break; } } while ($tok = strtok(' ')); if (count($stack) !== 1) { throw new Exception('Invalid expression: end stack doesn\'t have exactly one element.'); } return $stack[0]; } echo math_rpn('1 2 + 5 * 2 /'); Quote Link to comment https://forums.phpfreaks.com/topic/199258-iam-looking-for-function-to-do-all-maths/#findComment-1045956 Share on other sites More sharing options...
skullJ Posted April 24, 2010 Author Share Posted April 24, 2010 RPN is working! This is my first example with RPN: <?php include('RPN.php'); function do_maths($expr){ global $variables; foreach($variables as $key => $value) { $expr = str_replace($key, $value, $expr); } $rpn = new Math_Rpn(); $result = $rpn->calculate($expr,'deg',false); return $result; } $variables['l1'] = 100; $variables['l2'] = 50; $variables['x'] = 60; $variables['y'] = 30; $phrase = "c = (l1 ^ 2 - l2 ^ 2 + x ^ 2 + y ^ 2) / 2"; $phrase = explode('=',$phrase); $result = do_maths($phrase[1]); echo $result; // the result is 6000 ?> But i really dont have luck with patterns and im using str_replace. Can anyone help me with pattern? Quote Link to comment https://forums.phpfreaks.com/topic/199258-iam-looking-for-function-to-do-all-maths/#findComment-1047552 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.