Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

postfix calculator  using stacks

Posted on 1998-02-05
3
1,975 Views
Last Modified: 2009-07-22
I need to write a program that is a post fix calculator.
The program must be able to deal with the user either entering a floating point number or a '+' ,a '-',a '*' ,or a '/'.  If one or more numbers are entered and the user hits return before entering the operation symbols '*\+-', then the list of numbers must be printed out from the stack.  If a user enters an operation symbol (=-*/) then the operation must be performed on the two numbers previous to it and the result  must be placed on the stack which is then printed out.  If the user hits q then the current stck must be printed out. If the user enters an operation on the stack and only one number has been entered previously, then an error message must be printed.  How can i do this ?  The data is scanned in as a string..but then must be tested against atof...i have read that atofs behavior is undefined if it does not come against a number when called.  I don't know how to implement this all into a loop ? suggestions! please....  
0
Comment
Question by:lkitchen
  • 2
3 Comments
 
LVL 3

Accepted Solution

by:
q2guo earned 200 total points
ID: 1257182
I assume that you have a structure call stack
and it contains the following member functions

char pop(void);
void push(char x);
bool isEmpty();
----------------------------------------------------
#include <stdio.h>
#include <ctype.h>
main(void)
{
    Stack *postFixc = new Stack;
    char temp, opt1, opt2;
    char str1[2] = {' ', '\0'};
    char str2[2] = {' ', '\0'};

    do {
      temp = getNextChar();
      if (temp == '+') {
           if(!postFixc->isEmpty()) return 1;
           opt1 = postFixc->pop();
           if(!postFixc->isEmpty()) {
               while(!postFixc->isEmpty())
                   printf("%c\n", postFixc->pop())
               return 0;
           }
           opt2 = postFixc->pop();
           str1[0] = opt1;
           str2[0] = opt2;
           temp = atoi(str1) + atoi(str2);          
           printf("%d\n", temp);
           postFixc->push(temp);
      }
      elseif %2
0
 
LVL 3

Expert Comment

by:q2guo
ID: 1257183
Refer any standard book on Data structures and Algorithms. One of the first things that you will find is stacks and post fix notation.
0
 

Expert Comment

by:bagalp
ID: 1257184
See page 75, K&R, "The C Programming Language"
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

840 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