- $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();
-
复制代码
|
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31