Blogger Information
Blog 16
fans 0
comment 0
visits 11232
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
学习的计算器
逃逃
Original
363 people have browsed it

<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title>计算器</title>

</head>

<body >

    <h2>实现一个计算器</h2>

    <table>

        <form action="" method="post">

            <tr>

                <td>请输入第一个数 <input type="number" name="num1" required value = "<?=$_POST['num1']?>"/><td>

                <td>

                    <select name="opt">

                        <option value="+" <?=$_POST["opt"] == "+" ? "selected" : "" ?>>+</option>

                        <option value="-" <?=$_POST["opt"] == "-" ? "selected" : "" ?>>-</option>

                        <option value="*" <?=$_POST["opt"] == "*" ? "selected" : "" ?>>*</option>

                        <option value="/" <?=$_POST["opt"] == "/" ? "selected" : "" ?>>/</option>

                        <option value="%" <?=$_POST["opt"] == "%" ? "selected" : "" ?>>%</option>

                    </select>

                </td>

                <td>请输入第二个数 <input type="number" name="num2" required value = "<?=$_POST['num2']?>"/></td>

                <td>

                    <input type="submit" name="sub" value="计算">

                </td>

            <tr>

        </form>

        <?php

            if(isset($_POST['sub']))://判断用户是否点击了计算按钮:

                if($_POST['opt'] == '/' && $_POST['num2'] == 0 || $_POST['opt'] == '%' && $_POST['num2'] == 0):

                    echo $mess = "<h3 style='color:red;'>除数不能为0</h3>";

                endif;

            endif;

        ?>

        <?php

            error_reporting(E_ALL & ~E_NOTICE); //屏蔽 NOTICE 的错误

            

            $num1 = isset($_POST['num1']) ? $_POST['num1'] :0; //三元运算符 判断是否获得num1的值 默认为0

            $num2 = isset($_POST['num2']) ? $_POST['num2'] :0; //三元运算符 判断是否获得num2的值 默认为0

            $opt = isset($_POST['opt']) ? $_POST['opt'] : '+'; //三元运算符 判断是否获得运算符的值 默认为加法

            $mess = isset($_POST['mess']) ? $_POST['mess'] : ''; //三元运算符 判断是否获得错误提示

            if(!$mess && isset($_POST['sub']) ):

            

                switch($_POST['opt']):

                    case '+':

                        $result = (int)$_POST['num1'] + (int)$_POST['num2']; break;

                    case '-':

                        $result = (int)$_POST['num1'] - (int)$_POST['num2']; break;

                    case '*':

                        $result = (int)$_POST['num1'] * (int)$_POST['num2']; break; 

                    case "/":

                        $result = (int)$_POST['num1'] / (int)$_POST['num2']; break;

                    case "%":

                        $result = (int)$_POST['num1'] % (int)$_POST['num2']; break; 

                endswitch;

                $res = "计算结果: {$_POST['num1']} {$_POST["opt"]} {$_POST['num2']} = {$result}";

                echo "<h3 style='color:green;'>{$res}</h3>";

            else:

                echo $mess;

            endif;

        ?>

    </table>


    <hr>

    <h2>筛选奇数组成新的数组并返回</h2>

    <p>99,72,35,18,7,54,39</p>

    <?php

        $arr = [99,72,35,18,7,54,39];


        function odd(array $arr):array  //odd是数组

        {

            $newArr = []; //声明一个新的空数组

            for ($i=0; $i < count($arr) ; $i++) { 

                if($arr[$i] % 2 != 0)//加上!就是奇数,不加是偶数==???

                {

                    array_push($newArr,$arr[$i]);

                }

            }


            return $newArr; //返回新数组

        }

        var_dump(odd($arr)); //打印输出

    ?>

</body>

</html>


Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post