Home Backend Development PHP Tutorial 逆波兰表达式计算,该如何解决

逆波兰表达式计算,该如何解决

Jun 13, 2016 am 10:05 AM
array case data stack

逆波兰表达式计算
最近在整理资料时发现了一些以前收藏的有趣代码
计划逐步将他们移植到 PHP 供有兴趣的人参考

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->/** * 逆波兰表达式计算 * 中缀转后缀 **/function postfix($infix) {    $priority = array( //算符优先级        '+' => 1, '-' => 1,        '*' => 2, '/' => 2,        '(' => 0, ')' => 0,        '.' => 0, 0, 0, 0, 0, 0, 0, 0, 0 , 0, 0        );    $stack = array(); //符号栈    $data = array(); //数值栈     $i = $top = 0;    $last = -1;    $len = strlen($infix);    while($i= $priority[$infix{$i}]) {                    postfix_callback(array_shift($stack), $data);                }                array_unshift($stack, $infix{$i});                break;            case ')':                if($t != '') array_unshift($data, $t);                $t = '';                while($stack[0] != '(') {                    postfix_callback(array_shift($stack), $data);                }                array_shift($stack);                break;            default:                if($i > $last+1 && $t != '') {                    array_unshift($data, $t);                    $t = '';                }                $t .= $infix{$i};                $last = $i;                break;         }        $i++;    }    while($stack) {        postfix_callback(array_shift($stack), $data);    }    return $data[0];}/** * postfix 的工作函数 * 用于计算表达式的值 **/function postfix_callback($ch, &$data) {    $b = array_shift($data);    switch($ch) {        case '+':            $data[0] += $b;            break;        case '-':            $data[0] -= $b;            break;        case '*':            $data[0] *= $b;            break;        case '/':            $data[0] /= $b;            break;    }}
Copy after login
测试例
PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->echo postfix( '(2+3)*(3+4)' ); //out 35
Copy after login


------解决方案--------------------
先回贴在看
------解决方案--------------------
这个好东西。

------解决方案--------------------
前排占座, 板主老大给力.
------解决方案--------------------
前排占座, 板主老大给力.
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

An in-depth exploration of the structural aspects and functions of OP Stack An in-depth exploration of the structural aspects and functions of OP Stack Jan 18, 2024 pm 05:45 PM

OPStack is an open source blockchain framework released by Optimism Collective, the development group behind the Optimism Network. It is an important tool for both the Ethereum and Optimism communities. The main goal of OPStack is to strengthen the Optimism Network, providing key software tools to the Optimism Mainnet, as well as the upcoming Optimism Superchain and its governance model. By providing a developer-oriented environment, the core idea of ​​OPStack is to promote growth and innovation in the Ethereum space. It paves the way for cutting-edge developments and makes blockchain creation simpler. OPStac

Sort array using Array.Sort function in C# Sort array using Array.Sort function in C# Nov 18, 2023 am 10:37 AM

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

Java uses the empty() function of the Stack class to determine whether the stack is empty Java uses the empty() function of the Stack class to determine whether the stack is empty Jul 25, 2023 pm 10:25 PM

Java uses the empty() function of the Stack class to determine whether the stack is empty. The stack (Stack) is a common data structure that follows the first-in, last-out (FILO) principle. In Java, we can use the Stack class to implement the functionality of the stack. The Stack class provides a series of methods to operate the stack. One of the commonly used methods is the empty() function, which is used to determine whether the stack is empty. In Java, the Stack class is located in the java.util package. To use this class, you need

HMD Skyline gets a new color option and official magnetic case HMD Skyline gets a new color option and official magnetic case Aug 23, 2024 am 07:04 AM

When the HMD Skyline(available on Amazon for $499) was launched last month, it was released in two colors - Neon Pink and Twisted Black. They are now joined by a third color dubbed Blue Topaz. HMD Global has also announced an official case for the ph

How to use the array_combine function in PHP to combine two arrays into an associative array How to use the array_combine function in PHP to combine two arrays into an associative array Jun 26, 2023 pm 01:41 PM

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values ​​of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

Simple and clear method to use PHP array_merge_recursive() function Simple and clear method to use PHP array_merge_recursive() function Jun 27, 2023 pm 01:48 PM

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values ​​of the same keys, making the program design more flexible. array_merge

switch case judgment variable switch case judgment variable Feb 19, 2024 am 08:04 AM

Switchcase requires specific code examples to determine variables. In programming, we often need to perform different operations based on different variable values. The switchcase statement is a convenient structure that allows you to select different blocks of code for execution based on the value of a variable. The following is a specific code example that shows how to use the switchcase statement to determine different values ​​​​of variables: #includeintmain(){

What data is in the data folder? What data is in the data folder? May 05, 2023 pm 04:30 PM

The data folder contains system and program data, such as software settings and installation packages. Each folder in the Data folder represents a different type of data storage folder, regardless of whether the Data file refers to the file name Data or the extension. Named data, they are all data files customized by the system or program. Data is a backup file for data storage. Generally, it can be opened with meidaplayer, notepad or word.

See all articles