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


Posted in PHP onOctober 12, 2010

找了很久,发现网上资料很少,于是想自己动手写,慢慢的发现问题多了,自己不怎么通算法,写一个计算式子短点还好,长了就挂了,再长点恐怕就要死机。

有一天做做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) or die('<font color=red>你输入的式子有错误</font>'); 
$rs=mysql_fetch_array($res); 
echo $rs[0]; 
?>

jsq.php
<html> 
<head> 
<title>手写输入计算器</title> 
<meta name="keywords" content="在线计算器,输入式子直接计算,手写计算器" /> 
<meta name="description" content="在线计算器,手写输入计算器,输入式子直接计算" /> 
<script src="jquery.js" language="javascript"></script> 
<script language="javascript"> 
$(function(){ 
$("#t_ask").keyup(function(){ 
$.post( 
"jsq1.php", 
{ 
t_ask : $("#t_ask").val() 
},function(data,textStatus) 
{ 
$("#res").html(data); 
} 
); 
}); 
}); 
</script> 
</head> 
<body> 
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0"> 
<tr> 
<td align="center" height="40"><h2>手写输入计算器</h2></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">返回首页</a></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"></textarea></td> 
</tr> 
<tr> 
<td height="23" align="right">=</td> 
<td><div id="res"></div></td> 
</tr> 
<tr> 
<td height="31" align="right"></td> 
<td><input type="button" name="tj" id="tj" value="按钮" /> 
<input type="reset" name="qc" id="qc" value="重置" /></td> 
</tr> 
</table> 
<table width="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(float arg)// 计算 <strong>e</strong>(自然对数的底)的指数 
<p>log(10,100)=2//自然对数 pow(2,4)=16 指数 sqrt(4)=2平方根 
<p><br /> 
</td> 
</tr> 
</table> 
</form> 
</body> 
</html>
PHP 相关文章推荐
php 常用类汇总 推荐收藏
May 13 PHP
PHP面向对象三大特点学习(充分理解抽象、封装、继承、多态)
May 07 PHP
PHP+javascript制作带提示的验证码源码分享
May 28 PHP
Yii2表单事件之Ajax提交实现方法
May 04 PHP
使用PHP连接数据库_实现用户数据的增删改查的整体操作示例
Sep 01 PHP
PHP基于自定义函数生成笛卡尔积的方法示例
Sep 30 PHP
php使用curl伪造来源ip和refer的方法示例
May 08 PHP
PHP-FPM 设置多pool及配置文件重写操作示例
Oct 02 PHP
Thinkphp5.0 框架使用模型Model添加、更新、删除数据操作详解
Oct 11 PHP
Thinkphp5.0 框架的请求方式与响应方式分析
Oct 14 PHP
确保Laravel网站不会被嵌入到其他站点中的方法
Oct 18 PHP
PHP pthreads v3使用中的一些坑和注意点分析
Feb 21 PHP
发一个php简单的伪原创程序,配合商城采集用的
Oct 12 #PHP
php知道与问问的采集插件代码
Oct 12 #PHP
php笔记之常用文件操作
Oct 12 #PHP
php+jquery编码方面的一些心得(utf-8 gb2312)
Oct 12 #PHP
windows下升级PHP到5.3.3的过程及注意事项
Oct 12 #PHP
PHP OPCode缓存 APC详细介绍
Oct 12 #PHP
并发下常见的加锁及锁的PHP具体实现代码
Oct 12 #PHP
You might like
PHP脚本的10个技巧(4)
2006/10/09 PHP
php获得文件扩展名三法
2006/11/25 PHP
php获取后台Job管理的实现代码
2011/06/10 PHP
用来解析.htpasswd文件的PHP类
2012/09/05 PHP
PHP产生不重复随机数的5个方法总结
2014/11/12 PHP
在win系统安装配置 Memcached for PHP 5.3 图文教程
2015/03/03 PHP
Windows下php+mysql5.7配置教程
2017/05/16 PHP
JS location几个方法小姐
2008/07/09 Javascript
使用jQuery模板来展现json数据的代码
2010/10/22 Javascript
扩展JavaScript功能的正确方法(译文)
2012/04/12 Javascript
用js+iframe形成页面的一种遮罩效果的具体实现
2013/12/31 Javascript
Javascript 按位左移运算符使用介绍(
2014/02/04 Javascript
纯js实现无限空间大小的本地存储
2015/06/18 Javascript
基于jquery实现全屏滚动效果
2015/11/26 Javascript
jQuery获取剪贴板内容的方法
2016/06/16 Javascript
JavaScript直播评论发弹幕切图功能点集合效果代码
2016/06/26 Javascript
js字符限制(字符截取) 一个中文汉字算两个字符
2017/09/12 Javascript
使用vue官方提供的模板vue-cli搭建一个helloWorld案例分析
2018/01/16 Javascript
vue移动端实现红包雨效果
2020/06/23 Javascript
Python基础之函数用法实例详解
2014/09/10 Python
Python中的hypot()方法使用简介
2015/05/18 Python
对Python进行数据分析_关于Package的安装问题
2017/05/22 Python
Python网络编程基于多线程实现多用户全双工聊天功能示例
2018/04/10 Python
《与孩子一起学编程》python自测题
2018/05/27 Python
Python3.5字符串常用操作实例详解
2019/05/01 Python
python做反被爬保护的方法
2019/07/01 Python
Django 设置admin后台表和App(应用)为中文名的操作方法
2020/05/10 Python
Python安装Bs4的多种方法
2020/11/28 Python
matplotlib之pyplot模块之标题(title()和suptitle())
2021/02/22 Python
HTML5新特性 多线程(Worker SharedWorker)
2017/04/24 HTML / CSS
River Island美国官网:英国高街时尚品牌
2018/09/04 全球购物
Hobbs官方网站:英国奢华女性时尚服装
2020/02/22 全球购物
PHP如何去执行一个SQL语句
2016/03/05 面试题
妈妈再爱我一次观后感
2015/06/08 职场文书
结婚典礼致辞
2015/07/28 职场文书
班主任经验交流心得体会
2015/11/02 职场文书