Solved

# Problem in stack(data structure in C)

Posted on 1998-11-19
349 Views
How to write a program using stack (in C language)which functions almost like a calculator. the program will receive inputs like for example : 12 * (15 - 6) + 388
The program will calculate +, -, *, /, (), operations.
The priority operations are :

1st. ( )
2nd. *, /
3rd. +, -

The program will print the operation for example like The question above :

15 - 6
12 * 9
108 + 388
= 496
0
Question by:tagheuer

Expert Comment

ID: 1254469
0

LVL 1

Expert Comment

ID: 1254470
I would solve this problem with a recursive function

pseudo code :

double solve (equation)
{
while ()
stop if you have as many (
as )
solve (equation between () )  -> same function
substitute result in equation

while *, /
isolate equation with * or /
solve (equation with * or /)
substitute result in equation

now you have an equation with only + and - => add (equation)
return result
}

hope this helps ...

Stefaan
0

Author Comment

ID: 1254471
I want more details about the codes.
0

Expert Comment

ID: 1254472
0

LVL 1

Expert Comment

ID: 1254473
>> I want more details about the codes

Can you be a little bit more specific? What part do you not understand?

Stefaan
0

LVL 27

Expert Comment

ID: 1254474

Quite, but what you need is a Reverse Polish Expression Evaluator. Take virtually any text book and look up Reverse Polish. You usually find the algorithm there. I'm NOT going to hack one in.

Furthermore you should now give the points away to the first person (Rat excepted) who can tell you the actual NAME of the person after whom the algorithm is called. His name is rarely used since it's almost unpronounceable.
0

LVL 10

Accepted Solution

rbr earned 30 total points
ID: 1254475
What further problems do you have?
0

LVL 84

Expert Comment

ID: 1254476
Jan Lukasiewicz
But rather than bother converting to his notation, I'd probably do it with either a recursive descent, or an inside-out evaluator
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: Iâ€™ve never triâ€¦
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were smallâ€¦
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.