数组、计算器

作业标题:0805 PHP编程作业
作业内容:1.给定一个数组$arr = [23,3,45,6,78,8,34],筛选其偶数成员组成新的数组返回,请封装函数。 2. 尝试实现简单的计算器功能,语言不限制。


  • 给定一个数组$arr = [23,3,45,6,78,8,34],筛选其偶数成员组成新的数组返回,请封装函数。
    1. <?php
    2. $arr = [23,3,45,6,78,8,34];
    3. $arr1=[];
    4. function arr($arr,$arr1)
    5. {
    6. for ($i = 0; $i <count($arr);$i++){
    7. //print($arr[$i]);
    8. if(($arr[$i]%2)==0){
    9. array_push($arr1,$arr[$i]);
    10. }
    11. echo "<br>";
    12. }
    13. print_r($arr1);
    14. }
    15. arr($arr,$arr1);
  • 尝试实现简单的计算器功能,语言不限制

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8" />
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    7. <title>Document</title>
    8. <link
    9. href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/css/bootstrap.css"
    10. rel="stylesheet"
    11. />
    12. <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
    13. <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.6.0/js/bootstrap.bundle.js"></script>
    14. <style>
    15. * {
    16. background-color: #d4edda;
    17. text-align: center;
    18. font-size: 20px;
    19. }
    20. .form-control {
    21. width: 500px;
    22. padding: 0.375rem 0.75rem;
    23. font-size: 1rem;
    24. font-weight: 400;
    25. line-height: 1.5;
    26. color: #495057;
    27. background-color: #fff;
    28. background-clip: padding-box;
    29. border: 1px solid #ced4da;
    30. border-radius: 0.25rem;
    31. transition: border-color 0.15s ease-in-out, box-shadow 0.15s ease-in-out;
    32. }
    33. .col-md-2 {
    34. padding-left: 0;
    35. padding-right: 0;
    36. }
    37. button {
    38. width: 242px;
    39. }
    40. </style>
    41. </head>
    42. <body>
    43. <h2 class="title">计算器</h2>
    44. <div class="d-flex h-100">
    45. <div class="m-auto">
    46. <form action="" style="align-content: center" onsubmit="return false;">
    47. <div class="form-group">
    48. <label class="col-md-2 control-label">第一个数</label>
    49. <div class="col-md-6">
    50. <input type="text" class="form-control" id="111" />
    51. </div>
    52. </div>
    53. <div class="form-group">
    54. <label class="col-md-2 control-label">第二个数</label>
    55. <div class="col-md-6">
    56. <input type="text" class="form-control" id="222" />
    57. </div>
    58. </div>
    59. <div class="form-group">
    60. <label class="col-md-3 control-label">选择运算符</label>
    61. <div class="col-md-12">
    62. <select id="123">
    63. <option id="11">+</option>
    64. <option id="12">-</option>
    65. <option id="13">*</option>
    66. <option id="14">/</option>
    67. <option id="15">%</option>
    68. </select>
    69. </div>
    70. </div>
    71. <div class="form-group">
    72. <label class="col-md-2 control-label"></label>
    73. <div class="col-md-12">
    74. <button class="btn btn-primary btn btn-default btn-lg">
    75. 运算结果
    76. </button>
    77. </div>
    78. </div>
    79. <div class="form-group">
    80. <label class="col-md-2 control-label"></label>
    81. <div class="col-md-6">
    82. <input type="text" class="form-control" id="lat" />
    83. </div>
    84. </div>
    85. </form>
    86. </div>
    87. </div>
    88. <div id="msg" style="margin-top: 20px; color: Red; display: none"></div>
    89. </body>
    90. <script>
    91. let a1;
    92. let b1;
    93. $("button").click(function () {
    94. a1 = $("#111").val();
    95. b1 = $("#222").val();
    96. //取运算符
    97. let run = $("#123 option:selected").val();
    98. alert(run);
    99. switch (run) {
    100. case "+":
    101. $("#lat").val(add(a1, b1));
    102. break;
    103. case "-":
    104. $("#lat").val(subtract(a1, b1));
    105. break;
    106. case "*":
    107. $("#lat").val(ride(a1, b1));
    108. break;
    109. case "/":
    110. $("#lat").val(devide(a1, b1));
    111. break;
    112. case "%":
    113. $("#lat").val(mod(a1, b1));
    114. break;
    115. }
    116. });
    117. //加法
    118. function add(a1, b1) {
    119. return a1 * 1 + b1 * 1;
    120. }
    121. //减法
    122. function subtract(a1, b1) {
    123. return a1 * 1 - b1 * 1;
    124. }
    125. //乘法
    126. function ride(a1, b1) {
    127. return a1 * b1;
    128. }
    129. //除法
    130. function devide(a1, b1) {
    131. if (a1 == 0) {
    132. alert("请输入不为0的数字");
    133. $("#111").val("");
    134. $("#lat").val("");
    135. $("#111").focus();
    136. } else {
    137. return a1 / b1;
    138. }
    139. }
    140. //取余
    141. function mod(a1, b1) {
    142. return a1 % b1;
    143. }
    144. </script>
    145. </html>

更多相关文章

  1. 请实例演绎php遍历数组与js遍历数组的区别
  2. 0805 PHP编程作业
  3. php 求数组中的偶数成员
  4. 数组循环取模及代码实现计算器
  5. 计算器作业
  6. 0805 一.将数组的偶数去出, 二.尝试实现计算器功能
  7. 参数,作用域,匿名函数
  8. 8月2日作业
  9. 实例演绎php遍历数组与js遍历数组的区别

随机推荐

  1. Django查询优化:根据多对一到多对多查找对
  2. 关于autotrace和explain plan是否可以反
  3. 教你如何彻底卸载MySQL数据库
  4. org.apache.commons.dbcp.SQLNestedExcep
  5. 与SUM和GROUP的表连接不工作
  6. VFP 读取SQL2000数据后,当要修改数据时,
  7. 通过Min / Max / Avg操作对表中的值进行
  8. 关于SQL2005安装完毕后,没有SQL Server M
  9. sql间歇性出现无法连接和卡死的情况。
  10. CentOS7安装MySQL并设置远程登录