0

例子:

Array (
     [0] => 35
     [1] => -
     [2] => 59
     [3] => *
     [4] => 2
     [5] => /
     [6] => 27
     [7] => *
     [8] => 2 )

然后计算:

59*2=118

新数组是:

 Array (
    [0] => 35
     [1] => -
     [2] => 118
     [3] => /
     [4] => 27
     [5] => *
     [6] => 2 )

这是我的原始来源: input ($_POST['numbers'])是一个字符串,例如:

65*6/6+5-5

class calculator {
    //property
    private $str='';
    private $len=0;
    private $ar_str=array();
    private $ar_design=array();
    private $ar_sum=array();
    private $ar_min=array();
    private $ar_mult=array();
    private $ar_divi=array();

//Method
public function __construct($str1=''){
    $this->str=$str1;
    $this->len=strlen($this->str);
    $this->ar_str=str_split($this->str);
    if($this->ar_str[0] == '+' ||
    $this->ar_str[0] == '-' ||
    $this->ar_str[0] == '*' ||
    $this->ar_str[0] == '/' ||
    $this->ar_str[$this->len-1] == '+' ||
    $this->ar_str[$this->len-1] == '-' ||
    $this->ar_str[$this->len-1] == '*' ||
    $this->ar_str[$this->len-1] == '/'
    ){
        exit("Syntax error!");
    }else if(!filter_var($this->ar_str[0], FILTER_VALIDATE_INT)){
        exit("just use numbers and 4 operators!");
    }
    $this->ar_design[0]=$this->ar_str[0];
    //start for
    $j=1;
    for($i=1;$i<$this->len;$i++){
        if($this->ar_str[$i] == '+' || $this->ar_str[$i] == '-' || $this->ar_str[$i] == '*' || $this->ar_str[$i] == '/'){
            if($this->ar_str[$i-1] == '+' || $this->ar_str[$i-1] == '-' || $this->ar_str[$i-1] == '*' || $this->ar_str[$i-1] == '/'){
                exit("Syntax error!");
            }else{
                $this->ar_design[$j]=$this->ar_str[$i];
                $j++;
            }
        }else if(filter_var($this->ar_str[$i], FILTER_VALIDATE_INT)){
            if($this->ar_str[$i-1] == '+' || $this->ar_str[$i-1] == '-' || $this->ar_str[$i-1] == '*' || $this->ar_str[$i-1] == '/'){
                $this->ar_design[$j]=$this->ar_str[$i];
            }else{
                $j--;
                $this->ar_design[$j]=$this->ar_design[$j].$this->ar_str[$i];
            }
        $j++;
        }else{
            exit("just use numbers and 4 operators!");
        }
    }//end of for

    print_r($this->ar_design);//array this array should be calculate!!!!!
}//end construct

}

if(isset($_POST['numbers'])){
    $num_str=trim($_POST['numbers']);
    if($num_str!=''){
        $num_str = str_replace('`','+',$num_str);
        new calculator($num_str);
    }
}

我可以找到答案:

$this->len_d=count($this->ar_design);
        $this->ar_cal[0]=$this->ar_design[0];
        $k=1;
        for($i=1;$i<$this->len_d;$i++){
            if($this->ar_design[$i] == '*'){
                $k--;
                $this->ar_cal[$k]=$this->ar_design[$i-1]*$this->ar_design[$i+1];
                $i++;
            }else{
                $this->ar_cal[$k]=$this->ar_design[$i];
            }
            $k++;
        }
        print_r($this->ar_cal);
4

3 回答 3

3

你可以用一个循环来做,然后执行它。

   $array=Array (35,'-',59,'*',2,'/',27,'*',2);
        foreach ($array as $value){
          $stringify.=$value;
        }
        echo 'Calculation looks like this: '.$stringify.'<br/>';

        function calculate( $math ){
            $calc = create_function("", "return (" .$math. ");" );
            return $calc();
        }   
        echo calculate($stringify);

