最近要安排我为BIT提供的《PHP高级应用–关于PHP你不知道的》一门课的讲课素材, 其中有部分涉及到PHP和Gtk2开发桌面应用的部分, 于是抽空就想写一了一个demo出来.

这是一个经典的求24的算法的PHP实现, 加上了Gtk2的界面, 其实也没什么复杂的, 和MFC开发没什么太大的区别, 唯一的不爽, 就是要自己用代码来写布局。。。

有兴趣的同学可以看看.

后记: 这里有一个网页版的, 可以用来玩玩: http://www.laruence.com/stashes/compute.php

运行截图

运行截图

完整源代码(PHP-Gtk example):

  1.   <?php
  2. /**
  3. * A 24 maker
  4. * @version 1.0.0
  5. * @author laruence<laruence at yahoo.com.cn>
  6. * @copyright (c) 2009 http://www.laruence.com
  7. */
  8.  
  9. class TwentyFourCal extends GtkWindow {
  10.      private $chalkboard;
  11.      private $inputs;
  12.      public $needle = 24;
  13.      public $precision = '1e-6';
  14.  
  15.      function TwentyFourCal() {
  16.           parent::__construct();
  17.           $this->draw();
  18.           $this->show();
  19.      }
  20.  
  21.      /**
  22.       * 画窗体方法
  23.       */
  24.      public function draw() {
  25.           $this->set_default_size(200, 200);
  26.           $this->set_title("24计算器");
  27.  
  28.           $mesg = new GtkLabel('Please input 4 integer(0-99):');
  29.           $this->chalkboard = new GtkLabel();
  30.  
  31.           $this->inputs = $inputs = array(
  32.                new GtkEntry(),
  33.                new GtkEntry(),
  34.                new GtkEntry(),
  35.                new GtkEntry()
  36.           );
  37.  
  38.           /**
  39.            * container
  40.            */
  41.           $table = new GtkTable(4, 1, 0);
  42.           $layout = array(
  43.                'left' => 0,
  44.                'right' => 1,
  45.                'top'     => 0,
  46.                'bottom' => 1,
  47.           );
  48.  
  49.           $vBox = new GtkVBox(false, true);
  50.           $vBox->pack_start($mesg);
  51.  
  52.           foreach ( $inputs as $input ) {
  53.                $input->set_max_length(2);
  54.                $table->attach($input, $layout['left'], $layout['right'],
  55.                     $layout['top']++, $layout['bottom']++);
  56.           }
  57.  
  58.           $vBox->pack_start($table);
  59.           $button = new GtkButton("Calculate");
  60.           $button->connect("clicked", array($this, "calculate"));
  61.           $vBox->pack_start($this->chalkboard);
  62.           $vBox->pack_start($button, true, false);
  63.  
  64.           $this->add($vBox);
  65.      }
  66.  
  67.      public function show() {
  68.           $this->show_all(); // 显示窗体
  69.      }
  70.  
  71.      private function notice($mesg) {
  72.           $this->chalkboard->set_text($mesg);
  73.      }
  74.  
  75.      /**
  76.       * 取得用户输入方法
  77.       */
  78.      public function calculate() {
  79.           $operants = array();
  80.           $inputs = $this->inputs;
  81.           foreach ($inputs as $input) {
  82.                $number = $input->get_text();
  83.                if (!preg_match('/^\s*\d+\s*$/', $number)) {
  84.                     $this->notice('pleas input for integer(0-99)');
  85.                     return ;
  86.                }
  87.                array_push($operants, $number);
  88.           }
  89.           $length = count($operants);
  90.           try {
  91.                $this->search($operants, 4);
  92.           } catch (Exception $e) {
  93.                $this->notice($e->getMessage());
  94.                return;
  95.           }
  96.           $this->notice('can\'t compute!');
  97.           return;
  98.      }
  99.  
  100.      /**
  101.       * 求24点算法PHP实现
  102.       */
  103.      private function search($expressions, $level) {
  104.           if ($level == 1) {
  105.                $result = 'return ' . $expressions[0] . ';';
  106.                if ( abs(eval($result) - $this->needle) <= $this->precision) {
  107.                     throw new Exception($expressions[0]);
  108.                }
  109.           }
  110.           for ($i=0;$i<$level;$i++) {
  111.                for ($j=$i+1;$j<$level;$j++) {
  112.                     $expLeft = $expressions[$i];
  113.                     $expRight = $expressions[$j];
  114.                     $expressions[$j] = $expressions[$level - 1];
  115.  
  116.                     $expressions[$i] = '(' . $expLeft . ' + ' . $expRight . ')';
  117.                     $this->search($expressions, $level - 1);
  118.  
  119.                     $expressions[$i] = '(' . $expLeft . ' * ' . $expRight . ')';
  120.                     $this->search($expressions, $level - 1);
  121.  
  122.                     $expressions[$i] = '(' . $expLeft . ' - ' . $expRight . ')';
  123.                     $this->search($expressions, $level - 1);
  124.  
  125.                     $expressions[$i] = '(' . $expRight . ' - ' . $expLeft . ')';
  126.                     $this->search($expressions, $level - 1);
  127.  
  128.                     if ($expLeft != 0) {
  129.                          $expressions[$i] = '(' . $expRight . ' / ' . $expLeft . ')';
  130.                          $this->search($expressions, $level - 1);
  131.                     }
  132.  
  133.                     if ($expRight != 0) {
  134.                          $expressions[$i] = '(' . $expLeft . ' / ' . $expRight . ')';
  135.                          $this->search($expressions, $level - 1);
  136.                     }
  137.                     $expressions[$i] = $expLeft;
  138.                     $expressions[$j] = $expRight;
  139.                }
  140.           }
  141.           return false;
  142.      }
  143.  
  144.      function __destruct() {
  145.           Gtk::main_quit();
  146.      }
  147. }
  148.  
  149. new TwentyFourCal();
  150. Gtk::main(); //进入GTK主循环
  151. ?>

 

GTK1的API Reference : http://gtk.php.net/manual/en/gtk.gtkentry.php

GTK2的API Reference: 很不完整

posted on 2018-01-11 10:07  SoftBlue  阅读(543)  评论(0编辑  收藏  举报