Use Php+mysql to do a powerful online calculator _php instance

Source: Internet
Author: User
Tags acos asin cos natural logarithm sin square root
Looking for a long time, found little information on the internet, so want to write their own hands, slowly found many problems, their own algorithm, write a short calculation formula is OK, long on the hang, and then long I am afraid to panic.

One day do MySQL suddenly found that the original MySQL function so powerful, you can directly calculate the string ... Haha, this will be happy.

The code is also super simple to do an AJAX calculator

There's a wrong hint and you can always display the input formula.

Interested friends can see more features can be developed on their own

Demo Address: http://www.jianlila.com/jsq.php

Jquer.js yourself to download

jsq1.php
Copy Code code as follows:

<?php
of the linked database
$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);
/* Trigonometric 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);/Anyway strings
$str =preg_replace ("/acos\ (. *) \)/is", "ACOs (\${1}/pi () *180) *180/pi ()", $str);/anti-cosine
$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> the formula you entered has an error </font> ');
$rs =mysql_fetch_array ($res);
echo $rs [0];
?>

jsq.php
Copy Code code as follows:

<title> Handwriting Input Calculator </title>
<meta name= "keywords" content= "online calculator, input formula direct calculation, handwriting calculator"/>
<meta name= "description" content= "online calculator, handwriting input calculator, input formula direct calculation"/>
<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>
<body>
<table width= "border=" 0 "align=" center "cellpadding=" 0 "cellspacing=" 0 ">
<tr>
&LT;TD align= "center" height= "+" ></tr>
</table>
<table width= "border=" 0 "align=" center "cellpadding=" 0 "cellspacing=" 0 ">
<tr>
&LT;TD height= "align=" "Center" > Here you can calculate your handwriting. Oh, don't try! <a href= "http://www.jianlila.com" > Back home </a></td>
</tr>
</table>

<form method= "POST" >
<table width= "border=" 0 "align=" center "cellpadding=" 0 "cellspacing=" 0 ">
<tr>
&LT;TD width= "27%" align= "right" > Calculation formula:</td>
&LT;TD width= "73%" ><textarea name= "T_ask" cols= "" rows= "6" id= "T_ask" ></textarea></td>
</tr>
<tr>
&LT;TD height= "align=" "right" >=</td>
<td><div id= "res" ></div></td>
</tr>
<tr>
&LT;TD height= "align=" "right" ></td>
<td><input type= "button" Name= "TJ" id= "TJ" value= "button"/>
<input type= "reset" name= "QC" id= "QC" value= "reset"/></td>
</tr>
</table>
<table width= "border=" 0 "align=" center "cellpadding=" 0 "cellspacing=" 0 ">
<tr>
<td><p> Description: <br/>
Trigonometric function:
<p>sin (60) sine cos (60) cosine tan (60) tangent cot (60) cotangent
<p>asin (0.5) inverse chord acos (0.5)
Anti-cosine atan (0.5) Anyway tangent
<p>abs (-1) =1 absolute value ceil (0.1) =1 into a
<p> Index logarithm
<p>exp (float arg)//Calculation <strong>e</strong> (bottom of natural logarithm) index
<p>log (10,100) =2//natural logarithm pow (2,4) = 16 exponent sqrt (4) = 2 square root
<p><br/>
</td>
</tr>
</table>
</form>
</body>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.