PHP计算器代码
2014-04-23 11:00:52 -0400
- 代码: 全选
-
<?php
/**
* Author: Crazy
* http://code.zhangyun.org
*/
header("Content-Type: text/html; charset=UTF-8");
$submit = isset($_POST['submit']) ? true : false;
if ($submit) {
$NumA = $_POST['numa'];
$NumB = $_POST['numb'];
$symbol = $_POST['symbol'];
if (!is_numeric($NumA) || !is_numeric($NumB))
echo "<script>alert('请输入正确的数字');</script>";
// 如果是十六进制呢?
// 要不要考虑计算范围?
switch ($symbol) {
case '+':
$result = $NumA + $NumB;
break;
case '-':
$result = $NumA - $NumB;
break;
case '*':
$result = $NumA * $NumB;
break;
case '/':
$result = $NumA / $NumB;
break;
default:
echo "<script>alert('错误的运算符');</script>";
break;
}
echo "<script>alert('$result');</script>";
}
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>计算器</title>
</head>
<body>
<form action="" method="post">
<input type="text" name="numa" value="" />
<select name="symbol">
<option value="+">加</option>
<option value="-">减</option>
<option value="*">乘</option>
<option value="/">除</option>
</select>
<input type="text" name="numb" value="" />
<input type="submit" name="submit" value="计算" />
</form>
</body>
</html>
Back to home
Subscribe |
Register |
Login
| N