首页 > 代码库 > 计算器

计算器

<?php    error_reporting(E_ALL & ~E_NOTICE);    if( isset($_POST[‘sub‘])){            $bz = true;        $errormess = "有以上问题<br>";                if($_POST[‘num1‘] == ""){            $bz = false;            $errormess = "第一个数不能为空<br>";        }else{                if(!is_numeric($_POST[‘num1‘])){                $bz = false;                $errormess .= "不是数字不能计算<br>";            }        }        if($_POST[‘num2‘] == ""){            $bz = false;            $errormess = "第二个数不能为空<br>";        }else{                if(!is_numeric($_POST[‘num2‘])){                $bz = false;                $errormess .= "不是数字不能计算<br>";            }        }                        if($bz){            $sum = "";            switch($_POST[‘ysf‘]){                case ‘+‘ :$sum = $_POST[‘num1‘] + $_POST[‘num2‘];break;                case ‘-‘ :$sum = $_POST[‘num1‘] - $_POST[‘num2‘];break;                case ‘*‘ :$sum = $_POST[‘num1‘] * $_POST[‘num2‘];break;                case ‘/‘ :$sum = $_POST[‘num1‘] / $_POST[‘num2‘];break;                case ‘%‘ :$sum = $_POST[‘num1‘] % $_POST[‘num2‘];break;            }                }            }    echo ‘<br>‘;?><html>    <head>        <title>计算器</title>    </head>        <body>        <table border="0" width="200" align="center">            <form action="jsq.php" method="post">                <caption><h1>简单计算器</h1></caption>                <tr>                    <td>                        <input type="text" size="10" name="num1" value="http://www.mamicode.com/<?php echo $_POST[‘num1‘]?>"><br>                    </td>                    <td>                        <select name="ysf">                            <option <?php echo $_POST[‘ysf‘] == "+" ? "selected" : ""?> value="http://www.mamicode.com/+">+</option>                            <option <?php if($_POST[‘ysf‘] == "-") echo "selected"?> value="http://www.mamicode.com/-">-</option>                            <option <?php if($_POST[‘ysf‘] == "*") echo "selected"?> value="http://www.mamicode.com/*">*</option>                            <option <?php if($_POST[‘ysf‘] == "/") echo "selected"?> value="http://www.mamicode.com/">/</option>                            <option <?php if($_POST[‘ysf‘] == "%") echo "selected"?> value="http://www.mamicode.com/%">%</option>                        </select>                    </td>                    <td>                        <input type="text" size="8" name="num2" value="http://www.mamicode.com/<?php echo $_POST[‘num2‘]?>"><br>                    </td>                    <td>                        <input type="submit" name="sub" value="http://www.mamicode.com/计算">                    </td>                </tr>                                <tr >                    <td colspan="4">                        <?php                            if($bz){                            echo "计算结果:{$_POST[‘num1‘]} {$_POST[‘ysf‘]} {$_POST[‘num2‘]} = {$sum}";                                            }else{                                echo "{$errormess}";                            }                        ?>                    </td>                </tr>            </form>        </table>    </body></html>

 

计算器