Blogger Information
Blog 43
fans 0
comment 0
visits 30327
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组、计算器
橙絮圆
Original
773 people have browsed it

数组、计算器

作业标题: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>
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post