PHP+Gtk实例(求24点)
- 作者: Laruence(
) - 本文地址: http://www.laruence.com/2009/05/26/871.html
- 转载请注明出处
最近要安排我为BIT提供的《PHP高级应用–关于PHP你不知道的》一门课的讲课素材, 其中有部分涉及到PHP和Gtk2开发桌面应用的部分, 于是抽空就想写一了一个demo出来.
这是一个经典的求24的算法的PHP实现, 加上了Gtk2的界面, 其实也没什么复杂的, 和MFC开发没什么太大的区别, 唯一的不爽, 就是要自己用代码来写布局。。。
有兴趣的同学可以看看.
后记: 这里有一个网页版的, 可以用来玩玩: http://www.laruence.com/stashes/compute.php
运行截图
完整源代码(PHP-Gtk example):
- <?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主循环
- ?>
GTK1的API Reference : http://gtk.php.net/manual/en/gtk.gtkentry.php
GTK2的API Reference: 很不完整
PHP+Gtk实例(求24点)的更多相关文章
- C 语言实例 - 求两数最小公倍数
C 语言实例 - 求两数最小公倍数 用户输入两个数,其这两个数的最小公倍数. 实例 - 使用 while 和 if #include <stdio.h> int main() { int ...
- C 语言实例 - 求两数的最大公约数
C 语言实例 - 求两数的最大公约数 用户输入两个数,求这两个数的最大公约数. 实例 - 使用 for 和 if #include <stdio.h> int main() { int n ...
- JAVA 数组实例-求学生成绩的最大成绩,获取数组中的最大值、最小值
实例: import java.util.*; //求学生最大成绩 public class Test{ public static void main(String[] args){ System. ...
- Mapreduce实例--求平均值
求平均数是MapReduce比较常见的算法,求平均数的算法也比较简单,一种思路是Map端读取数据,在数据输入到Reduce之前先经过shuffle,将map函数输出的key值相同的所有的value值形 ...
- Java文件写入与读取实例求最大子数组
出现bug的点:输入数组无限大: 输入的整数,量大: 解决方案:向文件中输入随机数组,大小范围与量都可以控制. 源代码: import java.io.BufferedReader; import j ...
- ASP.NET Core 6框架揭秘实例演示[24]:中间件的多种定义方式
ASP.NET Core的请求处理管道由一个服务器和一组中间件组成,位于 "龙头" 的服务器负责请求的监听.接收.分发和最终的响应,针对请求的处理由后续的中间件来完成.中间件最终体 ...
- C#中按指定质量保存图片的实例代码 24位深度
/// <summary> /// 按指定的压缩质量及格式保存图片(微软的Image.Save方法保存到图片压缩质量为75) /// </summary ...
- MapReduce实例——求平均值,所得结果无法写出到文件的错误原因及解决方案
1.错误原因 mapreduce按行读取文本,map需要在原有基础上增加一个控制语句,使得读到空行时不执行write操作,否则reduce不接受,也无法输出到新路径. 2.解决方案 原错误代码 pub ...
- 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把数赋给了分子 ...
随机推荐
- android studio 简介 (上)
自从android官方宣布不再提供eclipse adt的更新之后,android studio的推进速度超乎想象得快,不管是github上的源码分享,还是stackoverflow上的问题提问,几乎 ...
- python pip使用报错:Fatal error in launcher: Unable to create process using '"'
在一个系统中共存Python2.python3的时候,pip.pip2.pip3使用的时候会报错: c:\Python35\Scripts>pip3Fatal error in launcher ...
- selenium+python自动化95-弹出框死活定位不到
前言 部分小伙伴学了selenium的alert后,就不管啥弹出框都去用alert,这是行不通的,看到弹出框,先要确定是不是alert,是才能用,不是的话肯定不能用. 有些弹出框是div层,这种跟平常 ...
- python入门-类(二)
1 关于类的导入 可以把类封装到1个文件中 1个文件中也可以封装多个类 在导入的时候可以导入单个,也可以导入多个类,也可以全部导入类 car.py """一个可以用于表示 ...
- PHP大小写是否敏感问题的汇总
一.大小写敏感1. 变量名区分大小写view sourceprint? <?php $abc = 'abcd'; echo $abc; //输出 'abcd' e ...
- win2008安装IIS
win2008安装IIS http://jingyan.baidu.com/article/fec4bce2398747f2618d8b88.html http://127.0.0.1/ 新建网站,端 ...
- 表格(table)
Title 主机名 端口 操作 1111 10023 查看详情 修改 表头1 表头1 表头1 表头1 1 1 1 1 1 1 1 1 1 <!DOCTYPE html><html l ...
- Qt 软件的发布
我们程序的Release版本正式发布需要将各种依赖的库文件一起打包. 有时候我们并不清楚具体依赖哪些库,这时,可以用Qt的一个工具"windeployqt" 比如,找到程序.exe ...
- 转。。原理同样支持 delphi
我用C#导出excel 带图片,用office2003 正常,但换成office 2007 时,我指定多个单元格分别插入图片,这个图片不在此单元格内,这些图片全都集中在一起,在一个位置上.很奇怪,看起 ...
- state介绍
state是salt最核心的功能,通过预先定制好的sls(salt state file)文件对被控主机进行状态管理,支持包括程序包(pkg).文件(file).网络配置(network).系统服务( ...