后台-php 用栈 做符号匹配 错误 求分析

问题描述

php 用栈 做符号匹配 错误 求分析

我想用两个数组实现表达式的四则运算 但是不知道为什么 代码有bug 我分析不出原因
求大神指教

解决方案

后台代码如下;
传入的是一个用空格分开的算术表达式例如:1 + 2 * 3 + 2

<?php
$result=0; //对result赋值,定义变量。

function youxianji($ch1,$ch2)
{
if($ch2 == '#'||$ch1 == '('||$ch2 == '(')
return 0;
if(($ch1 == '+'||$ch1 == '-')&&($ch2 == '*'||$ch2 == '/'))
return 1;
if(($ch1 == '*'||$ch1 == '/')&&($ch2 == '+'||$ch2 == '-'))
return 0;
if(($ch1 == '*'||$ch1 == '/')&&($ch2 == '*'||$ch2 == '/'))
return 1;
if(($ch1 == '+'||$ch1 == '-')&&($ch2 == '+'||$ch2 == '-'))
return 1;
if($ch1 == ')')
return 1;

}

function operator($d1,$d2,$ch)

{
switch ($ch)
{
case '+':return $d1+$d2;
break;
case '-':return $d1-$d2;
break;
case '*':return $d1*$d2;
break;
case '/':return $d1/$d2;
break;
default :return 0;
}

}

function calu($ch)
{

$a1=array();//存储的运算符的数组
$a2=array();//存储数字的数组
array_push($a1,'#');
$i=0;

while($i<count($ch))
{
if(is_string($it)) //is a char
{
$top=end($a1);//提取运算符
$Doit = youxianji($it,$top); //对运算符或数字进行处理
switch ($Doit)
{
case 0: array_push($a1,$it); //新加进来的优先级高,进栈
break;
// case 0: char_stack.Push((*it)); //新加进来的优先级,进栈
// break;
case 1: $dTemp = end($a2); //加进来的优先级低,相等,需要先运算,此时可能是数字
array_pop($a2);
$dResult = operator(end($a2),$dTemp,end($a1));
array_pop($a2); //出栈后重新赋值入栈
array_push($a2,$dResult);
array_pop($a1); //运算符出栈
if($it != ')')
{
array_push($a1,$it);
}
break;
}
if(($it== ')')&&(end($a1) !='('))
{
//nothing to do
}
else
{
if(($it == ')')&&(end($a1) == '('))
{
array_pop($a1);
}
$i++;
}
}
else if(is_double($it))
{
array_push($a2,$it);
$i++;

}
$it=$ch[$i];

}

while(end($a1) != '#') //所有字符都处理完,留下栈内的字符处理
{
$dTemp = end($a2); //加进来的优先级低,相等,需要先运算
array_pop($a2);
$dResult = operator(end($a2),$dTemp,end($a1));//实行运算
array_pop($a2); //出栈后重新赋值入栈
array_push($a2,$dResult);
array_pop($a1); //运算符出栈
}
return end($a2);
}

if($_POST['suanshi']!=null){
$suanshi=$_POST['suanshi'];
$array=explode(' ',$suanshi);

echo calu($array);

}

else echo "还没输入";

?>

解决方案二:

http://bardo.iteye.com/blog/845630

解决方案三:

http://wenku.it168.com/d_001271502.shtml

时间: 2024-08-06 20:41:07

后台-php 用栈 做符号匹配 错误 求分析的相关文章

wordpress后台访问时没有wp-admin报404错误原因分析

前几天不知道对wordpress所在的服务器做什么操作了今天更新时后台所有的菜单都出现了404错误.本来以为是服务器上丢失了文件导致的.但是我登录上服务器查看所有的后台文件都在呢.多点了几个链接发现URL里没有自动添加上wp-admin这一级目录,所以报404错误. 回想以下好像是我改了head的base,但是在代码里找了找没发现哪和之前不一样.和另一个wordpess的站的后台head对比了一下也没发现问题. 1. 检查网站 .htaccess 文件 登陆FTP找到根目录下的.htaccess

struct-c语言用栈做的数制转换,为什么输出不对啊,总是4206655,还停不下来

问题描述 c语言用栈做的数制转换,为什么输出不对啊,总是4206655,还停不下来 #include"stdio.h" #include"stdlib.h" #define STACK_INIT_SIZE 100 #define STACKINCREMENT 10 #define NULL 0 #define OK 1 #define ERROR 0 #define OVERFLOW -2 #define TRUE 1 #define FALSE 0 typedef

js-博文用什么插件做,前台后台(java)做好看些

问题描述 博文用什么插件做,前台后台(java)做好看些 我想做一个类似于空间日志博文那种类型,编写--保存--发布.要做的好看些就好了!最主要的是贴上一些代码好看一点.把所有的保存到数据库里面.

mybatis-SpringMVC+Mybatis调用后台报空指针异常,代码没查出错误,页面也有参数传进来,求解答

问题描述 SpringMVC+Mybatis调用后台报空指针异常,代码没查出错误,页面也有参数传进来,求解答 [DEBUG]2015-08-12 11:07:30,234 org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver Resolving exception from handler [public java.util.Map cn.sg.controller.Back

otl-Solaris下用OTL编程出现:ld: 致命的: 符号参照错误. 没有输出被写入错误

问题描述 Solaris下用OTL编程出现:ld: 致命的: 符号参照错误. 没有输出被写入错误 -bash-3.00$ g++ -g -I. -I/export/home/oracle/app/product/10.2.0.1/rdbms/public -L/export/home/oracle/app/product/10.2.0.1/lib32 -lclntsh -o otl otlexamp.cpp -lsocket -lnsl -lresolv 未定义 文件中的 符号 在文件中 ins

菜单-做侧滑布局错误,我找不到怎解决,大牛们帮看看,白菜求解

问题描述 做侧滑布局错误,我找不到怎解决,大牛们帮看看,白菜求解 package com.test.cehua.sindleMenu; import android.annotation.SuppressLint;import android.content.Context;import android.util.AttributeSet;import android.util.DisplayMetrics;import android.util.TypedValue;import androi

aspx前台有两个按钮 id为 a和b 我在后台cs中怎么做才能让它们隐藏

问题描述 aspx前台有两个按钮 id为 a和b 我在后台cs中怎么做才能让它们隐藏 aspx前台有两个按钮 id为 a和b 我在后台cs中怎么做才能让它们隐藏 解决方案 加一个div 如果按钮隐藏出现空白不好看 所以干脆直接隐藏一个div 在aspx.cs里面获取那div的id 然后隐藏div

hibernate-用ssh写的java后台,在本机上做了下并发测试,才100用户并发mysql就开始报异常了,怎么解决

问题描述 用ssh写的java后台,在本机上做了下并发测试,才100用户并发mysql就开始报异常了,怎么解决 org.hibernate.exception.JDBCConnectionException: Cannot open connection at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:97) at org.hibernate.exception.JDBCExceptionHe

跟后台打印程序系统服务通讯时出现错误的解决方法_win服务器

事件类型:      警告事件来源:      TermServDevices事件种类:      无事件 ID:      1114日期:          2006-12-19事件:          15:50:17用户:          N/A计算机:      描述:跟后台打印程序系统服务通讯时出现错误.请打开服务管理单元,确认后台打印程序服务是否在运行.-----------------------------------------------------------------