search
Homephp教程PHP源码四则运算表达式转换为后缀表达式

四则运算表达式转换为后缀表达式

May 25, 2016 pm 04:58 PM
ArithmeticexpressionConvert

四则运算表达式(中缀表达式)转换为后缀表达式:
如中缀表达式9 + ( 3 - 1 ) * 3 + 10 / 2,对应的后缀表达式是9 3 1 - 3 * + 10 2 / +
PS:逻辑实现了,要下班了,代码有些乱,明天再来优化。

<?php
function suffix($str, &$stack, &$newStrList){

//如果是数字则输出
if(is_numeric($str)){
 $newStrList .= $str . &#39; &#39;;
}
//如果是左括号则入栈
 else if($str == &#39;(&#39;){
 $stack[] = $str;
}
//如果是右括号则将最近的左括号之前的所有数据出栈
 else if($str == &#39;)&#39;){
 while($arrPop = array_pop($stack)){
 if($arrPop == &#39;(&#39;){
break;
}
 $newStrList .= $arrPop . &#39; &#39;;
}
}
//如果是加减乘除则判断与栈顶符号优先级
 else if(in_array($str, array(&#39;+&#39;, &#39;-&#39;, &#39;*&#39;, &#39;/&#39;)) && count($stack) > 0){
 $key = (count($stack) - 1);
 if(in_array($stack[$key], array(&#39;+&#39;, &#39;-&#39;, &#39;*&#39;, &#39;/&#39;))){
 if(checkPriority($str, $stack[$key]) != 1){
 for($i=$key; $i>=0; $i--){
 if($stack[$i] == &#39;(&#39;){
break;
}
 $newStrList .= $stack[$i] . &#39; &#39;;
unset($stack[$i]);
 $stack = array_values($stack);
}
 $stack[] = $str;
}else{
 $stack[] = $str;
}
}else{
 $stack[] = $str;
}
}else{
 $stack[] = $str;
}
}

/**
 * 判断运算符的优先级
 * @param $operatorA
 * @param $operatorB
 * @return A大于B返回1,A等于B返回0,A小于B返回-1
*/
function checkPriority($operatorA, $operatorB){
switch($operatorA){
 case &#39;+&#39;:
 case &#39;-&#39;:
 if($operatorB == &#39;+&#39; || $operatorB == &#39;-&#39;){
 return 0;
 }else if($operatorB == &#39;*&#39; || $operatorB == &#39;/&#39;){
 return -1;
}
break;
 case &#39;*&#39;:
 case &#39;/&#39;:
 if($operatorB == &#39;+&#39; || $operatorB == &#39;-&#39;){
 return 1;
 }else if($operatorB == &#39;*&#39; || $operatorB == &#39;/&#39;){
 return 0;
}
break;
default:
exit(&#39;error&#39;);
}
}

$stack = array();
$newStrList = &#39;&#39;;
$strList = &#39;9 + ( 3 - 1 ) * 3 + 10 / 2&#39;;
$strList = explode(&#39; &#39;, $strList);
foreach($strList as $str){
 if($str == &#39; &#39;){
continue;
}
 suffix($str, $stack, $newStrList);
}
$stack = array_reverse($stack);
foreach($stack as $s){
 $newStrList .= $s . &#39; &#39;;
}
echo $newStrList;
Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.