PHP -- four arithmetic operation
PHP 生成 简单四则运算。
Thanks for Open Source.
本代码基于 jiaych php四则运算计算函数实现。
<?php
/*基于jiaych php四则运算计算函数
http://download.csdn.net/user/jiaych 实现 */
class randmath
{
//$num 数字个数,$nsize 数字位数
public function creatmath($num, $nsize)
{
$str_num = rand(0, pow(10,$nsize));
for ($i = 1; $i < $num; $i++) {
$str_t=rand(0, pow(10,$nsize)); $str_num = $this->rand_num($str_num, rand(0, pow(10,$nsize))); }
return $str_num;
}
//生成四则运算符号
function rand_num($str1, $str2)
{
$s_num = rand(1, 4);
$str="";
switch ($s_num) {
case 1: //+
$str= $str1 . "+" . $str2;
break;
case 2: //-
$str= $str1 . "-" . $str2;
break;
case 3: //*
$str= $str1 . "*" . $str2;
break;
case 4: // /
$str= $str1 . "/" . $str2;
break;
/* case 5: //()
echo "</br>" . $s_num;
return $str1."+".$str2;
break; */
}
return $str;
}
}
class math_rpn { function exp2rpn($expression){ $_stack = array('#');
$_rpnexp = array();
$_operator = array('(', '+', '-', '*', '/', ')');
$_priority = array('#' => 0, '(' => 10, '+' => 20, '-' => 20, '*' => 30, '/' => 30);
$data='';
$len = strlen($expression); for($i = 0; $i < $len; $i++) {
$char = substr($expression, $i, 1); if ($char == '(') {
$_stack[] = $char;
continue;
} else if ( ! in_array($char, $_operator)) {
$data.=$char;
if($i+1<$len)
{
$next = substr($expression, $i+1, 1);
if(in_array($next, $_operator)||is_null($next))
{
$_rpnexp[] = $data;
$data=null;
}
}
else
{
$_rpnexp[] = $data;
$data=null;
}
continue;
} else if ($char == ')') {
for($j = count($_stack); $j >= 0; $j--) {
$tmp = array_pop($_stack);
if ($tmp == "(") {
break;
} else {
$_rpnexp[] = $tmp;
}
}
continue;
} else if ($_priority[$char] <= $_priority[end($_stack)]) {
$_rpnexp[] = array_pop($_stack);
$_stack[] = $char;
continue;
} else {
$_stack[] = $char;
continue;
}
} for($i = count($_stack); $i >= 0; $i--) {
if (end($_stack) == '#') break;
$_rpnexp[] = array_pop($_stack);
}
$mystack=array();
foreach($_rpnexp as $ret)
{
if($ret=="+")
{
$tmp_a=array_pop($mystack);
$tmp_b=array_pop($mystack);
$mystack[]=$tmp_a+$tmp_b;
}
else if($ret=="-")
{
$tmp_a=array_pop($mystack);
$tmp_b=array_pop($mystack);
$mystack[]=$tmp_b-$tmp_a;
}
else if($ret=="*")
{
$tmp_a=array_pop($mystack);
$tmp_b=array_pop($mystack);
$mystack[]=$tmp_b*$tmp_a;
}
else if($ret=="/")
{
$tmp_a=array_pop($mystack);
$tmp_b=array_pop($mystack);
$mystack[]=$tmp_b/$tmp_a;
}
else
{
$mystack[]=$ret;
}
}
return $mystack[0];
}
}//测试实例
/*$expression = "(10.1+3)*(15)-1.4+5";
echo $expression."=";
$mathrpn = new math_rpn();
echo $mathrpn->exp2rpn($expression)."</br>";
*/
// $rand_math = new randmath();
// echo $rand_math->creatmath(4, 2);
?>
randmath.php
<?php
header("Content-type: text/html; charset=utf-8");
include("randmath.php"); $mathrpn = new math_rpn();
$rand_math = new randmath();//生成随机式子 $i=10;
while($i>0)
{
$rand_formula = $rand_math->creatmath(4, 1);//生成随机式子方法($num 数字个数,$nsize 数字位数)
$math_result=$mathrpn->exp2rpn($rand_formula);
if(is_int($math_result)&$math_result>0)
{
$i--;
echo $rand_formula . "=" . $math_result."</br>";
}
}
?>
ShowRPN.php
这样就能生成简单四则运算了。
PHP -- four arithmetic operation的更多相关文章
- C#在Oralce环境执行查询时报"Arithmetic operation resulted in an overflow"
问题描述:C#代码在Oralce环境执行分组求和的Sql时报错,提示“Arithmetic operation resulted in an overflow”,即算术运算导致溢出 (1).执行Sql ...
- Algebraic Foundations ( Arithmetic and Algebra) CGAL 4.13 -User Manual
理解: 本节主要介绍CGAL的代数结构和概念之间的互操作.与传统数论不同,CGAL的代数结构关注于实数轴的“可嵌入”特征.它没有将所有传统数的集合映射到自己的代数结构概念中,避免使用“数的类型”这一术 ...
- Linux Kernel Oops异常分析
1.PowerPC小系统内核异常分析 1.1 异常打印 Unable to handle kernel paging request for data at address 0x36fef31eFa ...
- [.net 面向对象编程基础] (7) 基础中的基础——流程控制语句
[.net 面向对象编程基础] (7) 基础中的基础——流程控制语句 本来没有这一节的内容,后来考虑到既然是一个系列文章,那么就尽可能写的详细一些,本节参考了网上朋友所写的例子,为的是让更多小伙伴学习 ...
- Assembly - Registers
Processor operations mostly involve processing data. This data can be stored in memory and accessed ...
- Process Kill Technology && Process Protection Against In Linux
目录 . 引言 . Kill Process By Kill Command && SIGNAL . Kill Process By Resource Limits . Kill Pr ...
- 【JAVA】通过公式字符串表达式计算值,网上的一种方法
public class Test { public static void main(String[] args) { SimpleCalculator s=new SimpleCal ...
- linux入命令基础
查看系统版本: cat /proc/version lsb_release -a uname -a 查看进程: ps ps aux |grep #查询字符串 杀掉进程: kill #标号 强制杀掉: ...
- free pascal 错误代码表
free pascal 错误代码表 为了方便对照检查运行时错误代码,这里把所有的错误代码的含义整理出来.(最大序号为232,中间不一定连续.user.pdf P175) Run-time errors ...
随机推荐
- ubuntu开机自启动脚本编写
1.将启动脚本复制到/etc/init.d目录下面 2.chmod 755 /etc/init.d/xxx 3.sudo update-rc.d /etc/init.d/xxx defaults 95 ...
- Linux下搭建jdk
1.上oracle下载jdk-8u73-linux-x64.gz文件(其他版本的也可以,不过本教程以.gz格式为准,其他格式没有尝试过,不造怎么弄) 2.在linux桌面上右键->打开终端 3. ...
- 【转】Maven实战(九)---模块聚合和继承
原博文出自于:http://blog.csdn.net/liutengteng130/article/details/47001831 感谢! 类之间有聚合和继承关系,Maven也具备这样的设计原 ...
- Web Service学习之一:Web Service原理
一.定义 Web Service 不是框架也不是技术 而是解决远程调用.跨平台调用.跨语言调用问题的一种规范. 二.应用1.同一个公司新.旧系统的整合:比如CRM系统与OA.客服系统相互调用2.不同公 ...
- JDBC学习笔记(5)——利用反射及JDBC元数据编写通用的查询方法
JDBC元数据 1)DatabaseMetaData /** * 了解即可:DatabaseMetaData是描述数据库的元数据对象 * 可以由Connection得到 */ 具体的应用代码: @Te ...
- HDU 5802 Windows 10 (贪心+dfs)
Windows 10 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5802 Description Long long ago, there was ...
- mybatis generator配置文件
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration ...
- C++常用容器
vector 顺序容器,和数组类似,可从尾部快速的插入和删除,可随机访问. vector的常用成员函数: #include<vector> std::vector<type> ...
- FloatingActionButton的一点学习感悟
最近在学习android材料设计的新控件,前面一篇文章讲到 CoordinatorLayout 结合几个新控件可以实现的几个效果.其中第一个是,Coordinatorlayout + Floating ...
- 利用微软Speech SDK 5.1开发语音识别系统主要步骤
利用微软Speech SDK 5.1开发语音识别系统主要步骤 2009-09-17 10:21:09| 分类: 知识点滴|字号 订阅 微软语音识别分两种模式:文本识别模式和命令识别模式.此两种模式的 ...