/* * 简单计算器 */ error_reporting(E_ALL & ~E_NOTICE); if(isset($_POST['sub'])){ $a = $_POST['num1']; $b = $_POST['num2']; $var = $_POST['operator']; $flag = true; $errormess = "存在问题如下:<br/>"; if($a == ""){ $flag = false; $errormess .= "第一个数不能为空<br/>"; }elseif(!is_numeric($a)){ $flag = false; $errormess .= "第一个数字必须是数字<br/>"; } if($b == ""){ $flag = false; $errormess .= "第二个数不能为空<br/>"; }elseif(!is_numeric($b)){ $flag = false; $errormess .= "第二个数字必须是数字<br/>"; } if($flag){ $sum=''; switch($var){ case '+': $sum=$a+$b; break; case '-': $sum=$a-$b; break; case '*': $sum=$a*$b; break; case '/': $sum=$a/$b; break; case '%': $sum=$a%$b; break; } } } ?> <html> <head> <title>简单计算器</title> </head> <body> <div> <form action="index.php" method="post"> <input type="text" name="num1" value="<?php echo $_POST['num1']?>" /> <select name="operator"> <option <?php echo $_POST['operator']=='+' ? "selected" : "" ?> value="+" >+</option> <option <?php echo $_POST['operator']=='-' ? "selected" : "" ; ?> value="-" >-</option> <option <?php echo $_POST['operator']=='*' ? "selected" : "" ; ?> value="*" >*</option> <option <?php echo $_POST['operator']=='/' ? "selected" : "" ; ?> value="/" >/</option> <option <?php echo $_POST['operator']=='%' ? "selected" : "" ; ?> value="%" >%</option> </select> <input type="text" name="num2" value="<?php echo $_POST['num2']?>" /> <input type="submit" name="sub" value="submit"> </form> </div> <h5>计算结果</h5> <?php if($flag){ echo $_POST['num1'].$_POST['operator'].$_POST['num2']."=".$sum; }else{ echo $errormess; } ?> </body> </html>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。