Use php + mysql to create a powerful online calculator

Source: Internet
Author: User
Tags acos asin
One day, I tried to search the calculator and found that it was JavaScript, and I had to click it one by one, but it could not be recorded. It was easy to make mistakes when I entered the formula, so I thought about how to make it easy to use.

One day, I tried to search the calculator and found that it was JavaScript, and I had to click it one by one, but it could not be recorded. It was easy to make mistakes when I entered the formula, so I thought about how to make it easy to use.

After searching for a long time, I found that there was very little information on the Internet, such as the US server and Hong Kong space. So I wanted to write it myself and found that there were too many problems. I didn't know how to use algorithms, it's okay to write a computing formula. If it's longer, it's okay. If it's longer, I'm afraid it will crash.

One day when I was doing mysql, I suddenly found that mysql was so powerful that I could directly compute strings... Haha.

The Code is also very simple, so I made an ajax calculator.

You can also display input sub-statements at any time when a suberror is prompted.

If you are interested, you can develop more functions by yourself.

Demo address:

Download jquer. js by yourself

Jsq1.php

The Code is as follows:


// Link to the 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 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 the cotangent
$ Str = preg_replace ("/asin \((. *) \)/is "," asin (\ $ {1}/pi () * 180) * 180/pi () ", $ str); // returns the arc sine.
$ Str = preg_replace ("/acos \((. *) \)/is "," acos (\ $ {1}/pi () * 180) * 180/pi () ", $ str); // returns the arc cosine
$ Str = preg_replace ("/atan \((. *) \)/is "," atan (\ $ {1}/pi () * 180) * 180/pi () ", $ str); // Replace the arc tangent
$ SQL = "select". $ str;
$ Res = mysql_query ($ SQL, $ db) or die ('your input formula has an error ');
$ Rs = mysql_fetch_array ($ res );
Echo $ rs [0];
?>


Jsq. php

The Code is as follows:




Handwritten input Calculator


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.