Posted by Kory Spansel on 2000-08-08
Hmm...I don't know pascal, but create a simple stack with some functions to
push and pop data. There is a format that makes parsing mathematical
expressions easy. I can't remember what it's called but it has this format:
5 5 +
so you get the first operand 5, push it on the stack, get the next operand
5, and push it on the stack, and then when you get the next token which is
an operator you pop the two previous operands and evaluate them according to
the operator. ie.
while (moretokens)
token = nexttoken()
if token = operator
a = pop()
b = pop()
result = evaluate(a, b, token)
push (result)
else
push (token)
end if
repeat
You could easily modify this to take expressions in the form of a + b, and
to take into account for parenthesis, order of operations, etc.
if token = "(" then
token = nexttoken()
while (token != ")")
if token = operator
.
.
.
else
push(token)
end if
token = nexttoken()
repeat
end if
something along those lines...
Kory
> Need Help in Pascal (stack)
> I'm a beginner in this language. I'm very confuse in coding the stack
> because its very complicated for me. I need help in writing a program in
> basic stacks or binary tree (for me to understand it easier), which can
> accepts mathematical expression in string format and delivers the
> answer(like a calculator).
> it must has the ability to convert string into integer or real
> and can detect expression character like:"( ) - x + / ^ ".
> I hope u can help me
>
> thanks
> vee
Previous post | Next post | Timeline | Home