Home >Backend Development >PHP Tutorial >PHP version of truth table for expressions-TrueValueTable, truth table logical expression_PHP tutorial
php
error_reporting(E_ALL & ~E_NOTICE);
$expression=$_GET['TrueTable']; //Read input box data
if(!empty($_GET['TrueTable'])){
$expression=strtoupper($expression); //Replace the entered lowercase letters with uppercase letters Letter
$expressionArr=str_split($expression); //Convert $expression into array
foreach($expressionArr as $v){ //Put letters in $varTotal in array
if($v>='A' && $v<='Z'){
$varTotal[]=$v;
}
}
$varTotal = array_unique($varTotal); //Remove repeated letters in the $varTotal array
sort($varTotal); //Forward sorting of letters in the $varTotal array
$varTotal1 = preg_replace("/([A-Z])/","$\1",$varTotal); //The letters in the $varTotal1 array become variables
$maxN = count($varTotal);
//$expressionArr = preg_replace("/([A-Z])/","$\1",$expressionArr); //The letters in the $expressionArr array become variables
$newExpression=implode('',$expressionArr); //$expressionArr array becomes a string
//echo $newExpression." [Explanation: disjunction (|), conjunction (&), non-(!), implication (->), equivalence (=) ]
";
$newExpression= preg_replace("/([A-Z])->([A-Z])/","!\1|\2",$newExpression);
$newExpression= preg_replace("/((.*?))->([A-Z])/","!\1|\2",$newExpression);
$newExpression= preg_replace("/((.*?))->(.*?)/","!\1|\2",$newExpression);
$newExpression= preg_replace("/([A-Z])->(.*?)/","!\1|\2", $newExpression);
$expressionArr=str_split($newExpression); //Convert $expression into array
$expressionArr = preg_replace("/([A-Z])/","$\1",$expressionArr);
$newExpression=implode('',$expressionArr);
//echo $newExpression;
}
?>
php
echo "
";
if(!empty($_GET['TrueTable'])){
foreach($varTotal as $v){
echo " {$v} ";
}echo "结果 ";
$result=pow(2,$maxN);
for($i=0;$i<$result;$i++){
$resultN[$i]=sprintf("%0{$maxN}d",decbin($i));
$resultN[$i]=str_split($resultN[$i]);
}
foreach($resultN as $k=>$v){
echo "";
foreach($resultN[$k] as $k1=>$v1){
echo " ";
}
//print_r($varTotal3);
}
?>
".$v1." ";
$varTotal2[$k1]=$varTotal1[$k1]."=".$v1;
//echo $varTotal2[$k1]." ";
eval($varTotal2[$k1].";");
}
//echo $newExpression;
eval("$varTotal3[] = ".$newExpression.";");
foreach($varTotal3 as $k2=>$v){
if($v==NULL){
$varTotal3[$k2]=0;
}
}
echo "".$varTotal3[$k]." ";
echo "