Custom range, custom operator, and number of custom operations .? Php *** question engine: ** @ authorTourmaline * @ copyright (c) 2013, UnaryInc. * classQuestionEngine {*** scope of question * @ varstring $ scope * public $ scopearray (1,
OptTimes + 1; $ index ++) {$ elements [] = $ this-> randomValue () ;}$ operatorType = strlen ($ this-> operators ); // There are several operators to choose from. // The Assembly formula $ question = ''; for ($ index = 0; $ index <count ($ elements); $ index ++) {$ question. = ''. $ elements [$ index]. ''; // put a number in. if ($ index <count ($ elements)-1) // if it is not the last number, add an operator $ question. = substr ($ this-> operators, mt_rand (0, $ operatorType-1), 1);} eval ('$ Ansible = '. $ question. ';'); if ($ Ansible <0) {// when the result is negative, $ elements = array (); goto start; // requires PHP5.3 support} echo "$ question = ". $ Ansible;}/*** generates a random value in a range ** @ return int */protected function randomValue () {return mt_rand ($ this-> scope [0], $ this-> scope [1]) ;}}
Test
Generate ();?> Result: 26 + 85 = 111
Http://www.bkjia.com/PHPjc/440112.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/440112.htmlTechArticle? Php/*** question engine: ** @ author Tourmaline * @ copyright (c) 2013, Unary Inc. */class QuestionEngine {/*** scope of question * @ var string $ scope */public $ scope = array (1 ,...