Home > Backend Development > PHP Tutorial > Use php+mysql to make a powerful online calculator_PHP tutorial

Use php+mysql to make a powerful online calculator_PHP tutorial

WBOY
Release: 2016-07-21 15:33:35
Original
1380 people have browsed it

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



< ;tr>


Here you can calculate by handwriting, why not give it a try! Return to homepage




< tr>






< /tr>



Calculation formula:
=






Instructions:

Trigonometric functions:

sin(60) sine cos(60) cosine tan (60) Tangent cot(60) Cotangent

asin(0.5) Arcsine acos(0.5)
Arccosine atan(0.5) Arctangent

abs(-1) =1 absolute value ceil(0.1)=1 into one

Exponential logarithm

exp(float arg)// Calculatee(natural logarithm The exponent of the base)

log(10,100)=2//natural logarithm pow(2,4)=16 exponent sqrt(4)=2 square root







www.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...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template