实现单操作符简单公式解析
时间:2021-07-01 10:21:17
帮助过:19人阅读
<无详细内容>
- $string = "#data_1 / #data_2";
- class operate{
- private $params;
- private $dataarray;
- private $result;
- private function getResult() {
- return $this->result;
- }
- private function setResult($result) {
- $this->result = $result;
- }
- private function getDataarray() {
- return $this->dataarray;
- }
- private function setDataarray($dataarray) {
- $this->dataarray = $dataarray;
- }
- private function getParams() {
- return $this->params;
- }
- public function setParams($params) {
- $this->params = $params;
- }
- private function add($data_1,$data_2){
- return $data_1+$data_2;
- }
- private function minus($data_1,$data_2){
- return $data_1-$data_2;
- }
- private function multiply($data_1,$data_2){
- return $data_1*$data_2;
- }
- private function divide($data_1,$data_2){
-
- return $data_1/$data_2;
-
- }
- private function stringtoarray(){
- $params = $this->getParams();
- $dataarray = explode(" ", $params);
- $this->setDataarray($dataarray);
-
- }
-
- private function getpos($array){
-
- $pos = array();
- $nums = sizeof($array);
- for ($i=0;$i<$nums;$i++){
- if(!is_numeric($array[$i])){
- $pos[] = $i;
- }
- }
- return $pos;
- }
- private function getdividepos(){
- $array = $this->getDataarray();// ||$expressionarray
- $pos = array();
- $nums = sizeof($array);
- for ($i=0;$i<$nums;$i++){
- if($array[$i]=="/"){
- $pos[] = $i;
- }
- }
- return $pos;
- }
- private function iszero(){
- $dataarray = $this->getDataarray();
- //print_r($dataarray);
- $array = $this->getdividepos(); // ||$devidpos
- //print_r($array);
- $nums = sizeof($array);
- echo "
iszero nums:".$nums." "; - if ($nums==0){
- $this->setResult(1);
- }else{
- for ($i=0;$i<$nums;$i++){
- $key = $array[$i]+1;
- //$key = $i + 1;
- echo "
key :$key "; - //echo "data:".$dataarray[$key];
- $data = (int)($dataarray[$key]);
- echo "data:".$data."
"; - if ($data==0){
- $this->setResult(0);
- }
- else {
- $this->setResult($data);
- }
- }
- }
- }
-
- public function main(){
- $this->stringtoarray();
- $dataarray = $this->getDataarray(); //array("#data_1","/","#data_2");
- print_r($dataarray);
- $dividepos = $this->iszero();
- $result = $this->getResult();
- echo "reslut:".$result."
"; - if($result==0){
- return 0;
- }else{
- $operatepos = $this->getpos($dataarray);
- $nums = sizeof($operatepos);
- for($i=0;$i<$nums;$i++){
- $pos = $operatepos[$i];
- $operate = $dataarray[$pos];
- echo "operate:".$operate."
"; - switch ($operate){
- case "+":
- $key1 = $pos - 1;
- $key2 = $pos + 1;
- $data_1 = $dataarray[$key1];
- $data_2 = $dataarray[$key2];
- $tempdata = $this->add($data_1, $data_2);
- return $tempdata;
- case "-":
- $key1 = $pos - 1;
- $key2 = $pos + 1;
- $data_1 = $dataarray[$key1];
- $data_2 = $dataarray[$key2];
- $tempdata = $this->minus($data_1, $data_2);
- return $tempdata;
- case "*":
- $key1 = $pos - 1;
- $key2 = $pos + 1;
- $data_1 = $dataarray[$key1];
- $data_2 = $dataarray[$key2];
- $tempdata = $this->multiply($data_1, $data_2);
- return $tempdata;
- case "/":
- $key1 = $pos - 1;
- $key2 = $pos + 1;
- $data_1 = $dataarray[$key1];
- $data_2 = $dataarray[$key2];
- $tempdata = $this->divide($data_1, $data_2);
- return $tempdata;
- }
-
-
- }
-
- }
-
-
- }
-
-
- }
- $data_3 = "2 * 3";
- $compute = new operate();
- $compute ->setParams($data_3);
- echo "result:".$compute->main();
|