欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

实现单操作符简单公式解析

程序员文章站 2024-02-11 18:14:40
...
  1. $string = "#data_1 / #data_2";
  2. class operate{
  3. private $params;
  4. private $dataarray;
  5. private $result;
  6. private function getResult() {
  7. return $this->result;
  8. }
  9. private function setResult($result) {
  10. $this->result = $result;
  11. }
  12. private function getDataarray() {
  13. return $this->dataarray;
  14. }
  15. private function setDataarray($dataarray) {
  16. $this->dataarray = $dataarray;
  17. }
  18. private function getParams() {
  19. return $this->params;
  20. }
  21. public function setParams($params) {
  22. $this->params = $params;
  23. }
  24. private function add($data_1,$data_2){
  25. return $data_1+$data_2;
  26. }
  27. private function minus($data_1,$data_2){
  28. return $data_1-$data_2;
  29. }
  30. private function multiply($data_1,$data_2){
  31. return $data_1*$data_2;
  32. }
  33. private function divide($data_1,$data_2){
  34. return $data_1/$data_2;
  35. }
  36. private function stringtoarray(){
  37. $params = $this->getParams();
  38. $dataarray = explode(" ", $params);
  39. $this->setDataarray($dataarray);
  40. }
  41. private function getpos($array){
  42. $pos = array();
  43. $nums = sizeof($array);
  44. for ($i=0;$i if(!is_numeric($array[$i])){
  45. $pos[] = $i;
  46. }
  47. }
  48. return $pos;
  49. }
  50. private function getdividepos(){
  51. $array = $this->getDataarray();// ||$expressionarray
  52. $pos = array();
  53. $nums = sizeof($array);
  54. for ($i=0;$i if($array[$i]=="/"){
  55. $pos[] = $i;
  56. }
  57. }
  58. return $pos;
  59. }
  60. private function iszero(){
  61. $dataarray = $this->getDataarray();
  62. //print_r($dataarray);
  63. $array = $this->getdividepos(); // ||$devidpos
  64. //print_r($array);
  65. $nums = sizeof($array);
  66. echo "
    iszero nums:".$nums."
    ";
  67. if ($nums==0){
  68. $this->setResult(1);
  69. }else{
  70. for ($i=0;$i $key = $array[$i]+1;
  71. //$key = $i + 1;
  72. echo "
    key :$key
    ";
  73. //echo "data:".$dataarray[$key];
  74. $data = (int)($dataarray[$key]);
  75. echo "data:".$data."
    ";
  76. if ($data==0){
  77. $this->setResult(0);
  78. }
  79. else {
  80. $this->setResult($data);
  81. }
  82. }
  83. }
  84. }
  85. public function main(){
  86. $this->stringtoarray();
  87. $dataarray = $this->getDataarray(); //array("#data_1","/","#data_2");
  88. print_r($dataarray);
  89. $dividepos = $this->iszero();
  90. $result = $this->getResult();
  91. echo "reslut:".$result."
    ";
  92. if($result==0){
  93. return 0;
  94. }else{
  95. $operatepos = $this->getpos($dataarray);
  96. $nums = sizeof($operatepos);
  97. for($i=0;$i $pos = $operatepos[$i];
  98. $operate = $dataarray[$pos];
  99. echo "operate:".$operate."
    ";
  100. switch ($operate){
  101. case "+":
  102. $key1 = $pos - 1;
  103. $key2 = $pos + 1;
  104. $data_1 = $dataarray[$key1];
  105. $data_2 = $dataarray[$key2];
  106. $tempdata = $this->add($data_1, $data_2);
  107. return $tempdata;
  108. case "-":
  109. $key1 = $pos - 1;
  110. $key2 = $pos + 1;
  111. $data_1 = $dataarray[$key1];
  112. $data_2 = $dataarray[$key2];
  113. $tempdata = $this->minus($data_1, $data_2);
  114. return $tempdata;
  115. case "*":
  116. $key1 = $pos - 1;
  117. $key2 = $pos + 1;
  118. $data_1 = $dataarray[$key1];
  119. $data_2 = $dataarray[$key2];
  120. $tempdata = $this->multiply($data_1, $data_2);
  121. return $tempdata;
  122. case "/":
  123. $key1 = $pos - 1;
  124. $key2 = $pos + 1;
  125. $data_1 = $dataarray[$key1];
  126. $data_2 = $dataarray[$key2];
  127. $tempdata = $this->divide($data_1, $data_2);
  128. return $tempdata;
  129. }
  130. }
  131. }
  132. }
  133. }
  134. $data_3 = "2 * 3";
  135. $compute = new operate();
  136. $compute ->setParams($data_3);
  137. echo "result:".$compute->main();
复制代码