Blogger Information
Blog 15
fans 0
comment 0
visits 8378
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组循环取模及代码实现计算器
我们的关系如此狭窄
Original
1234 people have browsed it
<?php
/*
 * 1.给定一个数组$arr = [23,3,45,6,78,8,34],筛选其偶数成员组成新的数组返回,请封装函数。
 *  2. 尝试实现简单的计算器功能,语言不限制。
 *
*/
$arr = [23,3,45,6,78,8,34];
$newA=[];
$i=0;
//while 循环
while(count($arr) > $i){
    if($arr[$i]%2 == 0) $newA[]=$arr[$i]; //取模
    $i++;
}
print_r($newA);
//do while 循环
$arr = [23,3,45,6,78,8,34];
$newA=[];
$b=0;
do{
    if($arr[$b]%2 == 0) $newA[]=$arr[$b];
    $b++;
}while($b<count($arr));
print_r($newA);
//foreach 循环
$newA=[];
$arr = [23,3,45,6,78,8,34];
foreach ($arr as $k=>$v){
    if($v%2 == 0) $newA[]=$v; //取模
  // gettype($v/2) == 'integer'?$newA[]=$v:'';
  // if(gettype($v/2) != 'integer') unset($arr[$k]);
  // if(gettype($v/2) == 'integer') $newA[]=$v;
}
print_r($newA);
//print_r($arr);

//数组方法array_walk  调用匿名幻术  引用传值  保存数据
$arr = [23,3,45,6,78,8,34];
$newA=[];
$callback = function($v,$k) use (&$newA,&$arr){
    if(gettype($v/2) != 'integer') unset($arr[$k]); //获取运算之后的数据类型  然后判断
    if(gettype($v/2) == 'integer') $newA[]=$v;
};
array_walk($arr,$callback);
print_r($newA);
print_r($arr);
die;
?>
<!--jquery写计算器   未完成 0不能做除数 .不能在之前带点的数字之后 运算时候运算字符串转换成运算也难
%前面必须跟数字  整形或者浮点型  这一些列的判断 整理不好  
  PHP版本的  3个input 等于号提交的相对简单就没写-->
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="./layui/css/layui.css"  media="all">
    <!-- 注意:如果你直接复制所有代码到本地,上述css路径需要改成你本地的 -->
</head>
<body>
<div class="layui-main">
    <div class="layui-form-item">
        <div class="layui-input-inline">
            <input name="password" disabled readonly autocomplete="off" class="layui-input layui-disabled text">
        </div>


    </div>
    <div class="layui-btn-container">
        <button type="button" class="layui-btn">clear</button>
        <button type="button" class="layui-btn">delete</button>
        <button type="button" class="layui-btn">%</button>

    </div>
    <div class="layui-btn-container">
        <button type="button" class="layui-btn">1</button>
        <button type="button" class="layui-btn">2</button>
        <button type="button" class="layui-btn">3</button>
        <button type="button" class="layui-btn">+</button>
    </div>
    <div class="layui-btn-container">
        <button type="button" class="layui-btn">4</button>
        <button type="button" class="layui-btn">5</button>
        <button type="button" class="layui-btn">6</button>
        <button type="button" class="layui-btn">-</button>
    </div>
    <div class="layui-btn-container">
        <button type="button" class="layui-btn">7</button>
        <button type="button" class="layui-btn">8</button>
        <button type="button" class="layui-btn">9</button>
        <button type="button" class="layui-btn">*</button>
    </div>
    <div class="layui-btn-container">
        <button type="button" class="layui-btn">0</button>
        <button type="button" class="layui-btn">.</button>

        <button type="button" class="layui-btn">=</button>
        <button type="button" class="layui-btn">/</button>
    </div>
</div>
<script  src="http://libs.baidu.com/jquery/1.7.2/jquery.min.js"></script>
<script src="./layui/layui.js" charset="utf-8"></script>
<!-- 注意:如果你直接复制所有代码到本地,上述 JS 路径需要改成你本地的 -->
<script>
    var strs=new Array(),nums=new Array(),jsu=new Array();



    $('button').click(function () {
        var _this = $(this),val_=_this.html(),_text = $('.text').val();
        let tmp_str = '';
        for (let i =0;i<strs.length;i++){
            tmp_str+=nums[i] + strs[i];
        }
        let tmp_ = _text.replace(tmp_str,'');
        if(!isNaN(val_)){
            if(tmp_.substring(0,1) - 0 > 0 || tmp_.indexOf('.') >= 0 || tmp_ ==''){
                $('.text').val(_text + val_);
            }
        }else{

            let sf = '+-*/';
            switch (val_) {
                case '%':
                    if(tmp_ - 0 > 0) {
                        $('.text').val(_text + val_);
                    }else{
                        if(tmp_.replace('%','') - 0 >0){
                            $('.text').val(_text + val_);
                        }
                    }
                    break;
                case 'clear':
                    $('.text').val('');
                    break;
                case  'delete':
                    let ex = _text.substring(0,_text.length - 1);
                    $('.text').val(ex);
                    break;
                case '=':

                    for (let i =0;i<strs.length;i++){
                       if(strs[i] == '*'){
                           nums[i+1] = nums[i] * nums[i+1];
                           nums[i] = 0;
                           strs[i] = '+';
                       }
                        if(strs[i] == '/'){
                            nums[i+1] = nums[i]  / nums[i+1];
                            nums[i] = 0;
                            strs[i] = '+';
                        }
                    }
                    let sum_ =0;
                    for(let i =0;i<strs.length;i++){
                        if(strs[i] == '+'){
                            nums[i+1] = nums[i] + nums[i+1];

                        }
                        if(strs[i] == '-'){
                            nums[i+1] = nums[i] - nums[i+1];
                        }
                        if(i == strs.length){
                            sum_ = nums[i]
                        }
                     }
                    alert(sum_);
                    break;
                default:
                        if(val_ == '.' && tmp_.indexOf('.') < 0 ){
                            $('.text').val(_text + val_);
                        }
                        if(tmp_ - 0 > 0){
                            if(sf.indexOf(val_) >= 0){
                                nums.push(tmp_);
                                strs.push(val_);

                                $('.text').val(_text + val_);
                            }

                        }



            }

        }



    })
</script>
</body>
</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