博主信息
博文 43
粉丝 0
评论 0
访问量 39481
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
数组、计算器
橙絮圆
原创
944人浏览过

数组、计算器

作业标题: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>
批改老师:PHPzPHPz

批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学