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

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

GTK2的API Reference: 很不完整

PHP+Gtk实例(求24点)的更多相关文章

  1. C 语言实例 - 求两数最小公倍数

    C 语言实例 - 求两数最小公倍数 用户输入两个数,其这两个数的最小公倍数. 实例 - 使用 while 和 if #include <stdio.h> int main() { int ...

  2. C 语言实例 - 求两数的最大公约数

    C 语言实例 - 求两数的最大公约数 用户输入两个数,求这两个数的最大公约数. 实例 - 使用 for 和 if #include <stdio.h> int main() { int n ...

  3. JAVA 数组实例-求学生成绩的最大成绩,获取数组中的最大值、最小值

    实例: import java.util.*; //求学生最大成绩 public class Test{ public static void main(String[] args){ System. ...

  4. Mapreduce实例--求平均值

    求平均数是MapReduce比较常见的算法,求平均数的算法也比较简单,一种思路是Map端读取数据,在数据输入到Reduce之前先经过shuffle,将map函数输出的key值相同的所有的value值形 ...

  5. Java文件写入与读取实例求最大子数组

    出现bug的点:输入数组无限大: 输入的整数,量大: 解决方案:向文件中输入随机数组,大小范围与量都可以控制. 源代码: import java.io.BufferedReader; import j ...

  6. ASP.NET Core 6框架揭秘实例演示[24]:中间件的多种定义方式

    ASP.NET Core的请求处理管道由一个服务器和一组中间件组成,位于 "龙头" 的服务器负责请求的监听.接收.分发和最终的响应,针对请求的处理由后续的中间件来完成.中间件最终体 ...

  7. C#中按指定质量保存图片的实例代码 24位深度

     /// <summary>        /// 按指定的压缩质量及格式保存图片(微软的Image.Save方法保存到图片压缩质量为75)        /// </summary ...

  8. MapReduce实例——求平均值,所得结果无法写出到文件的错误原因及解决方案

    1.错误原因 mapreduce按行读取文本,map需要在原有基础上增加一个控制语句,使得读到空行时不执行write操作,否则reduce不接受,也无法输出到新路径. 2.解决方案 原错误代码 pub ...

  9. C 语言实例 -求分数数列1/2+2/3+3/5+5/8+...的前n项和

    程序分析:抓住分子与分母的变化规律:分子a:1,2,3,5,8,13,21,34,55,89,144...分母b:2,3,5,8,13,21,34,55,89,144,233...分母b把数赋给了分子 ...

随机推荐

  1. REST 风格

  2. AVL树Python实现

    # coding=utf-8 # AVL树Python实现 def get_height(node): return node.height if node else -1 def tree_mini ...

  3. 3.4 SpringBoot发送邮件

      spring官方提供了spring-boot-starter-mail来整合邮件发送功能,本质上还是利用了JavaMailSender类. 首先我们要在项目中引入相关依赖 <parent & ...

  4. node.js入门基础

    内容: 1.node.js介绍 2.node.js内置常用模块 3.node.js数据交互 一.node.js介绍 (1)node.js特点 与其他语言相比,有以下优点: 对象.语法和JavaScri ...

  5. 一、探索 Android Studio

    探索 Android Studio 本文内容 项目结构 界面 Gradle 构建系统 调试和分析工具 Android Studio 是基于 IntelliJ IDEA 的官方 Android 应用开发 ...

  6. 得到当前对象在不同大小的页面中的绝对位置,及冒泡cancelBubble

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. 为挂载到/home的RAID磁盘组扩容

    公司一台DELL服务器,安装的Ubuntu16.04系统,原来是6块1.2T的SAS盘做RAID-5挂载到/home,现在/home空间不够用了,需要扩容,再增加2块1.2T的盘.整个操作不复杂,但有 ...

  8. PHP图像 因其本身有错无法显示

    昨天终于将客户的一个网站迁移至虚拟主机上,满怀希望的敲入网址.唰的一声,网站很轻松的被打开了. 心里那个高兴啊~~~ 咦,怎么产品图片都没有显示出来.一块块都是空白.敲入img src对应的地址,看看 ...

  9. ABAP-动态创建DATABASE/FUNCTION(风险)

    警告:此程序仅供研究,请谨慎操作,切勿对系统标准数据表及功能函数进行测试(可能无法修复). 程序:EWUCINS REPORT EWUCINS MESSAGE-ID US NO STANDARD PA ...

  10. Winform 窗体关闭事件

    //窗体关闭前事件 private void FrmMain_FormClosing(object sender, FormClosingEventArgs e) { DialogResult res ...