PHP -- four arithmetic operation

时间:2023-03-09 18:42:16
PHP -- four arithmetic operation

PHP 生成 简单四则运算。

Thanks for Open Source.

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

 <?php
/*基于jiaych php四则运算计算函数
http://download.csdn.net/user/jiaych 实现 */
class randmath
{
//$num 数字个数,$nsize 数字位数
public function creatmath($num, $nsize)
{
$str_num = rand(0, pow(10,$nsize));
for ($i = 1; $i < $num; $i++) {
$str_t=rand(0, pow(10,$nsize)); $str_num = $this->rand_num($str_num, rand(0, pow(10,$nsize))); }
return $str_num;
}
//生成四则运算符号
function rand_num($str1, $str2)
{
$s_num = rand(1, 4);
$str="";
switch ($s_num) {
case 1: //+
$str= $str1 . "+" . $str2;
break;
case 2: //-
$str= $str1 . "-" . $str2;
break;
case 3: //*
$str= $str1 . "*" . $str2;
break;
case 4: // /
$str= $str1 . "/" . $str2;
break;
/* case 5: //()
echo "</br>" . $s_num;
return $str1."+".$str2;
break; */
}
return $str;
}
}
class math_rpn { function exp2rpn($expression){ $_stack = array('#');
$_rpnexp = array();
$_operator = array('(', '+', '-', '*', '/', ')');
$_priority = array('#' => 0, '(' => 10, '+' => 20, '-' => 20, '*' => 30, '/' => 30);
$data='';
$len = strlen($expression); for($i = 0; $i < $len; $i++) {
$char = substr($expression, $i, 1); if ($char == '(') {
$_stack[] = $char;
continue;
} else if ( ! in_array($char, $_operator)) {
$data.=$char;
if($i+1<$len)
{
$next = substr($expression, $i+1, 1);
if(in_array($next, $_operator)||is_null($next))
{
$_rpnexp[] = $data;
$data=null;
}
}
else
{
$_rpnexp[] = $data;
$data=null;
}
continue;
} else if ($char == ')') {
for($j = count($_stack); $j >= 0; $j--) {
$tmp = array_pop($_stack);
if ($tmp == "(") {
break;
} else {
$_rpnexp[] = $tmp;
}
}
continue;
} else if ($_priority[$char] <= $_priority[end($_stack)]) {
$_rpnexp[] = array_pop($_stack);
$_stack[] = $char;
continue;
} else {
$_stack[] = $char;
continue;
}
} for($i = count($_stack); $i >= 0; $i--) {
if (end($_stack) == '#') break;
$_rpnexp[] = array_pop($_stack);
}
$mystack=array();
foreach($_rpnexp as $ret)
{
if($ret=="+")
{
$tmp_a=array_pop($mystack);
$tmp_b=array_pop($mystack);
$mystack[]=$tmp_a+$tmp_b;
}
else if($ret=="-")
{
$tmp_a=array_pop($mystack);
$tmp_b=array_pop($mystack);
$mystack[]=$tmp_b-$tmp_a;
}
else if($ret=="*")
{
$tmp_a=array_pop($mystack);
$tmp_b=array_pop($mystack);
$mystack[]=$tmp_b*$tmp_a;
}
else if($ret=="/")
{
$tmp_a=array_pop($mystack);
$tmp_b=array_pop($mystack);
$mystack[]=$tmp_b/$tmp_a;
}
else
{
$mystack[]=$ret;
}
}
return $mystack[0];
}
}//测试实例
/*$expression = "(10.1+3)*(15)-1.4+5";
echo $expression."=";
$mathrpn = new math_rpn();
echo $mathrpn->exp2rpn($expression)."</br>";
*/
// $rand_math = new randmath();
// echo $rand_math->creatmath(4, 2);
?>

randmath.php

  <?php
header("Content-type: text/html; charset=utf-8");
include("randmath.php"); $mathrpn = new math_rpn();
$rand_math = new randmath();//生成随机式子 $i=10;
while($i>0)
{
$rand_formula = $rand_math->creatmath(4, 1);//生成随机式子方法($num 数字个数,$nsize 数字位数)
$math_result=$mathrpn->exp2rpn($rand_formula);
if(is_int($math_result)&$math_result>0)
{
$i--;
echo $rand_formula . "=" . $math_result."</br>";
}
}
?>

ShowRPN.php

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