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 ...
随机推荐
- 《Java数据结构与算法》笔记-CH2无序数组
/** * 本章目标: * 1.自制数组类 * 2.有序数组:按关键字升降序排列:二分法查找 * 3.分析有序数组.大O表示法 */ /** * 自制数组类 书中有的地方有错误,本程序以修改 */ c ...
- Yii CModel中rules验证规则
array( array(‘username’, ‘required’), array(‘username’, ‘length’, ‘min’=>3, ‘max’=>12), array( ...
- Xshell异常断开
这可能是由于 SSH 超时断开连接 导致的!可以这样做...修改/etc/ssh/sshd_config文件,找到 ClientAliveInterval 0和ClientAliveCountMax ...
- POJ 3268 Silver Cow Party (最短路dijkstra)
Silver Cow Party 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/D Description One cow fr ...
- 新浪云sae 邮件服务 quicksend()
<?php header("Content-Type: text/html;charset=utf-8"); $mail = new SaeMail(); $form_Con ...
- My97DatePicker源码的K方法
<head></head> <script> var X = window,M = "document", C = "getEleme ...
- Spring入门(10)-Spring JDBC
Spring入门(10)-Spring JDBC 0. 目录 JdbcTemplate介绍 JdbcTemplate常见方法 代码示例 参考资料 1. JdbcTemplate介绍 JdbcTempl ...
- ios 调试
http://www.cnblogs.com/weilaikeji/p/3306597.html http://www.cnblogs.com/Twisted-Fate/p/4760156.html ...
- SQLite使用事务更新—by command
public void SaveToDB(DataTable dt) { /* todo:sqlite没有提供批量插入的机制,需要通过事务处理 更新所有数据 * http://www.cnblogs. ...
- ADO.NET 快速入门(三):从存储过程获取输出参数
一些存储过程通过参数返回值.当参数在SQL表达式或者存储过程中被定义为“输出”,参数值会返回给调用者.返回值存储在 OleDbCommand 或者 SqlCommand 对象的参数集合的参数里. ...