Problem in stack(data structure in C)

Posted on 1998-11-19
Last Modified: 2010-04-15
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
Question by:tagheuer

Expert Comment

ID: 1254469
refer badrul's query.

Expert Comment

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

pseudo code :

double solve (equation)
   while ()
      isolate equation between ()   -> start with first ( and  
                                       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 ...


Author Comment

ID: 1254471
I want more details about the codes.

Expert Comment

ID: 1254472
Do your own homework.
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.


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?

LVL 27

Expert Comment

ID: 1254474
>> Do your own homework.

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.
LVL 10

Accepted Solution

rbr earned 30 total points
ID: 1254475
What further problems do you have?
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

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

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

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now