<html>
<head>
<title>简单计算器</title>
</head>
<body>
<?php
if(isset({1}POST["num1"])&&isset({1}POST["num2"])){
if(empty({1}POST["num1"])){
echo"<font color='red'>第一个操作数不可以为空</font><br>";
unset({1}POST["sub"]);
}
if(empty({1}POST["num2"])){
echo"<font color='red'>第二个操作数不可以为空</font><br>";
unset({1}POST["sub"]);
}
$oper={1}POST["oper"];
$num1={1}POST["num1"];
$num2={1}POST["num2"];
if($oper=="/"||$oper=="%"){
if($num2==0){
echo"<font color='red'>不可以作为除数</font><br>";
unset({1}POST["sub"]);
}
}
$sum=0;
switch ($oper){
case"+":
$sum=$num1+$num2;
break;
case"-":
$sum=$num1-$num2;
break;
case"*":
$sum=$num1*$num2;
break;
case"/":
$sum=$num1/$num2;
break;
case"%":
$sum=$num1+$num2;
break;
}
echo"<tr><td colspan='4' align='center'>";
echo"计算结果:$num1 $oper $num2=$sum";
echo"</td></tr>";
}
?>
<table border="1" align="center" width="400">
<form action="" method="post">
<caption><h2>简单计算器</h2></caption>
<tr>
<td>
<input type="text" size="10" name="num1" value= "<?php if(!empty($num1))echo$num1;?>">
</td>
<td>
<select name="oper">
<option value="+" >+</option>
<option value="-" >-</option>
<option value="*" >*</option>
<option value="/" >/</option>
<option value="%" >%</option>
</select>
</td>
<td>
<input type="text" size="10" name="num2" value="<?php if(!empty($num2))echo$num2?>">
</td>
<td>
<input type="submit" name="sub" value="计算">
</td>
</tr>
<?php
/*if(isset({1}POST["sub"])&&empty({1}POST["sub"])){
$sum=0;
switch ($oper){
case"+":
$sum=$num1+$num2;
break;
case"-":
$sum=$num1-$num2;
break;
case"*":
$sum=$num1*$num2;
break;
case"/":
$sum=$num1/$num2;
break;
case"%":
$sum=$num1+$num2;
break;
}
echo"<tr><td colspan='4' align='center'>";
echo"计算结果:$num1 $oper $num2=$sum";
echo"</td></tr>";
}
*/?>
</form>
</table>
</body>
</html>