实现单操作符简单公式解析
Freigeben: 2016-07-25 09:02:32
Original
1129 Leute haben es durchsucht
-
$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 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 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 $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 $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();
-
复制代码
|
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Neueste Artikel des Autors
-
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