Home > php教程 > php手册 > body text

用PHP实现的四则运算表达式计算实现代码

WBOY
Release: 2016-06-06 20:38:23
Original
976 people have browsed it

题目要求:有一个四则运算的字符串表达式,编写一个函数,计算四则运算的结果

PHP实现:
代码如下:
/**
* 计算四则运算表达式
*/
error_reporting(E_ALL);
$exp = '(1+2*(3+5)/4)*(3+(5-4)*2)';
$arr_exp = array();
for($i=0;$i$arr_exp[] = $exp[$i];
}
$result = calcexp( array_reverse($arr_exp) );
echo $exp . '=' . $result;
function calcexp( $exp ){
$arr_n = array();
$arr_op = array();
while( ($s = array_pop( $exp )) != '' ){
if( $s == '(' ){
$temp = array(); $quote = 1; $endquote = 0;
while( ($t = array_pop($exp)) != '' ){
if( $t == '(' ){
$quote++;
}
if( $t == ')' ){
$endquote++;
if( $quote == $endquote ){
break;
}
}
array_push($temp, $t);
}
$temp = array_reverse($temp);
array_push($arr_n, calcexp($temp) );
}else if( $s == '*' || $s == '/' ){
$n2 = array_pop($exp);
if( $n2 == '(' ){
$temp = array(); $quote = 1; $endquote = 0;
while( ($t = array_pop($exp)) != '' ){
if( $t == '(' ){
$quote++;
}
if( $t == ')' ){
$endquote++;
if( $quote == $endquote )
break;
}
array_push($temp, $t);
}
$temp = array_reverse($temp);
$n2 = calcexp($temp);
}
$op = $s;
$n1 = array_pop($arr_n);
$result = operation($n1, $op, $n2);
array_push($arr_n, $result);
}elseif( $s == '+' || $s == '-' ){
array_push($arr_op, $s);
}else{
array_push($arr_n, $s);
}
}
$n2 = array_pop($arr_n);
while( ($op = array_pop($arr_op)) != '' ){
$n1 = array_pop($arr_n);
$n2 = operation($n1, $op, $n2);
}
return $n2;
}
function operation( $n1, $op, $n2 ){
switch ($op) {
case '+':
return intval($n1) + intval($n2);
break;
case '-':
return intval($n1) - intval($n2);
break;
case '*':
return intval($n1) * intval($n2);
break;
case '/':
return intval($n1) / intval($n2);
break;
}
}

这个实现方式中使用了两个堆栈,一个用来存储数字,一个用来存储运算符,遇到括号以后就递归进入括号内运算,实现方式有点笨拙,后面补充一下“逆波兰表达式”的算法实现。
Related labels:
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 Recommendations
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!