变量中缀到前缀到后缀

2024-02-01

我在互联网上搜索了一个很好的实现,将变量表达式从中缀表示法转换为前缀和后缀,而不是数字表达式。我所做的所有搜索都没有成功。基本上我想看看 PHP 中是否有任何实现,这样我可以修改它以支持更多运算符,而不仅仅是 (-,*,+,=)。

例如转换:

a+b/c*(p/c)

同时保留变量名称,并且不必输入数字来评估它们。


我在 Miles Kaufmann 提供的 evalmath 类中找到了一个很好的实现:http://www.phpclasses.org/package/2695-PHP-Safely-evaluate-mathematical-expressions.html http://www.phpclasses.org/package/2695-PHP-Safely-evaluate-mathematical-expressions.html

后缀代码的中缀:

 // Convert infix to postfix notation
    function nfx($expr) {

        $index = 0;
        $stack = new EvalMathStack;
        $output = array(); // postfix form of expression, to be passed to pfx()
        $expr = trim(strtolower($expr));

        $ops   = array('+', '-', '*', '/', '^', '_');
        $ops_r = array('+'=>0,'-'=>0,'*'=>0,'/'=>0,'^'=>1); // right-associative operator?  
        $ops_p = array('+'=>0,'-'=>0,'*'=>1,'/'=>1,'_'=>1,'^'=>2); // operator precedence

        $expecting_op = false; // we use this in syntax-checking the expression
                               // and determining when a - is a negation

        if (preg_match("/[^\w\s+*^\/()\.,-]/", $expr, $matches)) { // make sure the characters are all good
            return $this->trigger("illegal character '{$matches[0]}'");
        }

        while(1) { // 1 Infinite Loop ;)
            $op = substr($expr, $index, 1); // get the first character at the current index
            // find out if we're currently at the beginning of a number/variable/function/parenthesis/operand
            $ex = preg_match('/^([a-z]\w*\(?|\d+(?:\.\d*)?|\.\d+|\()/', substr($expr, $index), $match);
            //===============
            if ($op == '-' and !$expecting_op) { // is it a negation instead of a minus?
                $stack->push('_'); // put a negation on the stack
                $index++;
            } elseif ($op == '_') { // we have to explicitly deny this, because it's legal on the stack 
                return $this->trigger("illegal character '_'"); // but not in the input expression
            //===============
            } elseif ((in_array($op, $ops) or $ex) and $expecting_op) { // are we putting an operator on the stack?
                if ($ex) { // are we expecting an operator but have a number/variable/function/opening parethesis?
                    $op = '*'; $index--; // it's an implicit multiplication
                }
                // heart of the algorithm:
                while($stack->count > 0 and ($o2 = $stack->last()) and in_array($o2, $ops) and ($ops_r[$op] ? $ops_p[$op] < $ops_p[$o2] : $ops_p[$op] <= $ops_p[$o2])) {
                    $output[] = $stack->pop(); // pop stuff off the stack into the output
                }
                // many thanks: http://en.wikipedia.org/wiki/Reverse_Polish_notation#The_algorithm_in_detail
                $stack->push($op); // finally put OUR operator onto the stack
                $index++;
                $expecting_op = false;
            //===============
            } elseif ($op == ')' and $expecting_op) { // ready to close a parenthesis?
                while (($o2 = $stack->pop()) != '(') { // pop off the stack back to the last (
                    if (is_null($o2)) return $this->trigger("unexpected ')'");
                    else $output[] = $o2;
                }
                if (preg_match("/^([a-z]\w*)\($/", $stack->last(2), $matches)) { // did we just close a function?
                    $fnn = $matches[1]; // get the function name
                    $arg_count = $stack->pop(); // see how many arguments there were (cleverly stored on the stack, thank you)
                    $output[] = $stack->pop(); // pop the function and push onto the output
                    if (in_array($fnn, $this->fb)) { // check the argument count
                        if($arg_count > 1)
                            return $this->trigger("too many arguments ($arg_count given, 1 expected)");
                    } elseif (array_key_exists($fnn, $this->f)) {
                        if ($arg_count != count($this->f[$fnn]['args']))
                            return $this->trigger("wrong number of arguments ($arg_count given, " . count($this->f[$fnn]['args']) . " expected)");
                    } else { // did we somehow push a non-function on the stack? this should never happen
                        return $this->trigger("internal error");
                    }
                }
                $index++;
            //===============
            } elseif ($op == ',' and $expecting_op) { // did we just finish a function argument?
                while (($o2 = $stack->pop()) != '(') { 
                    if (is_null($o2)) return $this->trigger("unexpected ','"); // oops, never had a (
                    else $output[] = $o2; // pop the argument expression stuff and push onto the output
                }
                // make sure there was a function
                if (!preg_match("/^([a-z]\w*)\($/", $stack->last(2), $matches))
                    return $this->trigger("unexpected ','");
                $stack->push($stack->pop()+1); // increment the argument count
                $stack->push('('); // put the ( back on, we'll need to pop back to it again
                $index++;
                $expecting_op = false;
            //===============
            } elseif ($op == '(' and !$expecting_op) {
                $stack->push('('); // that was easy
                $index++;
                $allow_neg = true;
            //===============
            } elseif ($ex and !$expecting_op) { // do we now have a function/variable/number?
                $expecting_op = true;
                $val = $match[1];
                if (preg_match("/^([a-z]\w*)\($/", $val, $matches)) { // may be func, or variable w/ implicit multiplication against parentheses...
                    if (in_array($matches[1], $this->fb) or array_key_exists($matches[1], $this->f)) { // it's a func
                        $stack->push($val);
                        $stack->push(1);
                        $stack->push('(');
                        $expecting_op = false;
                    } else { // it's a var w/ implicit multiplication
                        $val = $matches[1];
                        $output[] = $val;
                    }
                } else { // it's a plain old var or num
                    $output[] = $val;
                }
                $index += strlen($val);
            //===============
            } elseif ($op == ')') { // miscellaneous error checking
                return $this->trigger("unexpected ')'");
            } elseif (in_array($op, $ops) and !$expecting_op) {
                return $this->trigger("unexpected operator '$op'");
            } else { // I don't even want to know what you did to get here
                return $this->trigger("an unexpected error occured");
            }
            if ($index == strlen($expr)) {
                if (in_array($op, $ops)) { // did we end with an operator? bad.
                    return $this->trigger("operator '$op' lacks operand");
                } else {
                    break;
                }
            }
            while (substr($expr, $index, 1) == ' ') { // step the index past whitespace (pretty much turns whitespace 
                $index++;                             // into implicit multiplication if no operator is there)
            }

        } 
        while (!is_null($op = $stack->pop())) { // pop everything off the stack and push onto output
            if ($op == '(') return $this->trigger("expecting ')'"); // if there are (s on the stack, ()s were unbalanced
            $output[] = $op;
        }
        return $output;
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

变量中缀到前缀到后缀 的相关文章

  • ASCII“../”是 PHP 中指示目录遍历的唯一字节序列吗?

    我有一个 PHP 应用程序 它使用 GET参数来选择文件系统上的 JS CSS 文件 如果我拒绝输入字符串包含的所有请求 或者可见 7 位 ASCII 范围之外的字节 当路径传递到 PHP 的底层 基于 C 文件函数时 这是否足以防止父目录
  • 在 JQuery ui 自动完成中显示图像

    我有一个带有 JQuery ui 自动完成功能的脚本 可以完美运行 有一个显示用户名字和姓氏的搜索过程 但在我的数据库中 还有用户的图片 我想将其显示在带有名字和姓氏的建议中 数据库中pic包含图片url 剧本 function searc
  • 在 wampserver 2.2 上安装 php_imagick.dll PHP 扩展

    我使用的是 32 位操作系统的 Windows 7 我安装了 ImageMagick 6 8 7 Q16Link https www imagemagick org script download php windows我能够从命令行 转换
  • 增加内存限制时出现奇怪的错误

    我使用的是共享托管环境 PHP 的默认内存限制是 32M 我在 Concrete5 设置方面遇到一些问题 当我尝试登录 Concrete5 的管理面板时 出现内存限制错误Allowed memory size of 33554432 byt
  • jquery ajax加载后丢失CSS

    大家知道如何解决 load Ajax 请求后的 css 问题吗 例如 如果我想从网页加载 DIV 在我的 Ajax 请求之后 container load path to div div id 我丢失了与该 div 关联的所有 css 和脚
  • Php 转换 GMT 格式的时间

    我有这个字符串2012 06 27 16 17 06我想将其转换为 GMT 格式 我怎样才能做到这一点 多谢 Use gmdate http php net manual en function gmdate php 使用以下命令将当前日期
  • node-mongodb-native的插入性能

    我正在使用 MongoDB 测试 Node js 的性能 我知道其中每一个都很好 彼此独立 但我正在尝试一些测试来感受它们 我遇到了这个问题 但无法确定来源 问题 我正在尝试在单个 Node js 程序中插入 1 000 000 条记录 它
  • 间歇性 PHP 抽象类错误

    我已经为此奋斗了一段时间 但无法弄清楚 也许其他人也有 或者 Slim PHP Apache 等这里有更深层次的问题 在正常工作几个小时后 我的 Slim 安装将开始给出所有路线均如此 致命错误 类 Slim Collection 包含 1
  • 是否可以在 PHP 中使用 file_get_contents 来破坏 CSRF 令牌验证

    在每个会话的表单上使用令牌的 CSRF 预防方法是一种流行的方法 但是 我不明白这种令牌方式如何保护file get contentsPHP 可以获取跨域文件表单的内容 gt 它可以获取表单上的令牌并使用它 那么这种token方式是如何运作
  • Composer 安装要求

    我正在尝试将 Composer 安装到 Laravel 项目中 当我做的时候sudo composer install在项目目录中它显示了两个错误 Problem 1 Installation request for simplesoftw
  • 当第二个参数包含运算符号时,为什么 ltrim 会删除一个字符? [复制]

    这个问题在这里已经有答案了 If I do ltrim 53 34567 53 ltrim 53 34567 53 ltrim 53 34567 53 I get 4567作为结果而不是34567 这种行为的解释是什么 ltrim 53 3
  • Laravel:使用 Faker 播种多个独特的列

    介绍 怎么样 伙计们 我有一个关于模型工厂和多个独特列的问题 背景 我有一个名为 Image 的模型 该模型将语言支持存储在单独的模型中 图片文字 图片文字 has an image id栏 语言栏和文本栏 图片文字有一个约束MySQL那个
  • 如何检查 $row['column_name'] 是否返回空 php mysql

    我有一个带有列的表格 id name phone describe 当我从这个表中获取值时 我正在使用 row mysql fetch array query 现在我想检查是否 row describe 返回空值 如何查看php 您可以使用
  • 优化数据可视化 Web 应用程序的性能

    我正在重写 3 年前编写的数据可视化网络工具 从那时起 浏览器的 JavaScript 引擎变得更快 所以我正在考虑将部分工作从服务器转移到客户端 在页面上 数据在表格和地图 或图表 中可视化 它使用相同的数据 但以不同的方式 因此准备显示
  • AFNetworking 上传图片

    我看过一些例子 但我认为我的问题可能出在 PHP 中 我正在尝试使用 AFNetworking 将图像从 iPhone 上传到服务器 这是我的 obj c 代码 IBAction uploadButtonClicked id sender
  • PHP 致命错误:未找到“MongoClient”类

    我有一个使用 Apache 的网站 代码如下 当我尝试访问它时 我在 error log 中收到错误 PHP Fatal Error Class MongoClient not found 以下是可能错误的设置 但我认为没有错误 php i
  • Laravel 意外错误“类用户包含 3 个抽象方法...”

    在 Laravel 上编写我的身份验证应用程序时 我遇到了一个以前从未见过的错误 我已经集思广益了近一个小时来解决这个问题的原因 但仍然找不到解决方案 Error User 类包含 3 个抽象方法 因此必须声明为抽象方法或实现其余方法 Il
  • 如何更改 Ubuntu 14.04 上的 php-cli 版本?

    我是 Linux 新手 在篡改时破坏了一些 php 设置 如果我执行一个包含以下内容的 php 脚本 phpinfo 它显示 php 版本为 5 6 但通过命令行 如果我运行php v它返回 7 0 版本 我想让两个版本匹配 我怎样才能修复
  • 为什么 LinkedIn v2 Share API 在任何 v2/shares 端点上给出权限不足的错误?

    当我调用任何 v2 LinkedIn 共享 API 端点时 例如https api linkedin com v2 socialActions https api linkedin com v2 socialActions share UR
  • 简单的颜色变化

    我正在创建一个用户界面 用户可以在其中更改页面的颜色值 我想要的是获取分配给其背景颜色的值并将其变亮一定程度 我只是想获得一条亮点线 而不必每次都制作新图像 示例 用户将背景颜色设置为 ECECEC 现在我希望某个元素边框变成 F4F4F4

随机推荐

  • 绘制谷歌折线,线宽以米为单位设置

    我注意到 当您在谷歌地图中绘制折线时 您会设置它的宽度 以像素为单位 笔画宽度 有没有办法可以将其设置为米 不幸的是 我不知道有什么简单的方法可以做到这一点 该 API 似乎没有这方面的文档 我认为该功能不受支持 一个问题是 像素在 API
  • 将命令行变量传递到基于 WiXx 的 Windows Installer MSI

    我正在使用 WiX 构建 MSI 安装程序 并且使用WixUI Advanced 我的定义ApplicationFolder看起来像这样 遵循另一个SO答案中的建议 WiX 技巧和技巧 https stackoverflow com que
  • Sapper/Svelte:如何添加 Markdown 文件?

    我正在使用 Sapper 使用默认创建博客工兵模板汇总 https github com sveltejs sapper template rollup 在博客文件夹中 它确实提到了从 Markdown 文件生成数据 但我找不到该怎么做 我
  • 基本身份验证不是无状态的

    我正在尝试在 CakePHP API 中使用基本身份验证 我可以使用 jQuery 与之对话 运行此代码工作正常 ajax type GET url http username email protected cdn cgi l email
  • iOS + AVFoundation。相同手动曝光设置下的不同照片亮度

    我使用 AVFoundation 来拍照 问题是 即使曝光时间 ISO 和白平衡设置恒定 我也会得到不同亮度的照片 手电筒 闪光灯和所有可能的稳定功能均被禁用 此问题也出现在介绍如何使用相机的标准 Apple 应用程序中 https dev
  • 可变数量列表的交集

    我定义两个列表的交集如下 def intersect a b return list set a set b 对于三个参数 它看起来像 def intersect a b c return list set a set b set c 我可
  • 如何在 Windows 中从 python 3.7 降级到 3.6

    我正在尝试安装tensorflow 但python 3 7不支持它 所以我想在不使用anaconda的情况下获得python 3 6 那么有什么建议吗 我已经这样做过很多次了 我的第一个技巧是使用虚拟环境 https realpython
  • 从命令列表中调用 shell 命令,直到所有命令都完成

    我有我想要调用的 shell 命令列表 最多可同时运行四个进程 我的基本想法是将命令发送到 shell 直到 4 个命令处于活动状态 然后 该脚本通过查找公共字符串 例如 不断检查所有进程的进程计数 nohup scrapy 抓取 urlM
  • 是否可以使 Node 不需要“.js”扩展名进行导入?

    Node 现在内置了对导入的支持 这非常棒 但该支持要求您指定文件扩展名 这很烦人 我确信这是有道理的 可能与他们对 mjs扩展 但是有什么方法可以解决它并使import 像平常一样 工作 你可以离开 js off 您可以通过设置来实现这一
  • Docker compose postgresql 服务 - 在构建过程中无法创建用户和数据库?

    我已经在这上面浪费了一整天的时间 并且说我对本应简单的任务的不必要的复杂性没有留下深刻的印象 这是一种严重的轻描淡写 好吧 说完之后 我正在使用 docker machine docker compose postgresql 和 redi
  • 如何在 Oracle 中转义 regexp_replace?

    我正在为字符串创建一个小的replaceParam函数 并且希望能够转义替换 例如 G select regexp replace ABC ABC ABC XXX from dual leads to XXX XXX 但我希望能够逃脱替换
  • 为什么 boostuniform_int_distribution 采用闭范围(而不是半开范围,遵循常见的 C++ 用法)?

    标题说明了一切 甚至还有一个warning http www boost org doc libs 1 55 0 doc html boost random tutorial html在文档页面中 警告 与常见的 C 用法相反uniform
  • 打开 AVD 管理器时出错

    在模拟器上运行代码时 我遇到了常见问题 控制台消息是 2013 01 05 19 39 15 Doodlz Android Launch 2013 01 05 19 39 15 Doodlz adb is running normally
  • 如何将动画 GIF 写入 iOS 相机胶卷?

    如何将动画 GIF 写入 iOS 相机胶卷 我知道照片库应用程序无法播放动画 但例如我应该能够在发送电子邮件等时导入它 我试过了 UIImageWriteToSavedPhotosAlbum UIImage imageWithData se
  • 更改列表项选择的视图属性

    我有一个包含自定义行的 ListView 此自定义行具有以下 UI 元素 图像视图图像视图1 图像视图2 文本视图文本视图1 文本视图2 文本视图3 要求是每当选择列表行时都会发生以下更改 imageView1背景 颜色改变 imageVi
  • 重置到 Git 中的第一个提交?

    有没有什么相当于 root标志在rebase命令为reset命令 git reset root 假设我想重置到当前分支中的第一个提交 我是否必须手动挖掘历史记录并找到该提交的哈希值 或者是否有一种简单的方法来重置到第一个可用的提交 根提交
  • Xcode 是否有更好的更新系统?

    Xcode 4 0 1 几天前发布了 这意味着我再次下载 4 5 GB 的野兽来更新 有谁知道苹果是否计划推出更好的更新系统 这些天我在等待洪流 不像下载到 80 却失去连接那么令人沮丧
  • Laravel Composer 安装出现错误“您的锁定文件不包含兼容的软件包集,请运行 Composer update”

    我编写 Laravel 代码已经有一段时间了 目前 我尝试从 github 克隆一个项目并在本地进行编辑 我在项目目录中安装了 Composer 但未包含供应商文件夹 我尝试运行composer install但我给了我这个错误 Your
  • gitlab - 使用 access_token 推送到存储库

    我实现了 oauth2 Web 流程 以便从我的应用程序的用户获取 access token 使用 access token 我想执行以下操作 获取用户信息 为该用户创建一个存储库 将代码推送到此存储库 使用 git push 我已经成功获
  • 变量中缀到前缀到后缀

    我在互联网上搜索了一个很好的实现 将变量表达式从中缀表示法转换为前缀和后缀 而不是数字表达式 我所做的所有搜索都没有成功 基本上我想看看 PHP 中是否有任何实现 这样我可以修改它以支持更多运算符 而不仅仅是 例如转换 a b c p c