例:
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);