Correcting teacher:灭绝师太
Correction status:qualified
Teacher's comments:计算器第二种解决办法:可以给select一个change事件,运算符被选择后,再点击的数就往右边的输入框追加
1. 给定一个数组$arr = [23,3,45,6,78,8,34],筛选其偶数成员组成新的数组返回,请封装函数。
自己弄没弄好,后来查手册,发现array_filter函数,参照手册后搞定
<?php //检测是否偶数的函数 function even($num) { return !($num % 2); } $arr=[23,3,45,6,78,8,34];//需要检测的数组 print_r(array_filter($arr,'even'));//打印输出偶数数组 ?>
点击 "运行实例" 按钮查看在线实例
2. 尝试实现简单的计算器功能,语言不限制。
简单版:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>计算器</title> <style> .input { height: 25px; } .submit { width: 80px; margin: 5px; padding: 5px; } .result { color: red; font-size: 28px; } </style> </head> <body> <center> <form name="sub" method="post"> <label>计算器</label> <input type="text" class="input" name="num1" value="<?php echo $_POST['num1'] ?>"> <select name="compute" class="input"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> <input name="num2" class="input" value="<?php echo $_POST['num2'] ?>" type="text"> <br> <input name="sub" type="submit" value="计算" class="submit"> </form> <?php $num1 = $_POST['num1']; $num2 = $_POST['num2']; $compute = $_POST['compute']; $result = null; if (isset($_POST["sub"])) { //判断数字1是否为空 if ($num1 == "") { $num1 = false; echo "第一个数不能为空<br>"; } //判断数字1是否为数字 if (!is_numeric($num1)) { $num1 = false; echo "第一个数不是数字<br>"; } //判断运算符是否为空 if ($compute == "") { $compute = false; echo "请选择运算符<br>"; } //判断数字2是否为空 if ($num2 == "") { $num2 = false; echo "第二个数不能为空<br>"; } //判断数字2是否为数字 if (!is_numeric($num2)) { $num2 = false; echo "第二个数不是数字<br>"; } } switch ($compute) { case '+': $result = $num1 + $num2; //加法求和 break; case '-': $result = $num1 - $num2; //减法求差 break; case '*': $result = $num1 * $num2; //乘法求积 break; case '/': $result = $num1 / $num2; //除法求商 break; default: } //输出计算结果 echo '<p class="result">' . $result . '</p>'; ?> </center> </form> </body> </html>
点击 "运行实例" 按钮查看在线实例
使用在input中输入数字计算都正常能实现计算器的功能了,想挑战下自己做带按钮的计算器,前半部分实现了,点击数字第一个输入框以及运算符都能通过按钮输入,第2个输入框搞不定了,思路是点击按钮先赋值到第一个input,当点击运算符后,再点击按钮则是赋值到第2个input,这最后一部往第2个input赋值搞不好了,代码贴出来请老师看看吧。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>计算器</title> <style> .input { height: 25px; } .button { padding: 5px 10px; margin: 5px 2px; } .buttonc { padding: 5px 15px; margin: 5px 2px; } .submit { width: 80px; margin: 5px; padding: 5px; } .result { color: red; font-size: 28px; } </style> </head> <body> <center> <form name="sub" method="post"> <label>计算器</label> <input type="text" class="input" name="num1" value="<?php echo $_POST['num1'] ?>" id="num1"> <select name="compute" class="input" id="compute"> <option value="">运算符</option> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> <input name="num2" class="input" value="<?php echo $_POST['num2'] ?>" type="text" id="num2"> <br> <input type="button" value="1" class="button" onclick="myFunction(this)"> <input type="button" value="2" class="button" onclick="myFunction(this)"> <input type="button" value="3" class="button" onclick="myFunction(this)"> <input type="button" value="4" class="button" onclick="myFunction(this)"> <input type="button" value="5" class="button" onclick="myFunction(this)"> <br> <input type="button" value="6" class="button" onclick="myFunction(this)"> <input type="button" value="7" class="button" onclick="myFunction(this)"> <input type="button" value="8" class="button" onclick="myFunction(this)"> <input type="button" value="9" class="button" onclick="myFunction(this)"> <input type="button" value="0" class="button" onclick="myFunction(this)"> <br> <input type="button" value="+" class="buttonc" onclick="document.getElementById('compute').value=this.value"> <input type="button" value="-" class="buttonc" onclick="document.getElementById('compute').value=this.value"> <input type="button" value="*" class="buttonc" onclick="document.getElementById('compute').value=this.value"> <input type="button" value="/" class="buttonc" onclick="document.getElementById('compute').value=this.value"> <br> <input name="del" type="button" value="C" class="submit" onclick="getElementById('num1').value = null;getElementById('num2').value = null"> <input name="sub" type="submit" value="计算" class="submit"> <script> function myFunction(button) { var x = button.value; document.getElementById("num1").value += x; } </script> </form> <?php $num1 = $_POST['num1']; $num2 = $_POST['num2']; $compute = $_POST['compute']; $result = null; if (isset($_POST["sub"])) { //判断数字1是否为空 if ($num1 == "") { $num1 = false; echo "第一个数不能为空<br>"; } //判断数字1是否为数字 if (!is_numeric($num1)) { $num1 = false; echo "第一个数不是数字<br>"; } //判断运算符是否为空 if ($compute == "") { $compute = false; echo "请选择运算符<br>"; } //判断数字2是否为空 if ($num2 == "") { $num2 = false; echo "第二个数不能为空<br>"; } //判断数字2是否为数字 if (!is_numeric($num2)) { $num2 = false; echo "第二个数不是数字<br>"; } } switch ($compute) { case '+': $result = $num1 + $num2; //加法求和 break; case '-': $result = $num1 - $num2; //减法求差 break; case '*': $result = $num1 * $num2; //乘法求积 break; case '/': $result = $num1 / $num2; //除法求商 break; default: } //输出计算结果 echo '<p class="result">' . $result . '</p>'; ?> </center> </form> </body> </html>
点击 "运行实例" 按钮查看在线实例