php gtk php7,PHP+Gtk实例(求24点)

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

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

有兴趣的同学可以看看.

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

fe1558821ff0fa0acecd8e808112f159.png运行截图

/**

* A 24 maker

* @version 1.0.0

* @author laruence

* @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

for ($j=$i+1;$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主循环

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PHP写得及时聊天工具,结构设计的很好,有源码。是学习PHP的好资料 JeCat-Jabber(简称:JJ) 是一款完全由 PHP 开发的即时通讯软件。JJ 采用 Gtk2 图像界面库来实现用户界面。顾名思义 JJ 使用 Jabber 协议(XMPP) ,因此可以 和 包括 GTalk 在内的 其它任何 XMPP 即时通讯软件 聊天。 JJ 项目的初衷在于 演示 桌面窗口 和 PHP 多任务处理,这两大 在长期在 PHP程序员 眼中 几乎是“不可能”的任务。 同时 也是 PHP框架 JCAT 的演示项目,JCAT 是一款同时 支持 Web 和 桌面 的 PHP框架,按照计划 将在稍后 发布。 JJ 的特点 > 跨平台。JJ 在Linxu 环境下 开发,在 Windows 平台上也一样可以正常使用。 > 实用 标准的 XMPP协议,能够与其它的 Jabber 软件互相通讯,例如 Google GTalk、Pidgin、PSI、Spark、Pandion,以及其它的 Jabber网页聊天窗口 > 可更换界面皮肤,皮肤样式文件 采用 类似 CSS 的语法,便于美工独立工作。 > 纯 PHP 实现,从 通讯协议 到 图形界面,全部都由 PHP 开发,如果你正好是一名 PHP 程序员,你可以驾轻就熟地在 JJ 之上进行二次开发。 > 可整合到你的网站中。JJ 近期的完善 会使 JJ 更容易地 为你的网站所有,让你的网站 也可以有像 淘宝旺旺 那样的 专用聊天工具 [ PHP 图像界面 ] JJ 采用 Gtk图像界面库,Gtk库 中 提供了丰富的 图形界面窗体(Widget),以及灵活方便的 窗体布局方式。 Gtk 本身 被广泛应用在 Linux 平台上,Linux 最著名 的桌面环境之一 Gnome 即是众多 Gtk软件 的集中展示。 PHP-Gtk2 将 Gtk 窗体绑定到 PHP 语言中,允许程序员 通过 PHP语言 来创建、显示、销毁 Gtk 窗体。PHP-Gtk 项目由 PHP创始人 亲自负责,并且自项目创立一来 一直持续发展,目前已经支持 最新的 Gtk2。 [ PHP 多任务处理 ] PHP 直到 5.2都没有提供 稳定可靠的 多线程特性。 但 PHP 仍然有能力进行多任务处理。 多线程 因为涉及到 线程安全、线程同步 等 困难,在 支持线程的语言中,也常常建议尽量避免使用。 Linux 的多路复用 是 更稳定和安全的 多任务模式,从 PHP5.0 开始 多路复用 支持 Windows 平台。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值