<html>
<head>
<title>PHP实现简单计算器</title>
<meta http-equiv=”content-type” content=”text/html;chaset=utf-8″>
</head>
<?php
error_reporting(0);
$num1=$_POST['num1'];
$num2=$_POST['num2'];
if(!$_POST['sub'])
{
$num=’计算器已经准备好了,请大人输入数字~!’;
}
else{
if($num1==”&&$num2==”)
{
$num=’亲,木有数字,请填写你要计算的数字!’;
}
elseif($num1==”)
{
$num=’请填写第一个数字~’;
}
elseif($num2==”)
{
$num=’请填写第二个数字’;
}
elseif(!is_numeric($num1)||!is_numeric($num2))
{
$num=’你写数字会死啊~!’;
}
else{
switch($_POST['ysf']){
case 1:$do=$num1+$num2;
$num=$num1.’+’.$num2 .’ = ‘.$do;
break;
case 2:$do=$num1-$num2;
$num=$num1.’-’.$num2 .’ = ‘.$do;
break;
case 3:$do=$num1*$num2;
$num=$num1.’×’.$num2 .’ = ‘.$do;
break;
case 4:if($num2==’0′){
$num=’除数不能为0啦!’;
}
else{
$do=$num1/$num2;
$num=$num1.’÷’.$num2 .’ = ‘.$do;
}
break;
case 5:$do=$num1%$num2;
$num=$num1.’%’.$num2 .’ = ‘.$do;
break;
}
}
}
?>
<body>
<table align=”center” border=”1″ width=”500″>
<caption><h1>简单计算器</h1></caption>
<form action=”jsq.php” method=”post”>
<tr>
<td width=”20″>
<input type=”text” size=”20″ name=”num1″ maxlength=”20″ value=”<?php echo $_POST['num1'];?>”>
</td>
<td width=”5″>
<select name=”ysf”>
<option value=”1″ <?php if($_POST['ysf']==’1′){ echo ‘selected’;}?>>+</option>
<option value=”2″ <?php if($_POST['ysf']==’2′){ echo ‘selected’;}?>>-</option>
<option value=”3″ <?php if($_POST['ysf']==’3′){ echo ‘selected’;}?>>×</option>
<option value=”4″ <?php if($_POST['ysf']==’4′){ echo ‘selected’;}?>>÷</option>
<option value=”5″ <?php if($_POST['ysf']==’5′){ echo ‘selected’;}?>>%</option>
</select>
</td>
<td width=”20″>
<input type=”text” size=”20″ name=”num2″ maxlength=”20″ value=”<?php echo $_POST['num2'];?>”>
</td>
<td width=”20″>
<input type=”submit” size=”20″ name=”sub” value=”计算”>
</td>
</tr>
<tr>
<td colspan=”3″>
计算结果:<?php echo $num; ?>
</td>
<td>
<a href=”jsq.php”>刷新页面</a>
</td>
</tr>
</form>
</table>
</body>
</html>
PHP实现简单计算器
最新推荐文章于 2024-09-22 10:52:57 发布