如何用 PHP 製作計算器
問題:
建立一個 PHP計算器,可以計算輸入的簡單代數表達式由使用者使用標準代數符號,包括表達式
常見方法:
一個低效但臨時的解涉及替換表達式的字串表示形式中的表達式,例如:
for ($a=1; $a <p><strong>調車場演算法:</strong></p><p>A更有效的方法是使用 Shunting Yard 演算法。 </p><p><strong>實作:</strong></p><p><strong>終端表達式(值和運算子):</strong></p><pre class="brush:php;toolbar:false">abstract class TerminalExpression { protected $value = ''; public static function factory($value) { // Create terminal expressions based on the provided value (number, operator, or parenthesis) } abstract public function operate(Stack $stack); } class Number extends TerminalExpression { public function operate(Stack $stack) { return $this->value; } } class Operator extends TerminalExpression { protected $precidence = 0; protected $leftAssoc = true; public function getPrecidence() { return $this->precidence; } public function isLeftAssoc() { return $this->leftAssoc; } } class Addition extends Operator { protected $precidence = 4; } class Subtraction extends Operator { protected $precidence = 4; } class Multiplication extends Operator { protected $precidence = 5; } class Division extends Operator { protected $precidence = 5; } class Parenthesis extends TerminalExpression { protected $precidence = 7; public function isParenthesis() { return true; } }
堆疊實作:
class Stack { protected $data = []; public function push($element) { array_push($this->data, $element); } public function pop() { return array_pop($this->data); } }
數學類別(執行器):
class Math { protected $variables = []; public function evaluate($string) { $stack = $this->parse($string); return $this->run($stack); } public function parse($string) { // Tokenize expression $tokens = array_map('trim', preg_split('((\d+|\+|-|\(|\)|\*|/)|\s+)', $string, null, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE)); // Parse operators and parentheses using the Shunting Yard algorithm $output = new Stack(); $operators = new Stack(); foreach ($tokens as $token) { $expression = TerminalExpression::factory($token); if ($expression->isOperator()) { $this->parseOperator($expression, $output, $operators); } elseif ($expression->isParenthesis()) { $this->parseParenthesis($expression, $output, $operators); } else { $output->push($expression); } } // Pop remaining operators on stack and push to output while (($op = $operators->pop()) && $op->isOperator()) { if ($op->isParenthesis()) { throw new RuntimeException('Mismatched Parenthesis'); } $output->push($op); } return $output; } public function run(Stack $stack) { // Evaluate stack and return result while (($operator = $stack->pop()) && $operator->isOperator()) { $value = $operator->operate($stack); $stack->push(TerminalExpression::factory($value)); } return $operator ? $operator->render() : $this->render($stack); } protected function extractVariables($token) { if ($token[0] == '$') { $key = substr($token, 1); return isset($this->variables[$key]) ? $this->variables[$key] : 0; } return $token; } // ... }
使用此實現,您可以實現,您可以如下計算表達式:
$math = new Math(); $answer = $math->evaluate('(2 + 3) * 4'); // 20 $answer = $math->evaluate('1 + 2 * ((3 + 4) * 5 + 6)'); // 83 $answer = $math->evaluate('(1 + 2) * (3 + 4) * (5 + 6)'); // 231 $math->registerVariable('a', 4); $answer = $math->evaluate('($a + 3) * 4'); // 28
以上是如何使用 Shunting-Yard 演算法建立 PHP 計算器?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Laravel使用其直觀的閃存方法簡化了處理臨時會話數據。這非常適合在您的應用程序中顯示簡短的消息,警報或通知。 默認情況下,數據僅針對後續請求: $請求 -

PHP客戶端URL(curl)擴展是開發人員的強大工具,可以與遠程服務器和REST API無縫交互。通過利用Libcurl(備受尊敬的多協議文件傳輸庫),PHP curl促進了有效的執行

Laravel 提供简洁的 HTTP 响应模拟语法,简化了 HTTP 交互测试。这种方法显著减少了代码冗余,同时使您的测试模拟更直观。 基本实现提供了多种响应类型快捷方式: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

您是否想為客戶最緊迫的問題提供實時的即時解決方案? 實時聊天使您可以與客戶進行實時對話,並立即解決他們的問題。它允許您為您的自定義提供更快的服務

Laravel的服務容器和服務提供商是其架構的基礎。 本文探討了服務容器,詳細信息服務提供商創建,註冊,並通過示例演示了實際用法。 我們將從OVE開始

文章討論了PHP 5.3中介紹的PHP中的晚期靜態結合(LSB),允許靜態方法的運行時間分辨率調用以更靈活的繼承。 LSB的實用應用和潛在的觸摸

PHP日誌記錄對於監視和調試Web應用程序以及捕獲關鍵事件,錯誤和運行時行為至關重要。它為系統性能提供了寶貴的見解,有助於識別問題並支持更快的故障排除


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

MinGW - Minimalist GNU for Windows
這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

SublimeText3漢化版
中文版,非常好用

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SublimeText3 Linux新版
SublimeText3 Linux最新版