Page 1 of 2

Performing number evaluations from input

Posted: Tue Jan 13, 2009 4:17 am
by johnny_control
A feature I would like to add to my personal site is the ability to allow users to enter in some numbers and have the executed, for instance:

Code: Select all

<form action="sum.php" method="post">
 <p>
  <input type="text" name="num1" />
  <select name="method">
   <option value="+">+</option>
   <option value="-">-</option>
   <option value="*">x</option>
   <option value="/">/</option>
  </select>
  <input type="text" name="num2" />
 </p>
</form>
I know that I could probably use eval() for this, but that is probably too dangerous. In the future I would like to turn it into a pretty cool app, where they can add parenthesis, etc.

Would anyone happen to know how I could create this?
Thanks!

Re: Performing number evaluations from input

Posted: Tue Jan 13, 2009 4:53 am
by jaoudestudios
I would do it using php, no need to drop into the shell. Once you have it working in php, then you can progressively enhance it with javascript :)

Re: Performing number evaluations from input

Posted: Tue Jan 13, 2009 4:53 am
by johnny_control
jaoudestudios wrote:I would do it using php, no need to drop into the shell. Once you have it working in php, then you can progressively enhance it with javascript :)
Indeed. I was planning on doing it in PHP. But I don't know where to start.

Re: Performing number evaluations from input

Posted: Tue Jan 13, 2009 5:03 am
by jaoudestudios
Its quite easy. What languages do you know, so I can try and find a similar one and relate the two together?

Something along these lines...

Code: Select all

 
<?php
if (!empty($_POST)) {
 // do your calculation
}
?>
<form action="sum.php" method="post">
 <p>
  <input type="text" name="num1" />
  <select name="method">
   <option value="+">+</option>
   <option value="-">-</option>
   <option value="*">x</option>
   <option value="/">/</option>
  </select>
  <input type="text" name="num2" />
 <input type="submit" name="submit" id="submit" value="Calculate" />
 </p>
</form>
 

Re: Performing number evaluations from input

Posted: Tue Jan 13, 2009 5:09 am
by johnny_control
I know PHP. Feel free to provide an example in PHP.

Re: Performing number evaluations from input

Posted: Tue Jan 13, 2009 5:16 am
by jaoudestudios
ok cool.

In the example I gave just put the conditions (add/subtract etc...) for each in the conditional statement and do the calculation. I would use a switch not elseif.

Re: Performing number evaluations from input

Posted: Tue Jan 13, 2009 5:19 am
by johnny_control
jaoudestudios wrote:ok cool.

In the example I gave just put the conditions (add/subtract etc...) for each in the conditional statement and do the calculation. I would use a switch not elseif.
That would be silly. Once users start to use parenthesis, or I add modulus then it would become code spaghetti.

Re: Performing number evaluations from input

Posted: Tue Jan 13, 2009 5:25 am
by jaoudestudios
johnny_control wrote:
jaoudestudios wrote:ok cool.

In the example I gave just put the conditions (add/subtract etc...) for each in the conditional statement and do the calculation. I would use a switch not elseif.
That would be silly. Once users start to use parenthesis, or I add modulus then it would become code spaghetti.
I thought you wanted it simple especially as you had nothing working yet. You can do it object orientated if you want. Create a calculation class with a method for each type of calculation.

Re: Performing number evaluations from input

Posted: Tue Jan 13, 2009 5:33 am
by jaoudestudios
I would start with a base class that has simple calculations (+,-,/,x), then extend (or however you prefer to do it) with additional modules (sin,cos,tan etc) one at a time and test as you go along, otherwise it will become messy. Get each module working then tidy it up before going on to the next one. Also if you have problems with a module you can just post that module here and we can help.

Re: Performing number evaluations from input

Posted: Tue Jan 13, 2009 5:35 am
by johnny_control
jaoudestudios wrote:I thought you wanted it simple especially as you had nothing working yet. You can do it object orientated if you want. Create a calculation class with a method for each type of calculation.
Ok. How would I go about creating a class that could handle, for instance:

Code: Select all

((100 * (985 + (12 * 8) + 98)) / 12.5)

Re: Performing number evaluations from input

Posted: Tue Jan 13, 2009 5:38 am
by jaoudestudios
How would the user add that info to your online calculator with 2 input fields? (num1 & num2)

Re: Performing number evaluations from input

Posted: Tue Jan 13, 2009 6:11 am
by johnny_control
jaoudestudios wrote:How would the user add that info to your online calculator with 2 input fields? (num1 & num2)
It would actually be using javascript to dynamically create elements, such as a opening parenthesis, a number, an addition/subtraction/division/multiplication/etc system, another number and finally a closing parenthesis.

Re: Performing number evaluations from input

Posted: Tue Jan 13, 2009 6:39 am
by jaoudestudios
johnny_control wrote:
jaoudestudios wrote:How would the user add that info to your online calculator with 2 input fields? (num1 & num2)
It would actually be using javascript to dynamically create elements, such as a opening parenthesis, a number, an addition/subtraction/division/multiplication/etc system, another number and finally a closing parenthesis.
I still recommend to do it with php and progressively enhance it with javascript later on. That way it downgrades gracefully.

Re: Performing number evaluations from input

Posted: Tue Jan 13, 2009 6:42 am
by johnny_control
jaoudestudios wrote:I still recommend to do it with php and progressively enhance it with javascript later on. That way it downgrades gracefully.
Right, okay. So do you know of any way to parse and execute an equation like I posted above?

Re: Performing number evaluations from input

Posted: Tue Jan 13, 2009 6:48 am
by jaoudestudios
((100 * (985 + (12 * 8) + 98)) / 12.5)
I would start by grabbing the inner 2 brackets with a regex and work outwards while keeping a running total. If this is looped it wont matter how many encapsulated brackets there are.

But like I said start simple.
Try something like this in this order...

Code: Select all

 
1. (2 + 3) * 2
2. (2 + 3) * (4 + 4)
etc...