/*
* 简单计算器
*/
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>