php版的求表达式的真值表-TrueValueTable

技术分享

贴上代码:

<?php
    error_reporting(E_ALL & ~E_NOTICE);    
    $expression=$_GET[‘TrueTable‘];    //读取输入框数据
    if(!empty($_GET[‘TrueTable‘])){
        $expression=strtoupper($expression);   //将输入的小写字母,替代成大写字母
        $expressionArr=str_split($expression); //将$expression转换成数组
        foreach($expressionArr as $v){         //把字母放在 $varTotal数组中
            if($v>=‘A‘ && $v<=‘Z‘){
                $varTotal[]=$v;
            }
        }
        $varTotal = array_unique($varTotal);  //去掉$varTotal数组中重复的字母
        sort($varTotal);                      //$varTotal数组中字母正向排序
        $varTotal1 = preg_replace("/([A-Z])/","$\\1",$varTotal);  //$varTotal1数组中字母变成变量
        $maxN = count($varTotal);
        //$expressionArr = preg_replace("/([A-Z])/","$\\1",$expressionArr); //$expressionArr数组中字母变成变量
        $newExpression=implode(‘‘,$expressionArr); //$expressionArr数组变成字符串
        //echo $newExpression."<br />";
        $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); //将$expression转换成数组
        $expressionArr = preg_replace("/([A-Z])/","$\\1",$expressionArr);
        $newExpression=implode(‘‘,$expressionArr);
        //echo $newExpression;
        
    }
?>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>真值表查询1.0    BY:小臻</title>
    </head>
    <body>
    <span style="color:red;text-align:center;"><h1>真值表查询1.0    BY:小臻</h1></span>
    <div style="width:500px;margin:30px auto 0 auto;text-align:center;">
        
        <form action="" method="get">
            <input type="text" name="TrueTable" value="<?php echo $_GET[‘TrueTable‘] ?>"/>
            <input type="submit" value="确定"/>    <br />    <br />
            <!--<input type="reset" value="取消" /><br /><br />
            <input type="button" value="∨" />
            <input type="button" value="∧" />
            <input type="button" value="┐" />
            <input type="button" value="→" />
            <input type="button" value="↔" /><br /><br />-->
        </form>
        <!--[ 说明:析取(∨) , 合取(∧) , 非(┐) , 蕴含(→) , 等价(↔) ]-->
        <p>[ 说明:析取(|) , 合取(&) , 非(!) , 蕴含(->) , 等价(=) ]</p>
    
        <table width="300px" style="margin:30px auto 0 auto;text-align:center;border:1px solid #000;">
                <?php
                    echo "<tr>";
                    if(!empty($_GET[‘TrueTable‘])){
                        foreach($varTotal as $v){
                            echo "<td>{$v}</td>";
                        }                    
                        echo "<td>结果</td>";
                        $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 "<tr>";
                            foreach($resultN[$k] as $k1=>$v1){
                                echo "<td>".$v1."</td>";
                                $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 "<td>".$varTotal3[$k]."</td>";
                            echo "</tr>";            
                        }
                        //print_r($varTotal3);
                    }
                
                ?>        
        </table>
    </div>
    </body>
</html>

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。