How to Make Your Own Online Calculator


I Recently produced my very own calculator and I was surprised how difficult it was. Yes of course a important calculator that supports the fundamentals is not really tough to do. But I had the Subsequent specification:

  • Use PHP, HTML, CSS and javascript only
  • One input field
  • Result history
  • No refreshing the page
  • Support trigonometric functions
  • Support for Pi, Sqrt and logarithm

The authentic undertaking is the "one input field"-thing. If you support the user produce numbers and operators in the identical input field you will have to separate them from every sole other later to perform the calculations. That is a great deal Much easier pointed out that done. For example, how do you establish the many meanings of the substring "-3" in "sqrt(6-3+5)" and "sqrt(-3+5)"? The Remedy is common expressions.

When you Acquire your calculator you have to use a excellent deal of regex to separate numbers, operators and operates type the two other. bring this string for example: "3+4.2-1". You have to break it into 3 elements and you have to know which pars that is operators and which parts that is numbers. The second this is achieved you just have to take a appear at the expressions in the precise mathematical order, placed the effects together and Look at the parts back again right up until eventually there is just One Part left and (hopefully) there is your answer. The quality calculator stuff like Pi, Square root, trigonometric function and Therefore forth is solved with prepared in PHP functions.

To fix the "no refreshing the page" you have to use Ajax. Ajax is a fairly renowned strategy to include posting to a page that is previously loaded. I would advise you to use the javascript library jQuery to send ajax-requests and to control the response. The item data ought to be prepared on the web page in a way that you can see effect history. Thanks to jQuery there is only drive for a couple of Thousands line of javascript and the code is not messy at all. jQuery can do a best deal much more than sending HTTP-requests. You can animate DOM elements, include occasion listeners, manipulate with CSS etc.

When you are composing a advice or anything that will be utilized by other those people than by by yourself you have to publish look at code. A lot of Test code. You will call for all around 50+ various expressions to attempt out if your calculator calculates them correctly. You will Likely fail a nicely deal of times Prior to you pass them all.

When the overall performance is carried out you need to add some design, some guidelines and the calculator is ready to be used.

What Do You Think?

 
About Online Calculator

Online Calculator is the best resource for up-to-date information on calculator

Live Feeds
Get in touch

Phone: 1.(855) 105-2535
Email: contact@