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>
时间: 2024-08-08 18:13:58

php版的求表达式的真值表-TrueValueTable的相关文章

用递归下降分析求表达式的值

<数据结构>中表达式求值的经典算法是用两个栈,一个存数字,一个存运算符.依次读入表达式中的每个字符,若是数字则进数字栈,若是运算符则和运算符栈的栈顶运算符比较优先权作相应操作,直至整个表达式求值完毕.运算符的优先级表如下   + - * / ( ) # + > > < < < > > - > > < < < > > * > > > > < > > / > >

求表达式的值--栈和队列的应用

1 #include<stdio.h> 2 #include<stdlib.h> 3 4 #define OK 1 5 #define ERROR 0 6 #define STACK_SIZE 20 7 #define STACK_INCREMENT 10 8 #define QUEUE_SIZE 20 9 10 typedef int Status; 11 12 typedef char StackElemtype; 13 typedef struct Stack{ 14 Sta

UVA 10700-Camel trading(栈求表达式的最大最小值)

Camel trading Time Limit: 1 second Background Aroud 800 A.D., El Mamum, Calif of Baghdad was presented the formula 1+2*3*4+5, which had its origin in the financial accounts of a camel transaction. The formula lacked parenthesis and was ambiguous. So,

谁能帮帮我-栈求表达式的值

求表达式的值 下面的代码有问题,带括号的算不了,哪位大佬能帮我修复一下:我崩溃了.... #include<stdio.h> #include<stdio.h> #include <stdlib.h> typedef struct { int num[100]; int top; }Stack1; typedef struct { char num[100]; int top; }Stack2; void Init_stack1(Stack1 *s) { s->t

(算法)求表达式(不含括号)的值

题目: 求字符串表达式(不含括号)的值 思路: 数字分离 运算符优先级+-*/ 只需一个字符串栈,保存数字和运算符 代码: #include<iostream> #include<stack> #include<sstream> #include<string> using namespace std; template<class out_T,class in_T> out_T convert(const in_T &t){ strin

(算法)求表达式(含括号)的值

题目: 求字符串表达式的值,如"-2+(3+4)-5*6",返回-25. 思路: 分离数字和运算符并考虑运算符的优先级,如(),*,/,+,-等. 两个栈:一个存数字,一个存运算符 代码: #include<iostream> #include<stack> #include<sstream> #include<string> using namespace std; double calc(double a,char op,double

Effective Java 第三版——42.lambda表达式优于匿名类

Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将近8年的时间,但随着Java 6,7,8,甚至9的发布,Java语言发生了深刻的变化. 在这里第一时间翻译成中文版.供大家学习分享之用. 42.lambda表达式优于匿名类 在Java 8中,添加了函数式接口,lambda表达式和方法引用,以便更容易地创建函数对象. Stream API随着其他语言的修改一同

触手不及(巴科斯范式求表达式树)

本题为学军神犇 cxt 出的神题. 题意 为了避免流露出自己的感情伤害别人, 小 M.M.T. 决定通过一个表达式来传递心意. 给出一个等式. 等式左边是一个 \(int\) 范围内的数, 等式右边是一个合法的 c++ 表达式. 例如:\(233 = 66 ? 4 ? 31\) 保证等式右边只包含数字 \(x (x ∈ [0, p),p\) 是给定的质数\()\), 加号, 减号, 乘号, 除号, 左右括号. 保证等式中没有任何空格,tab 等不可见字符. 而且保证合法. 但是遗憾的是, 因为一

求表达式的值

1.测试代码 public class MyClass { public static void Run() { KExpression exp = new KExpression(); string backExp = exp.GetRPN("1+3*(4+6/2)"); string value = exp.GetValue(backExp); } } 2.把计算公式转后缀表达式,去掉其中的括号 /// <summary> /// 把中缀表达式转为后缀表达式 /// &