四则运算表达式(中缀表达式)转换为后缀表达式:
如中缀表达式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 . ' '; } //如果是左括号则入栈 else if($str == '('){ $stack[] = $str; } //如果是右括号则将最近的左括号之前的所有数据出栈 else if($str == ')'){ while($arrPop = array_pop($stack)){ if($arrPop == '('){ break; } $newStrList .= $arrPop . ' '; } } //如果是加减乘除则判断与栈顶符号优先级 else if(in_array($str, array('+', '-', '*', '/')) && count($stack) > 0){ $key = (count($stack) - 1); if(in_array($stack[$key], array('+', '-', '*', '/'))){ if(checkPriority($str, $stack[$key]) != 1){ for($i=$key; $i>=0; $i--){ if($stack[$i] == '('){ break; } $newStrList .= $stack[$i] . ' '; 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 '+': case '-': if($operatorB == '+' || $operatorB == '-'){ return 0; }else if($operatorB == '*' || $operatorB == '/'){ return -1; } break; case '*': case '/': if($operatorB == '+' || $operatorB == '-'){ return 1; }else if($operatorB == '*' || $operatorB == '/'){ return 0; } break; default: exit('error'); } } $stack = array(); $newStrList = ''; $strList = '9 + ( 3 - 1 ) * 3 + 10 / 2'; $strList = explode(' ', $strList); foreach($strList as $str){ if($str == ' '){ continue; } suffix($str, $stack, $newStrList); } $stack = array_reverse($stack); foreach($stack as $s){ $newStrList .= $s . ' '; } echo $newStrList;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
