Use php + mysql to create a powerful online calculator _ php instance-php Tutorial

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. I can enter it directly on the keyboard. After searching for a long time, I found that there was very little information on the Internet, so I wanted to write it myself. I slowly found that there were many problems. I didn't know how to use algorithms, but it was okay to write a computing formula, if it is longer, it will go down. if it is longer, I am 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: http://www.jianlila.com/jsq.php

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


Related Article

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.