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的更多相关文章

  1. C#在Oralce环境执行查询时报"Arithmetic operation resulted in an overflow"

    问题描述:C#代码在Oralce环境执行分组求和的Sql时报错,提示“Arithmetic operation resulted in an overflow”,即算术运算导致溢出 (1).执行Sql ...

  2. Algebraic Foundations ( Arithmetic and Algebra) CGAL 4.13 -User Manual

    理解: 本节主要介绍CGAL的代数结构和概念之间的互操作.与传统数论不同,CGAL的代数结构关注于实数轴的“可嵌入”特征.它没有将所有传统数的集合映射到自己的代数结构概念中,避免使用“数的类型”这一术 ...

  3. Linux Kernel Oops异常分析

    1.PowerPC小系统内核异常分析 1.1  异常打印 Unable to handle kernel paging request for data at address 0x36fef31eFa ...

  4. [.net 面向对象编程基础] (7) 基础中的基础——流程控制语句

    [.net 面向对象编程基础] (7) 基础中的基础——流程控制语句 本来没有这一节的内容,后来考虑到既然是一个系列文章,那么就尽可能写的详细一些,本节参考了网上朋友所写的例子,为的是让更多小伙伴学习 ...

  5. Assembly - Registers

    Processor operations mostly involve processing data. This data can be stored in memory and accessed ...

  6. Process Kill Technology && Process Protection Against In Linux

    目录 . 引言 . Kill Process By Kill Command && SIGNAL . Kill Process By Resource Limits . Kill Pr ...

  7. 【JAVA】通过公式字符串表达式计算值,网上的一种方法

    public class Test {    public static void main(String[] args) {     SimpleCalculator s=new SimpleCal ...

  8. linux入命令基础

    查看系统版本: cat /proc/version lsb_release -a uname -a 查看进程: ps ps aux |grep #查询字符串 杀掉进程:  kill #标号 强制杀掉: ...

  9. free pascal 错误代码表

    free pascal 错误代码表 为了方便对照检查运行时错误代码,这里把所有的错误代码的含义整理出来.(最大序号为232,中间不一定连续.user.pdf P175) Run-time errors ...

随机推荐

  1. flot图表的使用

    Flot是一套用Javascript写的绘制图表用的函式库, 专门用在网页上执行绘制图表功能, 由于Flot利用jQuery所以写出来的, 所以也称它为jQuery Flot ,它的特点是体积小.执行 ...

  2. 转】Nginx+tomcat集群环境搭建(Windows下)

    原博文出自于:    http://blog.csdn.net/clj198606061111/article/details/22621003       感谢! 实验环境 windows xp s ...

  3. iOS学习之触摸事件

    触摸事件 iOS中的事件: 在用户使用app过程中,会产生各种各样的事件.iOS中的事件可以分为3大类型: view的触摸事件处理: 响应者对象: 在iOS中不是任何对象都能处理事件,只有继承了UIR ...

  4. Light oj 1236 - Pairs Forming LCM (约数的状压思想)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1236 题意很好懂,就是让你求lcm(i , j)的i与j的对数. 可以先预处理1e7以 ...

  5. Contest 7.23(不知道算什么)

    Problem A   URAL 1181 Cutting a Painted Polygon 题目大意就是说有一个N边形,让你做N-3条边,让他们的每个三角形的三个顶点颜色都不相同. 这里有一个引理 ...

  6. IE 、Firefox、Chrome 浏览器在 F12 控制台下切换至不同框架介绍

    有不少网页的页面,还在使用 iframe 标签,而此时,相当于页面有两个 window 对象,一个为当前页面 window ,另一个则为 iframe 页面下的 window .因为,有时候需要在 c ...

  7. Python3爬取中文网址

    本以为Python3的中文兼容性无比友好,结果还是会有一点问题的. 解决方案如下,就以爬取我自己的一个网页极简翻译为例 #coding:utf8 import urllib.request impor ...

  8. 项目经验之:再来一章:excel导入数据 封装成最棒的不容易!!!

    我见过很的系统,包括OA,ERP,CRM等,在常用的功能当中,从外部导入数据是最常用到的.因为很多客户需要以excel的形式提供数据,,这样的方式我们又如何做呢, 大家最常见的做法可能是这样的,在需要 ...

  9. Parallax Occlusion Mapping

    如上图,本来是采样original texture coordinates点的颜色,其实却采样了correcter texture coordinates点的颜色. 而且会随着视线的不同看到凹凸程度变 ...

  10. poj2105 IP Address(简单题)

    题目链接:id=2105">http://poj.org/problem?id=2105 ----------------------------------------------- ...