Implement simple formula parsing with a single operator

WBOY
Release: 2016-07-25 09:02:32
Original
1070 people have browsed it
  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<$nums;$i++){
  45. if(!is_numeric($array[$i])){
  46. $pos[] = $i;
  47. }
  48. }
  49. return $pos;
  50. }
  51. private function getdividepos(){
  52. $array = $this->getDataarray();// ||$expressionarray
  53. $pos = array();
  54. $nums = sizeof($array);
  55. for ($i=0;$i<$nums;$i++){
  56. if($array[$i]=="/"){
  57. $pos[] = $i;
  58. }
  59. }
  60. return $pos;
  61. }
  62. private function iszero(){
  63. $dataarray = $this->getDataarray();
  64. //print_r($dataarray);
  65. $array = $this->getdividepos(); // ||$devidpos
  66. //print_r($array);
  67. $nums = sizeof($array);
  68. echo "
    iszero nums:".$nums."
    ";
  69. if ($nums==0){
  70. $this->setResult(1);
  71. }else{
  72. for ($i=0;$i<$nums;$i++){
  73. $key = $array[$i]+1;
  74. //$key = $i + 1;
  75. echo "
    key :$key
    ";
  76. //echo "data:".$dataarray[$key];
  77. $data = (int)($dataarray[$key]);
  78. echo "data:".$data."
    ";
  79. if ($data==0){
  80. $this->setResult(0);
  81. }
  82. else {
  83. $this->setResult($data);
  84. }
  85. }
  86. }
  87. }
  88. public function main(){
  89. $this->stringtoarray();
  90. $dataarray = $this->getDataarray(); //array("#data_1","/","#data_2");
  91. print_r($dataarray);
  92. $dividepos = $this->iszero();
  93. $result = $this->getResult();
  94. echo "reslut:".$result."
    ";
  95. if($result==0){
  96. return 0;
  97. }else{
  98. $operatepos = $this->getpos($dataarray);
  99. $nums = sizeof($operatepos);
  100. for($i=0;$i<$nums;$i++){
  101. $pos = $operatepos[$i];
  102. $operate = $dataarray[$pos];
  103. echo "operate:".$operate."
    ";
  104. switch ($operate){
  105. case "+":
  106. $key1 = $pos - 1;
  107. $key2 = $pos + 1;
  108. $data_1 = $dataarray[$key1];
  109. $data_2 = $dataarray[$key2];
  110. $tempdata = $this->add($data_1, $data_2);
  111. return $tempdata;
  112. case "-":
  113. $key1 = $pos - 1;
  114. $key2 = $pos + 1;
  115. $data_1 = $dataarray[$key1];
  116. $data_2 = $dataarray[$key2];
  117. $tempdata = $this->minus($data_1, $data_2);
  118. return $tempdata;
  119. case "*":
  120. $key1 = $pos - 1;
  121. $key2 = $pos + 1;
  122. $data_1 = $dataarray[$key1];
  123. $data_2 = $dataarray[$key2];
  124. $tempdata = $this->multiply($data_1, $data_2);
  125. return $tempdata;
  126. case "/":
  127. $key1 = $pos - 1;
  128. $key2 = $pos + 1;
  129. $data_1 = $dataarray[$key1];
  130. $data_2 = $dataarray[$key2];
  131. $tempdata = $this->divide($data_1, $data_2);
  132. return $tempdata;
  133. }
  134. }
  135. }
  136. }
  137. }
  138. $data_3 = "2 * 3";
  139. $compute = new operate();
  140. $compute ->setParams($data_3);
  141. echo "result:".$compute->main();
复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!