<?php
/**
*
* 出题引擎一枚
*
* @author Tourmaline
* @copyright (c) 2013, Unary Inc.
*/
class QuestionEngine {
/**
* 出题范围
* @var string $scope
*/
public $scope = array(1, 100);
/**
* 包含的运算符,含有多个则混合出题
* @var string $operators
*/
public $operators = '+-';
/**
* 运算次数
* @var int
*/
public $optTimes = 1;
public function generate() {
//按运算次数产生一组数值
start:
for ($index = 0; $index < $this->optTimes + 1; $index++) {
$elements[] = $this->randomValue();
}
$operatorType = strlen($this->operators); //有几个运算符供选择
//开始组装算式
$question = '';
for ($index = 0; $index < count($elements); $index++) {
$question.=' ' . $elements[$index] . ' '; //放一个数字进来
if ($index < count($elements) - 1)//如果不是最后一个数字的话,在后面加个运算符
$question.=substr($this->operators, mt_rand(0, $operatorType - 1), 1);
}
eval('$anwser = ' . $question . ';');
if ($anwser < 0) { //排除结果为负数的情况
$elements = array();
goto start; //需PHP5.3的支持
}
echo "$question= " . $anwser;
}
/**
* 产生一个范围内的随机值
*
* @return int
*/
protected function randomValue() {
return mt_rand($this->scope[0], $this->scope[1]);
}
}
测试
适合初学的标准三层架构,采用ajax,页面布局div+css符合w3c,用vs自带的sqlserver,免配置sqlserver,使用方便,里面共有5个项目,点击最外层的.sln直接可运行。网站采用asp.net 用户角色配置(membership,UserRoles),用户角色、权限可在asp.net配置里修改,注册,登陆均采用asp.net登陆控件,网站根据用户角色自定义sitemap,基本上
0
<?php include 'QuestionEngine.class.php'; $hello = new QuestionEngine(); $hello->generate(); ?> 结果:26 + 85 = 111
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号