可以改进,例如通过验证输入...

工作示例:http ://allanthya.net/arrcalc2.php

于 2013-02-18T14:25:45.777 回答
0
<?php
    $array = array( 35, '-', 59, '*', 2, '/', 27, '*', 2 );
    print_r($array);
    echo "<p>";

    while ( true ) {
        $div = array_keys($array, '/');
        $mult = array_keys($array, '*');
        $add = array_keys($array, '+');
        $sub = array_keys($array, '-');

        if ( count($div) >= 1 ) {
            $index = $div[0];
            $op1 = $array[$index-1];
            $op2 = $array[$index+1];

            $array[$index] = $op1 / $op2;
            unset($array[$index-1]);
            unset($array[$index+1]);
            $array = array_values($array);

            continue;
        } else if ( count($mult) >= 1 ) {
            $index = $mult[0];
            $op1 = $array[$index-1];
            $op2 = $array[$index+1];

            $array[$index] = $op1 * $op2;
            unset($array[$index-1]);
            unset($array[$index+1]);
            $array = array_values($array);

            continue;
        } else if ( count($add) >= 1 ) {
            $index = $add[0];
            $op1 = $array[$index-1];
            $op2 = $array[$index+1];

            $array[$index] = $op1 + $op2;
            unset($array[$index-1]);
            unset($array[$index+1]);
            $array = array_values($array);

            continue;
        } else if ( count($sub) >= 1 ) {
            $index = $sub[0];
            $op1 = $array[$index-1];
            $op2 = $array[$index+1];

            $array[$index] = $op1 - $op2;
            unset($array[$index-1]);
            unset($array[$index+1]);
            $array = array_values($array);

            continue;
        }
        else {
            break;
        }
    }
    print_r($array);
    echo "<p>";
?>

这是 while 循环每次迭代后 print_r() 的输出,因此您可以看到它逐个运行除法、乘法、加法和减法,并在运行时修改数组:

Array ( [0] => 35 [1] => - [2] => 59 [3] => * [4] => 2 [5] => / [6] => 27 [7] => * [8] => 2 )

Array ( [0] => 35 [1] => - [2] => 59 [3] => * [4] => 0.0740740740741 [5] => * [6] => 2 )

Array ( [0] => 35 [1] => - [2] => 4.37037037037 [3] => * [4] => 2 )

Array ( [0] => 35 [1] => - [2] => 8.74074074074 )

Array ( [0] => 26.2592592593 ) 
于 2013-02-18T14:34:49.653 回答
0

为什么不计算将输入拆分为数组,然后输出结果?举个例子:

$input = "35-59*2/27*2";

if (preg_match('/[^-+*\/\\d]/', $input)) // simple checking
{
    echo "just use numbers and 4 operators!"; 
}
else
{
    $substraction = explode('-', $input);
    foreach ($substraction as $pos_s => $sub)
    {
        $addition = explode('+', $sub);
        foreach ($addition as $pos_a => $add)
        {
            $multiplication = explode('*', $add);
            foreach ($multiplication as $pos_m => $mult)
            {
                $division = explode('/', $mult);
                $d = $division[0];
                for ($i=1; $i < count($division);$i++)              
                    $d = $d / $division[$i];

                $multiplication[$pos_m] = $d;
            }
            $m = $multiplication[0];
            for ($i=1; $i < count($multiplication);$i++)                
                $m = $m * $multiplication[$i];

            $addition[$pos_a] = $m;
        }
        $a = $addition[0];
        for ($i=1; $i < count($addition);$i++)              
            $a = $a + $addition[$i];

        $substraction[$pos_s] = $a;
    }
    $result = $substraction[0];
    for ($i=1; $i < count($substraction);$i++)              
        $result = $result - $substraction[$i];
    echo $input . " = " . $result; 
}

印刷

35-59*2/27*2 = 26.259259259259

于 2013-02-18T18:23:17.397 回答