1.设计思路:
  在index.php中建立两个表单,有两个提交,一个跳转到fourArithmeticOperation.php,这里保存用户输入的参数到config.txt中,留给main函数调出。界面跳转到main.php。main.php执行简单四则运算。main.php中调用exercise类中的randomNumber函数,将生成的习题和结果写入到txt,main.php再读出显示给用户,待用户输入结果后,再输出判断结果。并将当前执行次数写入到time.txt。然后再次执行main.php。当time中的参数达到用户定制数量要求时,界面跳转到quit.php中,此处有一个按钮,点击界面跳转到load.php,执行下载exercise.txt文件。
  另一个提交,跳转到multitermOperation.php,其思路与上面几乎相同,差别在main1.php调用的为exercise类中的randomFormula函数。
2.源代码
index.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<form method = "post" action = "fourArithmeticOperation.php">
简单四则运算 <input type = "submit" name = "continue" value = "简单四则运算"></form>
<form method = "post" action = "multitermOperation.php">
多项运算 <input type = "submit" name = "continue" value = "多项运算"></form>
<?php
if(isset($_REQUEST["continue"]))
{
}
?>
</html> fourArithmeticOperation.php
<!DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8">
<title></title>
</head>
<form method = "get" action = "fourArithmeticOperation.php">
简单四则运算 <br>
习题数量 <input type = "text" name = "test1" > <br>
数值范围 <input type = "text" name = "test2" > <br>
<input type = "submit"></form>
<body>
<?php
if(isset($_REQUEST["test1"]) && (isset($_REQUEST["test2"])))
{
$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"); //配置完成后跳转到主程序页面。
}
?>
</body>
</html> multitermOperation.php
<!DOCTYPE html>
<html>
<head>
<meta charset = "UTF-8">
<title></title>
</head>
<form method = "get" action = "multitermOperation.php">
多项运算 <br>
习题数量 <input type = "text" name = "test3" > <br>
数值范围 <input type = "text" name = "test4" > <br>
<input type = "submit"></form>
<body>
<?php
if(isset($_REQUEST["test3"]) && (isset($_REQUEST["test4"])))
{
$open1 = fopen("config.txt","w+" ); //config.txt用于存放用户在表单输入的参数。
fwrite($open1,$_REQUEST["test3"]."\r\n".$_REQUEST["test4"]);
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 = main1.php"); //配置完成后跳转到主程序页面。
}
?>
</body>
</html> main.php
<?PHP
static $flag = true; //用于标记是否出题,逻辑上的变量。
$open0 = file("config.txt");
$range = chop($open0[1]); //读取随机数大小上限。
if(!(isset($_REQUEST["answer"])) && $flag) //出现回答后不再运行。
{
include("exercise.php");
$object = new exercise();
$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")."<br>"."您的结果为:".$_REQUEST["answer"]."<br>";
}
else
{
?>
<form method = "post">
<input type = "text" name = "answer"><br>
<input type = "submit"></form>
<?PHP
}
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);
?>
<form method = "post" action = "main.php">
<input type = "submit" value = "下一题"></form>
<?PHP
}
else //否则跳转到退出界面。
{
echo "答题完毕。";
header("Refresh:3;url = quit.php");
}
} main1.php
<?PHP
static $flag = true; //用于标记是否出题,逻辑上的变量。
$open0 = file("config.txt");
$range = chop($open0[1]); //读取随机数大小上限。
if(!(isset($_REQUEST["answer1"])) && $flag) //出现回答后不再运行。
{
include("exercise.php");
$object = new exercise();
$object->randomFormula($range);
$flag = false;
} $open1 = fopen("exercise.txt",'r'); //读取exercise.txt的最后一行数据,即当前试题。
while($buf = fgets($open1))
{
$res = $buf;
}
fclose($open1);
echo $res; if(isset($_REQUEST["answer1"])) //输出答案和输入结果。
{
echo file_get_contents("result.txt")."<br>"."您的结果为:".$_REQUEST["answer1"]."<br>";
}
else
{
?>
<form method = "post">
<input type = "text" name = "answer1"><br>
<input type = "submit"></form>
<?PHP
}
if(isset($_REQUEST["answer1"])) //输入结果后进行判断。
{
judge($_REQUEST["answer1"]); }
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);
?>
<form method = "post" action = "main1.php">
<input type = "submit" value = "下一题"></form>
<?PHP
}
else //否则跳转到退出界面。
{
echo "答题完毕。";
header("Refresh:3;url = quit.php");
}
} exercise.php
<?PHP
class exercise
{
public $numberA;
public $numberB;
public $numberC;
public $result;
public function exercise()
{
$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");
fwrite($openB,$this->result);
if($symbolA == 0)
{
if($symbolB == 0)
{
$this->result = $this->numberB + $this->numberC;
}
if($symbolB == 1)
{
$this->result = $this->numberB - $this->numberC;
}
if($symbolB == 2)
{
$this->result = $this->numberB * $this->numberC;
}
if($symbolB == 3)
{
$this->result = $this->numberB / $this->numberC;
}
$this->result += $this->numberA;
}
if($symbolA == 1)
{
if($symbolB == 0)
{
$this->result = $this->numberA - $this->numberB + $this->numberC;
}
if($symbolB == 1)
{
$this->result = $this->numberA - $this->numberB - $this->numberC;
}
if($symbolB == 2)
{
$this->result = $this->numberA - $this->numberB * $this->numberC;
}
if($symbolB == 3)
{
$this->result = $this->numberA - $this->numberB / $this->numberC;
}
} if($symbolA == 2)
{
if($symbolB == 0)
{
$this->result = $this->numberA * $this->numberB + $this->numberC;
}
if($symbolB == 1)
{
$this->result = $this->numberA * $this->numberB - $this->numberC;
}
if($symbolB == 2)
{
$this->result = $this->numberA * $this->numberB * $this->numberC;
}
if($symbolB == 3)
{
$this->result = $this->numberA * $this->numberB / $this->numberC;
}
} if($symbolA == 3)
{
if($symbolB == 0)
{
$this->result = $this->numberA / $this->numberB + $this->numberC;
}
if($symbolB == 1)
{
$this->result = $this->numberA / $this->numberB - $this->numberC;
}
if($symbolB == 2)
{
$this->result = $this->numberA / $this->numberB * $this->numberC;
}
if($symbolB == 3)
{
$this->result = $this->numberA / $this->numberB / $this->numberC;
}
}
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;
} quit.php
<?php
echo "欢迎再次使用!";
?>
<form method = "post" action = "load.php">
<input type = "submit" value = "下载习题"> load.php
<?php
$filename = "exercise.txt";
header('Content-Type‘txt'); //指定下载文件类型
header('Content-Disposition: attachment; filename="'.$filename.'"'); //指定下载文件的描述
header('Content-Length:'.filesize($filename)); //指定下载文件的大小
readfile($filename);//将文件内容读取出来并直接输出,以便下载

3.结果截图

4.难点分析

