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'])文字列です, LIKE:

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()の出力を次に示します。これにより、除算、乗算、加算、減算を1つずつ実行し、配列を変更しながら実行することができます。

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 に答える