最近要安排我为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. centos6性能监控软件

    常用软件在此下载 http://rpm.pbone.net/ http://pkgs.org/ collectl 显示cpu\disk\network的实时信息http://dl.fedoraproj ...

  2. 77. sqlserver 锁表解决方式

    select request_session_id spid, OBJECT_NAME(resource_associated_entity_id) tableName from sys.dm_tra ...

  3. rabbitMQ 的基本知识

    参考:  https://www.cnblogs.com/dwlsxj/p/RabbitMQ.html

  4. ajax 遍历json一维数组

    $.each(data,function(index,value){}data必须是Object类型index是数组的下标value可以是一个对象 function myonclick() { var ...

  5. VBA 连接文本的自定义函数(可用于数组公式)

    Function ConTxt(ParamArray args() As Variant) As VariantDim tmptext As Variant, i As Variant, cellv ...

  6. python内置函数 eval()、exec()以及complie()函数

    1.eval函数 eval() 函数用来执行一个字符串表达式,并返回表达式的值. eval(expression[, globals[, locals]]) 参数 expression -- 表达式. ...

  7. ABAP-年月期间搜索帮助

    selection-screen begin of block block1 with frame title text-. parameters:p_mon1 like s031-spmon, p_ ...

  8. 面试题-------SSL协议简介

    SSL协议简介 SSL简介 Secure Socket Layer,为Netscape所研发,用以保障在Internet上数据传输之安全,利用数据加密(Encryption)技术,可确保数据在网络上之 ...

  9. 修改rabbitmq Web UI 监控页面的端口

    在前几天工作中遇到一个问题,部署服务器,需要用rabbitmq自带的一个web UI监控组件,但是15672的端口没有对外映射.尝试了几种办法.开始修改rabbitmq.config,rabbitmq ...

  10. springMVC学习记录2-使用注解配置

    前面说了一下使用xml配置springmvc,下面再说说注解配置.项目如下: 业务很简单,主页和输入用户名和密码进行登陆的页面. 看一下springmvc的配置文件: <?xml version ...