  编码一开始难点就在如何使用表单中得到的值,如果提交到当前文件中,会使另一个文件中使用不了,即使提交到别的文件中,也很有局限性。所以实现一个数的运算及比较都花了很长时间。上网查了很多,后来发现可以存到txt中实现,虽然打开关闭的很麻烦,但好在逻辑上没错。后来在网上提问和web上机时,发现了session功能(还没有在php中尝试)。还有一些小问题也困扰过我,如不知道<?php ?>,if,else和表单之间的嵌套,不知道txt的读取打开方式和下载以及php中类的声明等等。

5.耗时

php实现网站四则运算。的更多相关文章

  1. 结对编程——paperOne基于java web的简易四则运算出题网站

    项目成员:张金生     张政 需求分析: 1.要进行四则运算: 2.运算题目随机: 3.进行对错判断: 4.整数运算. 程序概要: 1.用JSP实现: 2.用户可选择题目数量: 3.答题页用表格列出 ...

  2. paperOne基于java web的简易四则运算出题网站

    项目成员:张金生     张政 需求概要 1.运算数均为正整数 2.包含的运算符有+,-,*,/ 3.除法运算结果为整除运算 4.批量生成题目并判题 核心功能分析 1.题目生成——java后端 题目生 ...

  3. 四则运算appNABCD模型

    团队: 郭志豪:http://www.cnblogs.com/gzh13692021053/ 杨子健:http://www.cnblogs.com/yzj666/ 刘森松:http://www.cnb ...

  4. 20175126Apollo 20175126《Java程序设计》结队编程项目——四则运算

