Jump to content

Iam looking for function to do all maths


skullJ

Recommended Posts

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! :(

 

 

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :(

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 /');

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.