ummm...... how to evaluate the formulae??

M

Mars

if I want to write a program to evaluate a formulae,
what kind of algorithm should I use??

for example,
input:
(2+3)*(3/4)+6-8

how to deal with the brackets??
need to use stacks??

Thx.......




?_?
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
if I want to write a program to evaluate a formulae,
what kind of algorithm should I use??

for example,
input:
(2+3)*(3/4)+6-8

Google for "recursive descent parser"
how to deal with the brackets??

That's part of what the parser does.
need to use stacks??

Perhaps. Or not, as your implementation requires.
Thx.......




?_?


- --

Lew Pitcher, IT Specialist, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFCE1wnagVFX4UWr64RAmueAKC7pvDB+wDo3KYuoTRUE3Vnb5y4vwCgv3iE
lWW1zW5Dy21NDd/V9dE7DTw=
=yl+Y
-----END PGP SIGNATURE-----
 
S

Steven

Mars said:
if I want to write a program to evaluate a formulae,
what kind of algorithm should I use??

for example,
input:
(2+3)*(3/4)+6-8

how to deal with the brackets??
need to use stacks??

You can convert the algebraic expression to reverse polish notation.
Try searching for reverse polish notation. It is very effective with
computational expressions and parenthesis.
Try http://en.wikipedia.org/wiki/Reverse_Polish_notation
 
M

Mars

Lew Pitcher mentioned:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1



Google for "recursive descent parser"




That's part of what the parser does.




Perhaps. Or not, as your implementation requires.


THX~~
 
S

sipetar

Mars said:
if I want to write a program to evaluate a formulae,
what kind of algorithm should I use??

for example,
input:
(2+3)*(3/4)+6-8

how to deal with the brackets??
need to use stacks??

Thx.......




?_?
RPN calculator (reverse polish notation)
I' ve got one implementaion with stach so fi you need it I can send you
by mail..
 
G

Gregory Toomey

Mars said:
if I want to write a program to evaluate a formulae,
what kind of algorithm should I use??

for example,
input:
(2+3)*(3/4)+6-8

how to deal with the brackets??
need to use stacks??

Thx.......




?_?

Try flex/bison, or write you own lexical analyser & parser.
http://www.gnu.org/software/bison/bison.html

If you dont know the basics of BNF/recursive descent you wont get far.

gtoomey
 
E

Eric Sosman

Gregory said:
Mars wrote:




Try flex/bison, or write you own lexical analyser & parser.
http://www.gnu.org/software/bison/bison.html

If you dont know the basics of BNF/recursive descent you wont get far.

These tools are surely overkill for so small a problem.
A stack and a precedence table will do the job just fine
and with less fuss. Two stacks if you want to evaluate at
the same time you're parsing.
 
M

Mars

(e-mail address removed) mentioned:
RPN calculator (reverse polish notation)
I' ve got one implementaion with stack so fi you need it I can send you
by mail..


Great~
my email is : (e-mail address removed)

Thx~
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top