找了很久,发现网上资料很少,于是想自己动手写,慢慢的发现问题多了,自己不怎么通算法,写一个计算式子短点还好,长了就挂了,再长点恐怕就要死机。 有一天做做mysql突然发现原来mysql功能这么强大,可以直接计算字符串。。。哈哈 这下可就高兴了。 代码还超级简单 就做了一个ajax的计算器 有式子错误提示 还可以时时显示输入的式子 有兴趣的朋友可以看看 更多的功能可以自己去开发 演示地址:http://www.jianlila.com/jsq.php jquer.js自己去下载 jsq1.php <div class="codetitle"><a style="CURSOR: pointer" data="11896" class="copybut" id="copybut11896" onclick="doCopy('code11896')"> 代码如下:<div class="codebody" id="code11896"> <?php //链接数据库的 $db=mysql_connect("localhost","root","123"); header("Content-Type:text/html;charset=GB2312"); $str=iconv('utf-8','gbk',trim($_POST['t_ask'])); $str=str_replace(" ","",str_replace("rn",$str)); $str=str_replace("(","(",$str); $str=str_replace(")",")",$str); /三角函数替换/ $str=preg_replace("/sin((.))/is","sin(${1}pi()/180)",$str);//替换sin $str=preg_replace("/cos((.))/is","cos(${1}pi()/180)",$str);//替换cos $str=preg_replace("/tan((.))/is","tan(${1}pi()/180)",$str);//替换tan $str=preg_replace("/cot((.))/is","1/tan(${1}pi()/180)",$str);//替换余切 $str=preg_replace("/asin((.))/is","asin(${1}/pi()180)180/pi()",$str);//反正弦 $str=preg_replace("/acos((.))/is","acos(${1}/pi()180)180/pi()",$str);//反余弦 $str=preg_replace("/atan((.))/is","atan(${1}/pi()180)*180/pi()",$str);//替换反正切 $sql="select ".$str ; $res=mysql_query($sql,$db) or die('你输入的式子有错误'); $rs=mysql_fetch_array($res); echo $rs[0]; ?> jsq.php <div class="codetitle"><a style="CURSOR: pointer" data="3610" class="copybut" id="copybut3610" onclick="doCopy('code3610')"> 代码如下:<div class="codebody" id="code3610"> 手写输入计算器 <script src="jquery.js" language="javascript"> <script language="javascript"> $(function(){ $("#t_ask").keyup(function(){ $.post( "jsq1.php", { t_ask : $("#t_ask").val() },function(data,textStatus) { $("#res").html(data); } ); }); }); <table width="800" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td align="center" height="40">手写输入计算器</td> </tr> </table> <table width="800" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td height="34" align="center">在这里你可以手写式子计算哦,还不快试试! <a href="http://www.jianlila.com">返回首页</td> </tr> </table> <form method="post"> <table width="800" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td width="27%" align="right">计算式子:</td> <td width="73%"><textarea name="t_ask" cols="60" rows="6" id="t_ask"></td> </tr> <tr> <td height="23" align="right">=</td> <td><div id="res"></td> </tr> <tr> <td height="31" align="right"></td> <td> </td> </tr> </table> <table width="800" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td>说明: 三角函数:
sin(60)正弦 cos(60)余弦 tan(60)正切 cot(60)余切
asin(0.5)反正弦 acos(0.5) 反余弦 atan(0.5)反正切
abs(-1)=1绝对值 ceil(0.1)=1进一
指数对数
exp(float arg)// 计算 e(自然对数的底)的指数
log(10,100)=2//自然对数 pow(2,4)=16 指数 sqrt(4)=2平方根
</td> </tr> </table>
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|