本帖最后由 白云苍狗 于 2017-1-17 16:48 编辑
<html>
<head>
<title>计算器</title>
</head>
<body>
<table border="0" cellspacing="" cellpadding="" align="center">
<form action="" method="post">
<h1 align="center">计算器</h1>
<td>
<input type="text" id="" value="<?php echo $_POST['text1']?>" name="text1"/>
</td>
<td>
<select name="suanshu">
<option value="+" <?php if($_POST['suanshu']=="+"){echo "selected";}?>>+</option>
<option value="-" <?php if($_POST['suanshu']=="-"){echo "selected";}?>>-</option>
<option value="*" <?php if($_POST['suanshu']=="*"){echo "selected";}?>>*</option>
<option value="/" <?php if($_POST['suanshu']=="/"){echo "selected";}?>>/</option>
<option value="%" <?php if($_POST['suanshu']=="%"){echo "selected";}?>>%</option>
</select></td>
<td>
<input type="text" id="" value="<?php echo $_POST['text2']?>" name="text2" />
</td>
<td>
<input type="submit" value="计算"/>
</td>
<td>
<!--计算结果:<?php echo $jieguo; ?>-->
</td>
</form>
</table>
</body>
</html>
<?php
$text1=$_POST['text1'];//输入框1
$suanshu=$_POST['suanshu'];//算法
$text2=$_POST['text2'];//输入框2
if(empty($text1)){
echo "请输入第一个框的数字";
}elseif(empty($text2)){
echo "请输入第二个框的数字";
}else{
switch ($suanshu) {
case '+':
$jieguo=$text1+$text2;
break;
case '-':
$jieguo=$text1-$text2;
break;
case '*':
$jieguo=$text1*$text2;
break;
case '/':
$jieguo=$text1/$text2;
break;
case '/':
$jieguo=$text1%$text2;
break;
}
echo "结果为:".$jieguo;
}
//对计算结果进行判断
//试试函数
|
|