{"id":18345736,"url":"https://github.com/hyperf/rpn","last_synced_at":"2025-04-06T08:31:51.798Z","repository":{"id":39653154,"uuid":"369141326","full_name":"hyperf/rpn","owner":"hyperf","description":"[READ ONLY] Reverse Polish Notation","archived":false,"fork":false,"pushed_at":"2025-02-05T12:25:35.000Z","size":20,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-17T12:07:37.588Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hyperf.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"open_collective":"hyperf","custom":"https://hyperf.wiki/#/zh-cn/donate"}},"created_at":"2021-05-20T08:46:17.000Z","updated_at":"2024-12-17T14:25:46.000Z","dependencies_parsed_at":"2024-11-21T03:29:01.935Z","dependency_job_id":null,"html_url":"https://github.com/hyperf/rpn","commit_stats":null,"previous_names":[],"tags_count":22,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperf%2Frpn","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperf%2Frpn/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperf%2Frpn/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hyperf%2Frpn/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hyperf","download_url":"https://codeload.github.com/hyperf/rpn/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247457323,"owners_count":20941900,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-05T21:09:19.069Z","updated_at":"2025-04-06T08:31:51.538Z","avatar_url":"https://github.com/hyperf.png","language":"PHP","readme":"# RPN - 逆波兰表示法\n\n`RPN` 是一种是由波兰数学家扬·武卡谢维奇1920年引入的数学表达式方式，在逆波兰记法中，所有操作符置于操作数的后面，因此也被称为后缀表示法。逆波兰记法不需要括号来标识操作符的优先级。\n\n```bash\ncomposer require hyperf/rpn\n```\n\n## RPN 逻辑\n\n基本逻辑\n\n- while 有输入\n  - 读入下一个符号X\n  - IF X是一个操作数\n    - 入栈\n  - ELSE IF X是一个操作符\n    - 有一个先验的表格给出该操作符需要n个参数\n    - IF堆栈中少于n个操作数\n      - （错误）用户没有输入足够的操作数\n  - Else，n个操作数出栈\n  - 计算操作符。\n  - 将计算所得的值入栈\n- IF栈内只有一个值\n  - 这个值就是整个计算式的结果\n- ELSE多于一个值\n  - （错误）用户输入了多余的操作数\n\n实例\n\n中缀表达式 `5 + ((1 + 2) * 4) - 3` 写作\n\n`5 1 2 + 4 * + 3 -`\n\n下表给出了该逆波兰表达式从左至右求值的过程，堆栈栏给出了中间值，用于跟踪算法。\n\n| 输入 | 操作     | 堆栈    | 注释                       |\n| ---- | -------- | ------- | -------------------------- |\n| 5    | 入栈     | 5       |                            |\n| 1    | 入栈     | 5, 1    |                            |\n| 2    | 入栈     | 5, 1, 2 |                            |\n| +    | 加法运算 | 5, 3    | 1, 2出栈，将结果 3 入栈    |\n| 4    | 入栈     | 5, 3, 4 |                            |\n| *    | 乘法运算 | 5, 12   | 3, 4 出栈，将结果 12 入栈  |\n| +    | 加法运算 | 17      | 5, 12 出栈，将结果 17 入栈 |\n| 3    | 入栈     | 17, 3   |                            |\n| -    | 减法运算 | 14      | 17, 3 出栈，将结果 14 入栈 |\n\n计算完成时，栈内只有一个操作数，这就是表达式的结果：14\n\n## 使用\n\n直接计算 RPN 表达式\n\n```php\n\u003c?php\nuse Hyperf\\Rpn\\Calculator;\n\n$calculator = new Calculator();\n$calculator-\u003ecalculate('5 1 2 + 4 * + 3 -', []); // '14'\n```\n\n设置计算精度\n\n```php\n\u003c?php\nuse Hyperf\\Rpn\\Calculator;\n\n$calculator = new Calculator();\n$calculator-\u003ecalculate('5 1 2 + 4 * + 3 -', [], 2); // '14.00'\n```\n\n设置变量\n\n```php\n\u003c?php\nuse Hyperf\\Rpn\\Calculator;\n\n$calculator = new Calculator();\n$calculator-\u003ecalculate('[0] 1 2 + 4 * + [1] -', [5, 10]); // '7'\n```\n\n### 中缀表达式转化为后缀表达式\n\n\u003e 暂时不支持使用变量 \n\n```php\n\u003c?php\nuse Hyperf\\Rpn\\Calculator;\n\n$calculator = new Calculator();\n$calculator-\u003etoRPNExpression('4 - 2 * ( 5 + 5 ) - 10'); // 4 2 5 5 + * - 10 -\n```\n","funding_links":["https://opencollective.com/hyperf","https://hyperf.wiki/#/zh-cn/donate"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhyperf%2Frpn","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhyperf%2Frpn","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhyperf%2Frpn/lists"}