PHP -- four arithmetic operation

PHP 生成 简单四则运算。

Thanks for Open Source.

本代码基于 jiaych php四则运算计算函数 实现。

  1 <?php
  2     /*基于jiaych php四则运算计算函数
  3     http://download.csdn.net/user/jiaych 实现
  4     
  5     */
  6     class randmath
  7     { 
  8           //$num 数字个数,$nsize 数字位数
  9           public function creatmath($num, $nsize)
 10           { 
 11                 $str_num = rand(0, pow(10,$nsize));
 12                 for ($i = 1; $i < $num; $i++) {
 13                 $str_t=rand(0, pow(10,$nsize)); 
 14                   
 15                      $str_num = $this->rand_num($str_num, rand(0, pow(10,$nsize)));
 16  
 17                 }
 18                 return $str_num;
 19           }
 20           //生成四则运算符号
 21          function rand_num($str1, $str2)
 22           { 
 23                $s_num = rand(1, 4);
 24                 $str="";
 25                 switch ($s_num) {
 26                       case 1: //+
 27                             $str= $str1 . "+" . $str2;
 28                             break;
 29                       case 2: //-
 30                             $str= $str1 . "-" . $str2;
 31                             break;
 32                       case 3: //*
 33                             $str= $str1 . "*" . $str2;
 34                             break;
 35                       case 4: // /
 36                             $str= $str1 . "/" . $str2;
 37                             break; 
 38                             /* case 5: //()
 39                             echo "</br>" . $s_num;
 40                             return $str1."+".$str2;
 41                             break; */
 42                 }
 43                 return $str; 
 44           }
 45     }
 46     class math_rpn {
 47     
 48     function exp2rpn($expression){
 49 
 50         $_stack  = array('#');
 51         $_rpnexp = array();
 52         $_operator = array('(', '+', '-', '*', '/', ')');
 53         $_priority = array('#' => 0, '(' => 10, '+' => 20, '-' => 20, '*' => 30, '/' => 30);
 54         $data='';
 55         $len = strlen($expression);
 56         
 57         for($i = 0; $i < $len; $i++) {
 58             $char = substr($expression, $i, 1);
 59             
 60             if ($char == '(') {
 61                 $_stack[] = $char;
 62                 continue;
 63             } else if ( ! in_array($char, $_operator)) {
 64                 $data.=$char;
 65                 if($i+1<$len)
 66                 {
 67                     $next = substr($expression, $i+1, 1);
 68                     if(in_array($next, $_operator)||is_null($next))
 69                     {
 70                         $_rpnexp[] = $data;
 71                         $data=null;
 72                     }
 73                 }
 74                 else
 75                 {
 76                     $_rpnexp[] = $data;
 77                     $data=null;
 78                 }
 79                 continue;
 80             } else if ($char == ')') {
 81                 for($j = count($_stack); $j >= 0; $j--) {
 82                     $tmp = array_pop($_stack);
 83                     if ($tmp == "(") {
 84                         break;    
 85                     } else {
 86                         $_rpnexp[] = $tmp;
 87                     }
 88                 }
 89                 continue;
 90             } else if ($_priority[$char] <= $_priority[end($_stack)]) {
 91                 $_rpnexp[] = array_pop($_stack);
 92                 $_stack[]  = $char;
 93                 continue;
 94             } else {
 95                 $_stack[] = $char;
 96                 continue;
 97             }
 98         }
 99         
100         for($i = count($_stack); $i >= 0; $i--) {
101             if (end($_stack) == '#') break;
102             $_rpnexp[] = array_pop($_stack);
103         }
104         $mystack=array();    
105         foreach($_rpnexp as $ret)
106         {
107             if($ret=="+")
108             {
109                 $tmp_a=array_pop($mystack);    
110                 $tmp_b=array_pop($mystack);    
111                 $mystack[]=$tmp_a+$tmp_b;
112             }
113             else if($ret=="-")
114             {
115                 $tmp_a=array_pop($mystack);    
116                 $tmp_b=array_pop($mystack);    
117                 $mystack[]=$tmp_b-$tmp_a;
118             }
119             else if($ret=="*")
120             {
121                 $tmp_a=array_pop($mystack);    
122                 $tmp_b=array_pop($mystack);    
123                 $mystack[]=$tmp_b*$tmp_a;
124             }
125             else if($ret=="/")
126             {
127                 $tmp_a=array_pop($mystack);    
128                 $tmp_b=array_pop($mystack);    
129                 $mystack[]=$tmp_b/$tmp_a;
130             }
131             else
132             {
133                 $mystack[]=$ret;
134             }
135         }
136         return $mystack[0];    
137     }
138 }//测试实例
139 /*$expression = "(10.1+3)*(15)-1.4+5";
140 echo $expression."="; 
141 $mathrpn = new math_rpn();
142 echo $mathrpn->exp2rpn($expression)."</br>";
143 */
144   //  $rand_math = new randmath(); 
145   //  echo $rand_math->creatmath(4, 2);
146 ?>
randmath.php
 1  <?php
 2     header("Content-type: text/html; charset=utf-8");
 3     include("randmath.php");
 4  
 5     $mathrpn = new math_rpn();
 6     $rand_math  = new randmath();//生成随机式子
 7  
 8     $i=10;
 9     while($i>0)
10     {
11         $rand_formula = $rand_math->creatmath(4, 1);//生成随机式子方法($num 数字个数,$nsize 数字位数)
12         $math_result=$mathrpn->exp2rpn($rand_formula);
13         if(is_int($math_result)&$math_result>0)
14         {
15             $i--;
16             echo $rand_formula . "=" . $math_result."</br>";
17         }          
18     }
19 ?> 
ShowRPN.php

这样就能生成简单四则运算了。

posted @ 2014-03-17 23:44  MicroHao  阅读(333)  评论(2编辑  收藏  举报