Heim  >  Artikel  >  Backend-Entwicklung  >  PHP-Version der Wahrheitstabelle für Ausdrücke – TrueValueTable

PHP-Version der Wahrheitstabelle für Ausdrücke – TrueValueTable

WBOY
WBOYOriginal
2016-08-08 09:28:551084Durchsuche

贴上代码:

<?<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教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn