Implement simple formula parsing of single operator

Source: Internet
Author: User
<无详细内容>
  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. $key 1 = $pos-1;
  107. $key 2 = $pos + 1;
  108. $data _1 = $dataarray [$key 1];
  109. $data _2 = $dataarray [$key 2];
  110. $tempdata = $this->add ($data _1, $data _2);
  111. return $tempdata;
  112. Case "-":
  113. $key 1 = $pos-1;
  114. $key 2 = $pos + 1;
  115. $data _1 = $dataarray [$key 1];
  116. $data _2 = $dataarray [$key 2];
  117. $tempdata = $this->minus ($data _1, $data _2);
  118. return $tempdata;
  119. Case "*":
  120. $key 1 = $pos-1;
  121. $key 2 = $pos + 1;
  122. $data _1 = $dataarray [$key 1];
  123. $data _2 = $dataarray [$key 2];
  124. $tempdata = $this->multiply ($data _1, $data _2);
  125. return $tempdata;
  126. Case "/":
  127. $key 1 = $pos-1;
  128. $key 2 = $pos + 1;
  129. $data _1 = $dataarray [$key 1];
  130. $data _2 = $dataarray [$key 2];
  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 ();
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.