html
<html>
<head>
    <meta charset="UTF-8">
    <title>简单计算器</title>
</head>
<body>
<h1>简单计算器</h1>
    <form action="10.php" method="post">
        <input type="text" name="v1" id="">
        <select name="op" id="">
            <option value="add">+</option>
            <option value="reduce">-</option>
            <option value="multi">*</option>
            <option value="div">/</option>
        </select>
        <input type="text" name="v2" id="">
        <button type="submit">结果</button>
    </form>
</body>
</html>
php
<?php header("Content-type:text/html;charset=utf-8"); interface math{ function cal($v1, $v2); } class mathadd implements math { public function cal($v1, $v2){ return $v1 + $v2; } } class mathreduce implements math { public function cal($v1, $v2){ return $v1 - $v2; } } class mathmulti implements math { public function cal($v1, $v2){ return $v1 * $v2; } } class mathdiv implements math { public function cal($v1, $v2){ return $v1 / $v2; } } /** * */ class Cmath { protected $type; protected $calc = null; function __construct($type) { $this->type = $type; $cal = ‘math‘.$type; $this->calc = new $cal(); } public function cal($v1, $v2){ return $this->calc->cal($v1, $v2); } } $op = $_POST[‘op‘]; $cal = new Cmath($op); echo $cal->cal($_POST[‘v1‘], $_POST[‘v2‘]);
原文:https://www.cnblogs.com/Mishell/p/12182415.html