Created
January 10, 2010 11:00
-
-
Save erikbgithub/273436 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* the normally used way to add different types in a function | |
* | |
* @param $nr1 - the first summand | |
* @param $nr2 - the second summand | |
* @param $type - the number type. default='decimal' | |
*/ | |
function add_classical($nr1, $nr2, $type='decimal'){ | |
$sum = NULL; | |
switch($type){ | |
case 'decimal': | |
$sum = (int)$nr1 + (int)$nr2; | |
case 'binary': | |
$sum = bindec($nr1) + bindec($nr2); | |
} | |
return $sum; | |
} | |
/** | |
* adds 2 numbers using the finite-state machine | |
* | |
* @param string $nr1 - the first summand | |
* @param string $nr2 - the second summand | |
* @param string $type - the number type. default='decimal' | |
*/ | |
function add_ddp($nr1, $nr2,$type="decimal"){ | |
//put it in a machine readable format | |
$list = chain($nr1, $nr2); | |
//let the machine work | |
return configure('wo',$list,$type); | |
} | |
/** | |
* creates the fs-machines action beginning with a configuration. | |
* will echo all configurations happening during its work. | |
* works recursively, so don't overdo it with this function. | |
* | |
* @param string $state - 'w' or 'wo' | |
* @param string $list - the list of entry signals | |
* @return resultstring | |
*/ | |
function configure($state, $list,$type="decimal"){ | |
echo "<br>//configure($state,".print_r($list,1).")"; | |
if(count($list) <= 0){ echo "=>".print_r(array(),1);return '';} | |
$i = count($list)-1; | |
$c1 = $list[$i][0]; | |
$c2 = $list[$i][1]; | |
$res = query($c1, $c2, $state,$type); | |
unset($list[$i]); | |
reset($list); | |
echo "=>".print_r($res,1); | |
return configure($res['nextstate'],$list,$type).$res['output']; | |
} | |
/** | |
* takes two numbers as strings and forms a list of strings in the format | |
* accepted by the fs-machine | |
* example: chain('123','456')=>['14','25','36'] | |
* | |
* @param string nr1 - the first number as string | |
* @param string nr2 - the second number as string | |
* @return array | |
*/ | |
function chain($nr1, $nr2){ | |
$list = array(); | |
for($y=0; $y < strlen($nr2); $y++){ | |
$list[] = $nr1[$y].$nr2[$y]; | |
} | |
return $list; | |
} | |
/** | |
* querys state and output for 2 numbers and a state | |
* | |
* @param string $c1 - the first number | |
* @param string $c2 - the second number | |
* @param string $state - 'w' or 'wo' depending on the result of the last query, | |
default is 'wo' because most fs-machines begin in this state | |
* @param string $type - number type that should be worked on. default='decimal' | |
* @return array | |
*/ | |
function query($c1, $c2, $state="wo", $type="decimal"){ | |
// security measures | |
$c1 = mysql_real_escape_string($c1); | |
$c2 = mysql_real_escape_string($c2); | |
$state = mysql_real_escape_string($state); | |
$type = mysql_real_escape_string($type); | |
// database request | |
$db = mysql_pconnect("localhost","root","root"); | |
mysql_select_db("ddpadd", $db); | |
$sql = "SELECT output_$state, nextstate_$state FROM ".$type."_add\n"; | |
$sql .= "WHERE no1='$c1' AND no2='$c2'"; | |
$result = mysql_query($sql, $db); | |
if(!$result) throw new Exception(mysql_error($db)); | |
$retval = array(); | |
while($row = mysql_fetch_array($result)){ | |
$retval[] = array("output"=>$row[0],"nextstate"=>$row[1]); | |
} | |
return $retval[0]; | |
} | |
echo "<br>classical 123 + 321 = ".add_classical('123','321'); | |
echo "<br>finite state machine 123 + 321 = ".add_ddp('123','321'); | |
echo "<br>add 2 binarys wih finite state machine 0101 + 0110 = ".add_ddp('0101', '0110','binary'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment