After searching for a long time, I found that there was very little information on the Internet, so I wanted to write it myself. Gradually, I found that there were many problems. I was not very good at algorithms. It was okay to write a short calculation formula, but it would fail if it was too long. If it was longer, I would be afraid. It's about to crash.
One day while doing mysql, I suddenly discovered that mysql is so powerful that it can directly calculate strings. . . Haha, I’m so happy now.
The code is super simple and I made an ajax calculator
There are formula error prompts and the entered formula can be displayed at all times
Interested friends can take a look You can develop more functions by yourself
Demo address: http://www.jianlila.com/jsq.php
Download jquer.js yourself
jsq1. php
Copy code The code is as follows:
//
$db that links the database =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);
/*Trigonometric function replacement*/
$str= preg_replace("/sin((.*))/is","sin(${1}*pi()/180)",$str);//Replace sin
$str=preg_replace("/cos ((.*))/is","cos(${1}*pi()/180)",$str);//Replace cos
$str=preg_replace("/tan((.*) )/is","tan(${1}*pi()/180)",$str);//Replace tan
$str=preg_replace("/cot((.*))/is", "1/tan(${1}*pi()/180)",$str);//Replace cotangent
$str=preg_replace("/asin((.*))/is","asin (${1}/pi()*180)*180/pi()",$str);//arcsine
$str=preg_replace("/acos((.*))/is"," acos(${1}/pi()*180)*180/pi()",$str);//Inverse cosine
$str=preg_replace("/atan((.*))/is", "atan(${1}/pi()*180)*180/pi()",$str);//Replace arctangent
$sql="select ".$str;
$res= mysql_query($sql,$db) or die('The formula you entered is wrong');
$rs=mysql_fetch_array($res);
echo $rs[0];
?>
jsq.php
Copy code The code is as follows:
Handwriting input calculator < ;script src="jquery.js" language="javascript">
Handwriting input calculator |
http://www.bkjia.com/PHPjc/322555.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322555.htmlTechArticleAfter searching for a long time, I found that there was very little information online, so I wanted to write it myself, but slowly I found that there were more questions. I’m not very good at algorithms. It’s better to write a short calculation formula, but it will fail if it’s too long. Then...