View Full Version : Math Parser thingy.....it's kinda cool

jverkoey

06-15-2005, 05:04 PM

So I was bored, and now I'm a bit less bored, but basically I've gone and set up an apache server on my computer and had no real use for it...so I made something!

Anyways, what it is is just a really simple script, you can type in a math expression and then I have a program that's polling a folder where the expressions are placed, and once it finds one, it parses it and returns some html code showing how it was parsed and such....kinda fun to play with if you like parsers.

Here we go, have fun:

http://thejefffiles.cjb.net/mathparser.php

And if you're bored you can look at the stats of my computer....

http://thejefffiles.cjb.net/

jverkoey

06-15-2005, 05:15 PM

Bonus points to anyone who can figure out how to crash the parser, haha.

B0bDole

06-15-2005, 05:54 PM

Here is your statement's results:

There was an error in your math string...

gcn_zelda

06-15-2005, 06:03 PM

What?! I put 3/0 and the result was 0.

Something's wrong here.

jverkoey

06-15-2005, 06:13 PM

I put in testing for divide by zero statements.....because I was afraid it would crash the parser.

jverkoey

06-15-2005, 06:14 PM

Here is your statement's results:

There was an error in your math string...

There was an error in your string evidently.

caroundw5h

06-15-2005, 07:00 PM

why not put a meta refresh code in your html to refresh after 30 seconds. letting the client manually press it is kinda .... :rolleyes:

nice idea though.

jverkoey

06-15-2005, 08:26 PM

Got the meta refresh and it polls every 10 seconds now instead of 30.

jverkoey

06-16-2005, 01:12 AM

I put up a diagram thingy if anyone's interested in how my parser works. It's only a really simple expression so it doesn't show order of operations, recursion with parenthesis, functions or variables though....but it's all I've gotten done so far.

http://thejefffiles.cjb.net/mathparser.php

vasanth

06-16-2005, 04:25 AM

nice... more then the actual parser i liek the way the system works..

It doesn't handle trig functions.

7smurfs

06-16-2005, 07:55 AM

Here is your statement's results:

Original expression: (3/2)+((14+5)/(6-1.5))

Infix expression:

LPAR

3.000000

DIV

2.000000

RPAR

ADD

LPAR

LPAR

14.000000

ADD

5.000000

RPAR

DIV

LPAR

6.000000

SUB

1.500000

RPAR

RPAR

Postfix expression:

3.000000

2.000000

DIV

14.000000

5.000000

ADD

6.000000

1.500000

SUB

DIV

ADD

The result is: 5.72222

7Smurfs approves this parser ;)

gcn_zelda

06-16-2005, 10:54 AM

Does it handle exponents yet?

I want to see what it'll do if I try to raise a negative exponent to the 1/2 power or something :P

jverkoey

06-16-2005, 11:27 AM

It handles trig functions and exponents, it always has.

The following functions are implemented:

sqrt

sin

cos

tan

atan

asin

acos

log

abs

and exponents just use the ^ operator. There is a flag you can set when you compile the math parser to change ^ to the XOR operator, but since I'm using this as a math expression evaluator, it's just a power operator right now.

http://thejefffiles.cjb.net/mathparser.php?op=view&id=83

(-1)^(-1/2)

gcn_zelda

06-16-2005, 11:57 AM

Oh, okay. I didn't know that.

Sounds good.

It handles trig functions and exponents, it always has.

Hmm... not very well then. cos(90) according to your parser is like -.447.... when the answer is actually 0. Same with sin.

Zach L.

06-17-2005, 06:35 AM

Radians... ;)

Bah... how many people actually want it in radians tho?

gcn_zelda

06-17-2005, 09:54 AM

So you can use pi, then?

Do you just type in "pi," or what?

jverkoey

06-17-2005, 01:07 PM

Um, I could define pi as a variable I suppose.

Here, I'll put some variables in there that you guys can mess with.

jverkoey

06-17-2005, 01:53 PM

I have defined a few variables for you to mess around with now: x, y, pi (const at 3.14159265), and array (a 25 value array).

Example: sin(pi) or array[3]=x

Variable values are kept between operations. So if you do array[3]=5 in one statement, the next time you call array[3], it will still be 5, assuming no one else changed it.

There we go:

x

y

array[25]

pi (const)

Powered by vBulletin® Version 4.2.5 Copyright © 2019 vBulletin Solutions Inc. All rights reserved.