{
}?>
简单四则运算习题数量
数值范围
{$open1 = fopen("config.txt","w+" ); //config.txt用于存放用户在表单输入的参数。
fwrite($open1,$_REQUEST["test1"]."\r\n".$_REQUEST["test2"]);fclose($open1);$open2 = fopen("time.txt","w+" ); //time.txt用于更新用户当前答题数量。
fwrite($open2,"0");fclose($open2);$open3 = fopen("exercise.txt","w+" ); //exercise.txt用于存放本次程序所出试题,同时用于判断是否有重复。
fclose($open3);header("Refresh:0;url = main.php"); //配置完成后跳转到主程序页面。
}?>
$open0 = file("config.txt");$range = chop($open0[1]); //读取随机数大小上限。
if(!(isset($_REQUEST["answer"])) && $flag) //出现回答后不再运行。
{include("exercise.php");$object = newexercise();$object->randomNumber($range);$flag = false;
}$open1 = fopen("exercise.txt",‘r‘); //读取exercise.txt的最后一行数据,即当前试题。
while($buf = fgets($open1))
{$res = $buf;
}fclose($open1);echo $res;if(isset($_REQUEST["answer"])) //输出答案和输入结果。
{echo file_get_contents("result.txt")."
"."您的结果为:".$_REQUEST["answer"]."
";
}else{?>
}if(isset($_REQUEST["answer"])) //输入结果后进行判断。
{
judge($_REQUEST["answer"]);
}function judge($answer)
{$result = file_get_contents("result.txt");if($result == $answer)
{echo "结果正确 ";
}else{echo "结果错误 ";
}$open2 = file("config.txt");$timeMax = chop($open2[0]); //读取习题数量。
$open3 = file("time.txt");$time = chop($open3[0]); //读取当前为第几题。
if($time < $timeMax - 1) //习题未出完时,当前题数加一,更新time.txt,重新运行main.txt
{echo "(第".($time + 1)."/".($timeMax)."题)";$time++;$open3 = fopen("time.txt","w+");fwrite($open3,"$time");fclose($open3);?>
}else //否则跳转到退出界面。
{echo "答题完毕。";header("Refresh:3;url = quit.php");
}
}<?PHPclassexercise
{public $numberA;public $numberB;public $numberC;public $result;public functionexercise()
{$this->numberA = null;$this->numberB = null;$this->result = null;
}public function randomFormula($a)
{$symbolA = rand(0,3);$symbolB = rand(0,3);$this->numberA = rand(0,$a);$this->numberB = rand(0,$a);$this->numberC = rand(0,$a);$openA = fopen("exercise.txt","a");$openB = fopen("result.txt","w+");fwrite($openA,$this->numberA.symbol($symbolA).$this->numberB.symbol($symbolB).$this->numberC." = "."\r\n");$this->result = 0;fwrite($openB,$this->result);fclose($openA);fclose($openB);
}public function randomNumber($a)
{$case2 = "0";$symbol = rand(0,3);$this->numberA = rand(0,$a);$this->numberB = rand(0,$a);$openA = fopen("exercise.txt","a");$openB = fopen("result.txt","w+");if($symbol == 0)
{$this->result = $this->numberA + $this->numberB;$case1 = $this->numberA." + ".$this->numberB." = "."\r\n";$case2 = $this->numberB." + ".$this->numberA." = "."\r\n";if(isRepeat($openA,$case1,$case2))
{fwrite($openA,$this->numberA." + ".$this->numberB." = "."\r\n");
}
}if($symbol == 1)
{$this->numberB = rand(0,$this->numberA);$case1 = $this->numberA." - ".$this->numberB." = "."\r\n";$this->result = $this->numberA - $this->numberB;if(isRepeat($openA,$case1,$case2))
{fwrite($openA,$this->numberA." - ".$this->numberB." = "."\r\n");
}
}if($symbol == 2)
{$case1 = $this->numberA." * ".$this->numberB." = "."\r\n";$case2 = $this->numberB." * ".$this->numberA." = "."\r\n";$this->result = $this->numberA * $this->numberB;if(isRepeat($openA,$case1,$case2))
{fwrite($openA,$this->numberA." * ".$this->numberB." = "."\r\n");
}
}if($symbol == 3)
{$this->numberB = rand(1,$a);$this->numberA = rand(1,$this->numberB);$case1 = $this->numberA." / ".$this->numberB." = "."\r\n";$gcd = getGreatestCommonDivisor($this->numberA,$this->numberB);$this->result = ($this->numberA / $gcd)."/".($this->numberB / $gcd);if($this->result == "1/1")
{$this->result = "1";
}if(isRepeat($openA,$case1,$case2))
{fwrite($openA,$this->numberA." / ".$this->numberB." = "."\r\n");
}
}fwrite($openB,$this->result);fclose($openA);fclose($openB);
}
}function symbol($a)
{$symbol = "";switch($a)
{case 0:
$symbol = " + ";break;case 1:
$symbol = " - ";break;case 2:
$symbol = " * ";break;case 3:
$symbol = " / ";break;
}return $symbol;
}function isRepeat($open1,$case1,$case2)
{while($buf = fgets($open1))
{if($buf == $case1 || $buf == $case2)
{header("Refresh:0;url = main.php");return false;
}else{continue;
}
}return true;
}function getGreatestCommonDivisor($numberA,$numberB)
{$n = 0;while($numberB > 0)
{$n = $numberA % $numberB;$numberA = $numberB;$numberB = $n;
}return $numberA;
}