自定义范围,自定义运算符,自定义运算次数。太牛逼了
- /**
- * Description of QuestionEngine
- * 出题引擎一枚
- *
- * @author lyc
- * @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 optTimes + 1; $index++) {
- $elements[] = $this->randomValue();
- }
- $operatorType = strlen($this->operators); //有几个运算符供选择
- //开始组装算式
- $question = '';
- for ($index = 0; $index $question.=' ' . $elements[$index] . ' '; //放一个数字进来
- if ($index $question.=substr($this->operators, mt_rand(0, $operatorType - 1), 1);
- }
-
- eval('$anwser = ' . $question . ';');
- if ($anwser $elements = array();
- goto start; //需PHP5.3的支持
- }
-
- echo "$question= " . $anwser;
- }
-
- /**
- * 产生一个范围内的随机值
- *
- * @return int
- */
- protected function randomValue() {
- return mt_rand($this->scope[0], $this->scope[1]);
- }
-
- }
复制代码
- include 'QuestionEngine.class.php';
- $hello = new QuestionEngine();
- $hello->generate();
- ?>
-
- 结果:26 + 85 = 111
复制代码
|
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
-
2024-10-22 09:46:29
-
2024-10-13 13:53:41
-
2024-10-12 12:15:51
-
2024-10-11 22:47:31
-
2024-10-11 19:36:51
-
2024-10-11 15:50:41
-
2024-10-11 15:07:41
-
2024-10-11 14:21:21
-
2024-10-11 12:59:11
-
2024-10-11 12:17:31