zl程序教程

您现在的位置是:首页 >  数据库

当前栏目

利用php+mysql来做一个功能强大的在线计算器

mysqlPHP 利用 一个 在线 功能强大 计算器
2023-06-13 09:14:24 时间
找了很久,发现网上资料很少,于是想自己动手写,慢慢的发现问题多了,自己不怎么通算法,写一个计算式子短点还好,长了就挂了,再长点恐怕就要死机。

有一天做做mysql突然发现原来mysql功能这么强大,可以直接计算字符串。。。哈哈这下可就高兴了。

代码还超级简单就做了一个ajax的计算器

有式子错误提示还可以时时显示输入的式子

有兴趣的朋友可以看看更多的功能可以自己去开发

演示地址:http://www.jianlila.com/jsq.php

jquer.js自己去下载

jsq1.php
复制代码代码如下:

<?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("\r\n","",$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)ordie("<fontcolor=red>你输入的式子有错误</font>");
$rs=mysql_fetch_array($res);
echo$rs[0];
?>

jsq.php
复制代码代码如下:

<html>
<head>
<title>手写输入计算器</title>
<metaname="keywords"content="在线计算器,输入式子直接计算,手写计算器"/>
<metaname="description"content="在线计算器,手写输入计算器,输入式子直接计算"/>
<scriptsrc="jquery.js"language="javascript"></script>
<scriptlanguage="javascript">
$(function(){
$("#t_ask").keyup(function(){
$.post(
"jsq1.php",
{
t_ask:$("#t_ask").val()
},function(data,textStatus)
{
$("#res").html(data);
}
);
});
});
</script>
</head>
<body>
<tablewidth="800"border="0"align="center"cellpadding="0"cellspacing="0">
<tr>
<tdalign="center"height="40"><h2>手写输入计算器</h2></td>
</tr>
</table>
<tablewidth="800"border="0"align="center"cellpadding="0"cellspacing="0">
<tr>
<tdheight="34"align="center">在这里你可以手写式子计算哦,还不快试试!<ahref="http://www.jianlila.com">返回首页</a></td>
</tr>
</table>

<formmethod="post">
<tablewidth="800"border="0"align="center"cellpadding="0"cellspacing="0">
<tr>
<tdwidth="27%"align="right">计算式子:</td>
<tdwidth="73%"><textareaname="t_ask"cols="60"rows="6"id="t_ask"></textarea></td>
</tr>
<tr>
<tdheight="23"align="right">=</td>
<td><divid="res"></div></td>
</tr>
<tr>
<tdheight="31"align="right"></td>
<td><inputtype="button"name="tj"id="tj"value="按钮"/>
<inputtype="reset"name="qc"id="qc"value="重置"/></td>
</tr>
</table>
<tablewidth="800"border="0"align="center"cellpadding="0"cellspacing="0">
<tr>
<td><p>说明:<br/>
三角函数:
<p>sin(60)正弦cos(60)余弦tan(60)正切cot(60)余切
<p>asin(0.5)反正弦acos(0.5)
反余弦atan(0.5)反正切
<p>abs(-1)=1绝对值ceil(0.1)=1进一
<p>指数对数
<p>exp(floatarg)//计算<strong>e</strong>(自然对数的底)的指数
<p>log(10,100)=2//自然对数pow(2,4)=16指数sqrt(4)=2平方根
<p><br/>
</td>
</tr>
</table>
</form>
</body>
</html>