PHP 简单计算器代码实现
/**简单计算器*/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><formaction="index.php"method="post"><inputtype="text"name="num1"value="<?phpecho$_POST['num1']?>"/><selectname="operator"><option<?phpecho$_POST['operator']=='+'?"selected":""?>value="+">+</option><option<?phpecho$_POST['operator']=='-'?"selected":"";?>value="-">-</option><option<?phpecho$_POST['operator']=='*'?"selected":"";?>value="*">*</option><option<?phpecho$_POST['operator']=='/'?"selected":"";?>value="/">/</option><option<?phpecho$_POST['operator']=='%'?"selected":"";?>value="%">%</option></select><inputtype="text"name="num2"value="<?phpecho$_POST['num2']?>"/><inputtype="submit"name="sub"value="submit"></form></div><h5>计算结果</h5><?phpif($flag){echo$_POST['num1'].$_POST['operator'].$_POST['num2']."=".$sum;}else{echo$errormess;}?></body></html>
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。