    结队编程项目——四则运算 一.项目需求 自动生成小学四则运算题目(加.减.乘.除)统计正确率 支持整数 支持多运算符(比如生成包含100个运算符的题目) 支持真分数 需求分析: 生成四则运算:需要使用 ...

  5. 四则运算之GUI

    四则运算之GUI Coding克隆地址:https://git.coding.net/lvgx/pair_programming.git   目录: 一.前言 二.计划时间——PSP 三.接口设计 四 ...

  6. 结对作业——web四则运算

    目录: 一.Coding.net项目地址 二.PSP 三.接口设计 四.接口实现 五.性能分析 六.单元测试 七.异常处理 八.模块设计 九.模块对接 十.结对 十一.思考 十二.PSP 网站:htt ...

  7. 结对作业——四则运算 Part3. 对于结对编程的总结与思考

    结对作业——四则运算 Part3. 对于结对编程的总结与思考 PB15061303 刘梓轩PB16061489 艾寅中 GITHUB 地址 戳这里 目录 Part 1. Core代码编写部分 Part ...

  8. 四则运算web版需求规格说明书

    目录 1引言... 4 1.1  目的... 4 1.2  背景... 4 1.3  术语... 4 1.4  预期读者与阅读建议... 5 1.5  参考资料... 6 1.6  需求描述约定... ...

  9. 如何一步一步用DDD设计一个电商网站(九)—— 小心陷入值对象持久化的坑

    阅读目录 前言 场景1的思考 场景2的思考 避坑方式 实践 结语 一.前言 在上一篇中(如何一步一步用DDD设计一个电商网站(八)—— 会员价的集成),有一行注释的代码: public interfa ...

随机推荐

  1. #define INVSQRT2 0.707106781 平方根倒数速算法

    转自 http://www.cnblogs.com/pkuoliver/archive/2010/10/06/1844725.html 源码下载地址:http://diducoder.com/sotr ...

  2. u-boot之内核是怎么启动的

    在u-boot之start_armboot函数分析已经分析过了整个程序框架,但只是说了下什么时候运行内核,并没有具体说明是怎么执行内核的.内核启动分以下几个步骤说明: 1.启动参数bootcmd=na ...

  3. android使用Pull解析来自服务器的xml文件时出现错误以及解决方案

    直接上代码,代码中有详细注释: 1 public class CheckUpdateManager { 2 private static final String TAG = "CheckU ...

  4. PAT 1061 判断题(15)(代码)

    1061 判断题(15 分) 判断题的评判很简单,本题就要求你写个简单的程序帮助老师判题并统计学生们判断题的得分. 输入格式: 输入在第一行给出两个不超过 100 的正整数 N 和 M,分别是学生人数 ...

  5. javascript 高级程序设计 九

    JS 面向对象的程序设计思想(1)深入理解JS对象 1.js的中没有OO语言中的'类'的概念.ECMAjs中把对象定义为:‘无序属性的集合,其属性可以包含基本值,对象或函数’. 2.ECMAScrip ...

  6. 如何使用NSDL玩转微信跳一跳

    目前网上介绍windows和IOS操作系统上玩微信跳一跳的有很多文章,但介绍Linux平台下的文章相对较少,所以动手操作下和大家分享,同时感谢wangshub在github上的分享: 1 下载wech ...

  7. 在HashTable上下文中,同步指的是什么?

    同步意味着在一个时间点只能有一个线程可以修改hash表,任何线程在执行HashTable的更新操作前都需要获取对象锁,其他线程需要等带锁的释放.

  8. swift MD5 加密方法

    引入OC类库 md5.h: #import <UIKit/UIKit.h> @interface Md5Controller : UIViewController @end md5.m: ...

  9. 教你避雷!网页设计中常见的17个UI设计错误集锦(附赠设计技巧)

    以下内容由摹客团队翻译整理,仅供学习交流,摹客iDoc是支持智能标注和切图的产品协作设计神器. 精心设计的用户界面对网站意义重大.具备所有最新功能和响应式设计有助于提高网站的搜索引擎排名,从而增加受众 ...

  10. proguard-rules.pro、混淆、导jar包

    前记: 买了一个<精通Android Studio>本来最想看的是关于混淆导jar包的,哪知道没有,有点小失望. 好吧,自己来. 在用Android Studio开发的时候,把minify ...