PHP版计算器
2015-08-30 16:56
543 查看
</pre><pre name="code" class="php"><?php /** * Created by PhpStorm. * User: Super pan * Date: 2015/8/26 * Time: 19:47 */ class CalController{ /** * @return string */ public function showAnswer(){ $expression = $_GET['str']; $exp= $this->arrange(str_split($expression)); $result= $this->getResult($exp); $arr = array( "expression"=>$expression, "myanswer" => $result ); return $this->array2Json($arr); } public function array2Json($arr) { foreach ( $arr as $key => $value ) { $arr[$key] = urlencode ($value); } $res = urldecode (json_encode ($arr)); return $res; } public function getResult($exp){ $result= array(); for($i=0; $i< count($exp); $i++){ if($this->isNum($exp[$i])){ //如果是数字 $result[]= $exp[$i]; }else if($exp[$i]!= '('){ //如果是运算符,且不是左括号 '(' $rank=$this->getRank($exp[$i]); for($j=count($result)-1; $j>0; $j--){ $d=$result[$j]; if(!$this->isNum($d) && $this->getRank($d)>=$rank){ //该字符是运算符且优先级更大或相等 $r=$this->calc($result[$j-1],$result[$j+1], $result[$j]); if($r=="by zero"){ return $r; } array_splice($result, $j+1, 1); array_splice($result, $j, 1); array_splice($result, $j-1, 1, $r); } } $result[]= $exp[$i]; }else if($exp[$i]== '('){ //如果是左括号 $inner= array(); for($i=$i+1; $i<count($exp)&&$exp[$i]!=')'; $i++){ $inner[]= $exp[$i]; } $innerResult= $this->getResult($inner); if($innerResult=="by zero"){ return $innerResult; } $result[]= $innerResult; } } $m=count($result)-1; if($m!=0){ for( ; $m>0; $m--){ if(!$this->isNum($result[$m])){ $r=$this->calc($result[$m-1], $result[$m+1], $result[$m]); array_splice($result, $m+1, 1); array_splice($result, $m, 1); array_splice($result, $m-1, 1, $r); } } } return $result[0]; } public function arrange($array){ //将形如'1','2','+','3','4'解析成'12','+','34' $exp= array(); for($i=0; $i<count($array); $i++){ if(!is_numeric($array[$i])&&$array[$i]!='.'){ $exp[]= $array[$i]; }else{ $temp= ''; while($i<count($array)&&(is_numeric($array[$i])||$array[$i]=='.')){ $temp=$temp.$array[$i]; $i++; } $i--; $exp[]=$temp; } } return $exp; } public function getRank($d){ if($d=='+'||$d=='-'){ //加减号优先级为1 return 1; }else{ //乘除号优先级为2 return 2; } } public function isNum($d){ if($d=='+'||$d=='-'||$d=='*'||$d=='/'||$d=='('||$d==')'){ return false; }else{ return true; } } public function calc($left, $right, $op){ if($op=='+') return ($left+$right); else if($op=='-') return ($left-$right); else if($op=='*'){ if($right=='0'){ return "by zero"; }else{ return ($left/$right); } } else return ($left/$right); } } $p= new CalController(); $ans= $p->showAnswer(); echo $ans;
相关文章推荐
- php 浮点数比较方法
- 把PHP的数组变成带单引号的字符串
- 了解FTP
- vsftpd匿名用户只能上传不能下载
- 29. PHP 错误控制
- vsftp不同帐号的目录和权限
- PHP判断字符是否为字母/数字检测/password_hash密码加密
- JS与PHP互通的RSA加解密
- 在论坛中看到很多php新手朋友,苦于找不到好的文章或资料,对php无从下手。
- PHP面向对象构造函数说明
- PHP性能优化
- 在PhpStorm9中与Pi的xdebug进行调试
- zend framework2-在controller-action设置layout布局视图变量
- PHP函数补完:call_user_func[转]
- ContentProvider的自定义及SMS和CONTACTS的内容提供者
- Postfix邮件服务器的原理和postfix + dovecot配置,使用Thunderbird发送邮件和SMTP认证
- WIN7上快速架设一个可以用于Yii2开发调试的PHP运行环境 [ 2.0 版本 ]
- PHP中$_SERVER的详细参数与说明
- py科学绘图库matplotlib详细教程
- php大力力 [038节] 全栈工程师的含义