>백엔드 개발 >PHP 튜토리얼 >표현식에 대한 진리표의 PHP 버전-TrueValueTable

표현식에 대한 진리표의 PHP 버전-TrueValueTable

WBOY
WBOY원래의
2016-08-08 09:28:551105검색

贴上代码:

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

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>真值表查询1.0    BY:小臻</title>
    </head>
    <body>
    <span><h1>真值表查询1.0    BY:小臻</h1></span>
    <div>
        
        <form action="" method="get">
            <input type="text" name="TrueTable" value="<?php echo <span>$_GET</span>['TrueTable'] ?>"/>
            <input type="submit" value="确定"/>    <br />    <br />
            <!--<input type="reset" value="取消" /><br /><br />
            <input type="button" value="&or;" />
            <input type="button" value="&and;" />
            <input type="button" value="┐" />
            <input type="button" value="&rarr;" />
            <input type="button" value="&harr;" /><br /><br />-->
        </form>
        <!--[ 说明:析取(&or;) , 合取(&and;) , 非(┐) , 蕴含(&rarr;) , 等价(&harr;) ]-->
        <p>[ 说明:析取(|) , 合取(&) , 非(!) , 蕴含(->) , 等价(=) ]</p>
    
        <table width="300px">
                <?<span>php
                    </span><span>echo</span> "<tr>"<span>;
                    </span><span>if</span>(!<span>empty</span>(<span>$_GET</span>['TrueTable'<span>])){
                        </span><span>foreach</span>(<span>$varTotal</span> <span>as</span> <span>$v</span><span>){
                            </span><span>echo</span> "<td>{<span>$v</span>}</td>"<span>;
                        }                    
                        </span><span>echo</span> "<td>结果</td>"<span>;
                        </span><span>$result</span>=<span>pow</span>(2,<span>$maxN</span><span>);

                        </span><span>for</span>(<span>$i</span>=0;<span>$i</span><<span>$result</span>;<span>$i</span>++<span>){    
                            </span><span>$resultN</span>[<span>$i</span>]=<span>sprintf</span>("%0{<span>$maxN</span>}d",<span>decbin</span>(<span>$i</span><span>));
                            </span><span>$resultN</span>[<span>$i</span>]=<span>str_split</span>(<span>$resultN</span>[<span>$i</span><span>]);
                        }
                        
                        </span><span>foreach</span>(<span>$resultN</span> <span>as</span> <span>$k</span>=><span>$v</span><span>){
                            </span><span>echo</span> "<tr>"<span>;
                            </span><span>foreach</span>(<span>$resultN</span>[<span>$k</span>] <span>as</span> <span>$k1</span>=><span>$v1</span><span>){
                                </span><span>echo</span> "<td>".<span>$v1</span>."</td>"<span>;
                                </span><span>$varTotal2</span>[<span>$k1</span>]=<span>$varTotal1</span>[<span>$k1</span>]."=".<span>$v1</span><span>;
                                </span><span>//</span><span>echo $varTotal2[$k1]." ";</span>
                                <span>eval</span>(<span>$varTotal2</span>[<span>$k1</span>].";"<span>);                                                            
                            }
                            
                            </span><span>//</span><span>echo $newExpression;</span>
                            <span>eval</span>("\$varTotal3[] = ".<span>$newExpression</span>.";"<span>);
                            </span><span>foreach</span>(<span>$varTotal3</span> <span>as</span> <span>$k2</span>=><span>$v</span><span>){
                                </span><span>if</span>(<span>$v</span>==<span>NULL</span><span>){
                                    </span><span>$varTotal3</span>[<span>$k2</span>]=0<span>;
                                }
                            
                            }
                            </span><span>echo</span> "<td>".<span>$varTotal3</span>[<span>$k</span>]."</td>"<span>;
                            </span><span>echo</span> "</tr>"<span>;            
                        }
                        </span><span>//</span><span>print_r($varTotal3);</span>
<span>                    }
                
                </span>?>        
        </table>
    </div>
    </body>
</html>

以上就介绍了php版的求表达式的真值表-TrueValueTable,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.