PHPBuilder - Cramers Rule



RSS Twitter
Snippets Math Functions

Cramers Rule

by: Craig
|
January 8, 2005

Version: 1.0.0

Type: Full Script

Category: Math Functions

License: Artistic License

Description: This is a php function that will let you quickly solve the following types of problems as well as display what was done to find the answer (all problems are algebra math level or above): Substitution Elemination



Format:<br>
Ax + By = E<br>
Cx + Dy = F<br>
Where A-F = already known numbers preceding variables<br>
and x-y = the variables you're want to find the value of.
<?
function calc($a, $b, $c, $d, $e, $f){
$numbers = array(1 => $a, 2 => $b, 3 => $c, 4 => $d, 5 => $e, 6 => $f);

for($x=1; $x < 7; $x++){
	$slash = strpos($numbers[$x], "/");
	if($slash !== false){
		list($y, $z) = explode("/", $numbers[$x]);
		$numbers[$x] = $y / $z;
		$numbers[$x] = round($numbers[$x], 20);
	}
}
	$answer[0] = ($numbers[1] * $numbers[4]) - ($numbers[3] * $numbers[2]);
	echo "$numbers[1] $numbers[2]<br>
		  $numbers[3] $numbers[4]<br>";
	echo "Array: $answer[0]<p>";
	
	$answer[0] = ($numbers[5] * $numbers[4]) - ($numbers[6] * $numbers[2]);
	echo "$numbers[5] $numbers[2]<br>
		  $numbers[6] $numbers[4]<br>";
	echo "X-array: $answer[0]<p>";
	
	$answer[0] = ($numbers[1] * $numbers[6]) - ($numbers[3] * $numbers[5]);
	echo "$numbers[1] $numbers[5]<br>
		  $numbers[3] $numbers[6]<br>";
	echo "Y-array: $answer[0]<p>";
	
	$answer[0] = $numbers[1] * $numbers[4];
	$answer['b'] = $answer[0] - ($numbers[3] * $numbers[2]);
	if($answer['b'] == 0){
		die("Invalid Input, A-D cannot equal 0");
	}
	
	$answer[1] = $numbers[5] * $numbers[4];
	$answer[2] = $answer[1] - ($numbers[6] * $numbers[2]);
	$answer['x'] = $answer[2] / $answer['b'];
	$answer['x'] = round($answer['x'], 10);
	
	$answer[3] = $numbers[1] * $numbers[6];
	$answer[4] =  $answer[3] - ($numbers[3] * $numbers[5]);
	$answer['y'] = $answer[4] / $answer['b'];
	$answer['y'] = round($answer['y'], 10);
		
	echo "X = $answer[x]<br>
		  Y = $answer[y]";
}
?>
<form method=POST action="z_calc.php" name="calculator">
 <table>
  <tr>
    <td>A:</td>
    <td>
	<input type="text" name="a">
	</td>
    <td>B:</td>
    <td>
	<input type="text" name="b">
	</td>
	<td rowspan=2>=<br>=</td>
	<td>E:</td>
	<td>
	<input type="text" name="e">
	</td>
  </tr>
  <tr>
   <td height="26">C:</td>
	<td>
	<input type="text" name="c">
	</td>
    <td>D:</td>
	<td>
	<input type="text" name="d">
	</td>
	<td>F:</td>
	<td>
	<input type="text" name="f">
	</td>
  </tr>
  <tr>
    <td colspan=7 align="center">
	<input type="submit" name="submit" value="Submit">
	<input type="reset" value="Reset">
	</td>
  </tr>
</table>
</form>
<?
if(isset($_POST['submit'])){
	calc($_POST['a'], $_POST['b'], $_POST['c'], $_POST['d'], $_POST['e'], $_POST['f']);
}
?>

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Craig

Comment:



Comment:

(Maximum characters: 1200). You have characters left.