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把数赋给了分子 ...
随机推荐
- 小朋友学C语言(7)
数组 一.数组简介 C 语言支持数组数据结构,它可以存储一个固定大小的相同类型元素的顺序集合.数组是用来存储一系列数据,但它往往被认为是一系列相同类型的变量. 数组的声明并不是声明一个个单独的变量,比 ...
- 听听八年阿里架构师怎样讲述Dubbo和Spring Cloud微服务架构
转自:https://baijiahao.baidu.com/s?id=1600174787011483381&wfr=spider&for=pc 微服务架构是互联网很热门的话题,是互 ...
- 搭建Discuz论坛
准备 LAMP 环境 LAMP 是 Linux.Apache.MySQL 和 PHP 的缩写,是 Discuz 论坛系统依赖的基础运行环境.我们先来准备 LAMP 环境 安装 MySQL 使用 yum ...
- Java的this和super总结
内容: 1.this和super作用 2.继承关系图 1.this和super作用 this和super的作用: this:区分本类中的成员变量和局部变量同名的情况,代指本类 super:区分子类中的 ...
- Python之模块(二)
1.subprocess模块 2.loggin模块 很多程序都有记录日志的需求,并且日志中包含的信息即有正常的程序访问日志,还可能有错误.警告等信息输出,python的logging模块提供了标准的日 ...
- centos7.3给squid搭建代理服务器添加认证apache
证使用浏览器打开 然后输入用户名和密码的方式 所以使用基于web服务的apache的 htpasswd 1 创建用户 设置密码 htpasswd -cd /etc/squid/passwords te ...
- 0_Simple__simpleTexture + 0_Simple__simpleTextureDrv
使用纹理引用来旋转图片,并在使用了静态编译和运行时编译两种环境. ▶ 源代码:静态编译 #include <stdio.h> #include <windows.h> #inc ...
- Redis 通用操作1
01, 设置值 => set key value 01.1, 设置值并添加有效期 => set key value ex 秒数 或者 set key value px 毫秒数 01.2, ...
- 17.在Action获取Scope对象
转自:https://wenku.baidu.com/view/84fa86ae360cba1aa911da02.html 引言:在前面的Action操作中,关键就是Action中的exectue方法 ...
- 自动选择最佳特征进行分类-SVM (Halcon)
HALCON12里的example,classify_pills_auto_select_features.hdev. 执行流程: 1.选取相关特征(本例选取color和region组的所有特征)(本 ...