PHP+Gtk例項(求24點)
阿新 • • 發佈:2018-12-23
最近要安排我為BIT提供的《PHP高階應用–關於PHP你不知道的》一門課的講課素材, 其中有部分涉及到PHP和Gtk2開發桌面應用的部分, 於是抽空就想寫一了一個demo出來.
這是一個經典的求24的演算法的PHP實現, 加上了Gtk2的介面, 其實也沒什麼複雜的, 和MFC開發沒什麼太大的區別, 唯一的不爽, 就是要自己用程式碼來寫佈局。。。
有興趣的同學可以看看.
<?php /** * A 24 maker * @version 1.0.0 * @author laruence<laruence at yahoo.com.cn> * @copyright (c) 2009 http://www.laruence.com */ class TwentyFourCal extends GtkWindow { private $chalkboard; private $inputs; public $needle = 24; public $precision = '1e-6'; function TwentyFourCal() { parent::__construct(); $this->draw(); $this->show(); } /** * 畫窗體方法 */ public function draw() { $this->set_default_size(200, 200); $this->set_title("24計算器"); $mesg = new GtkLabel('Please input 4 integer(0-99):'); $this->chalkboard = new GtkLabel(); $this->inputs = $inputs = array( new GtkEntry(), new GtkEntry(), new GtkEntry(), new GtkEntry() ); /** * container */ $table = new GtkTable(4, 1, 0); $layout = array( 'left' => 0, 'right' => 1, 'top' => 0, 'bottom' => 1, ); $vBox = new GtkVBox(false, true); $vBox->pack_start($mesg); foreach ( $inputs as $input ) { $input->set_max_length(2); $table->attach($input, $layout['left'], $layout['right'], $layout['top']++, $layout['bottom']++); } $vBox->pack_start($table); $button = new GtkButton("Calculate"); $button->connect("clicked", array($this, "calculate")); $vBox->pack_start($this->chalkboard); $vBox->pack_start($button, true, false); $this->add($vBox); } public function show() { $this->show_all(); // 顯示窗體 } private function notice($mesg) { $this->chalkboard->set_text($mesg); } /** * 取得使用者輸入方法 */ public function calculate() { $operants = array(); $inputs = $this->inputs; foreach ($inputs as $input) { $number = $input->get_text(); if (!preg_match('/^\s*\d+\s*$/', $number)) { $this->notice('pleas input for integer(0-99)'); return ; } array_push($operants, $number); } $length = count($operants); try { $this->search($operants, 4); } catch (Exception $e) { $this->notice($e->getMessage()); return; } $this->notice('can\'t compute!'); return; } /** * 求24點演算法PHP實現 */ private function search($expressions, $level) { if ($level == 1) { $result = 'return ' . $expressions[0] . ';'; if ( abs(eval($result) - $this->needle) <= $this->precision) { throw new Exception($expressions[0]); } } for ($i=0;$i<$level;$i++) { for ($j=$i+1;$j<$level;$j++) { $expLeft = $expressions[$i]; $expRight = $expressions[$j]; $expressions[$j] = $expressions[$level - 1]; $expressions[$i] = '(' . $expLeft . ' + ' . $expRight . ')'; $this->search($expressions, $level - 1); $expressions[$i] = '(' . $expLeft . ' * ' . $expRight . ')'; $this->search($expressions, $level - 1); $expressions[$i] = '(' . $expLeft . ' - ' . $expRight . ')'; $this->search($expressions, $level - 1); $expressions[$i] = '(' . $expRight . ' - ' . $expLeft . ')'; $this->search($expressions, $level - 1); if ($expLeft != 0) { $expressions[$i] = '(' . $expRight . ' / ' . $expLeft . ')'; $this->search($expressions, $level - 1); } if ($expRight != 0) { $expressions[$i] = '(' . $expLeft . ' / ' . $expRight . ')'; $this->search($expressions, $level - 1); } $expressions[$i] = $expLeft; $expressions[$j] = $expRight; } } return false; } function __destruct() { Gtk::main_quit(); } } new TwentyFourCal(); Gtk::main(); //進入GTK主迴圈 ?>
GTK2的API Reference: 很不完整