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. RabbitMQ (五)主题(Topic) -摘自网络

    虽然使用direct类型改良了我们的系统,但是仍然存在一些局限性:它不能够基于多重条件进行路由选择. 在我们的日志系统中,我们有可能希望不仅根据日志的级别而且想根据日志的来源进行订阅.这个概念类似un ...

  2. 转载-清除Linux中MySQL的使用痕迹~/.mysql_history

    原文地址:清除Linux中MySQL的使用痕迹~/.mysql_history 作者:RogerZhuo 原贴:http://bbs.chinaunix.net/thread-3676498-1-1. ...

  3. homework-01 最大子串和

    题目描述 对于一个给定的数列,求该数列最大的子串和(连续) 问题分析 处理发生区间上的问题时,经常会用一个非常简单经典的思路——部分和(也有叫前缀和).部分和的思想在很多复杂的区间上的算法中都有应用, ...

  4. C/C++ 不带参数的回调函数 与 带参数的回调函数 函数指针数组 例子

    先来不带参数的回调函数例子 #include <iostream> #include <windows.h> void printFunc() { std::cout<& ...

  5. 第三方类AFNetworking

    1.AFNetworking简介 AFNetworking是一个在iOS开发中使用非常多的轻量级网络开源库适用于iOS以及Mac OS X.它构建于在(Apple iOS开发文档) NSURLConn ...

  6. Lua学习笔记(一):搭建开发环境

    Lua是一个小巧高效的解释型脚本语言,可以方便的嵌入到任意的语言中,很多应用程序.游戏使用LUA作为自己的嵌入式脚本语言,以此来实现可配置性.可扩展性.这其中包括魔兽世界.博德之门.愤怒的小鸟.VOC ...

  7. 十,选择cfee编辑器并学会调试程序。以及结束语。

    为什么推荐用cfree呢?因为我认为这个编辑器界面友好,用起来方便. 你也许会问,调试程序是什么? 那么下面思考几个问题:对于前面讲的分支结构和循环结构有点不懂怎么办?如果写的程序语法没有错误但是运算 ...

  8. hdu 2553 N皇后问题 (经典DFS)

    题目链接:点击链接 思路:用一维数组hang[num] = i,num表示第num行,i表示第i列,计算n = 1~10皇后的不同放置数量,然后打表 #include<stdio.h> # ...

  9. IIS支持其他类型下载

    路径:IIS--默认网站属性-->http 头-->MIME映射 -->文件类型 -->新类型

  10. SmallDateTime时间范围检查

    SamllDataTime是SQL Server中的一种表达时间的类型, 精度不高但是省空间,具体的细节就查看一下MSDN的文档吧. http://msdn.microsoft.com/zh-cn/l ...