Thread: postfix calculator problem???

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    1

    postfix calculator problem???

    I want to be able to evaluate an expression typed by the user (ie 23+5-4, 23-4-5, 2+5+6+7 etc...any length). The expression contains only integers, '+', and '-' (no parentheses). I have worked on the following for a hours now...it crashes when I run it after it prompts for the input. I do not know what is wrong. Can anyone tell me what is wrong with my code ( I am programming in VC++ --which later I have to make g++ compatible). Thanks very much. -chanthee
    (You must forgive me if this looks ugly...but I don't know how to set tags...I've never posted before)
    Code:
    #include <iostream>
    #include <cstring>
    #include <cstdlib>
    #include <cctype>
    #include <stack>
    
    using namespace std;
    
    int read_and_evaluate(istream& ins);
    
    void evaluate_expression(stack<int>& numbers, char& symbol);
    
    
    
    int main()
    { int answer;
    
    cout << "Enter an arithmetic expression: "<< endl;
    answer = read_and_evaluate(cin);
    cout << "The answer is "<< answer << endl;
    
    return EXIT_SUCCESS;
    }
    
    int read_and_evaluate(istream& ins)
    {
    
    stack<int> numbers;
    int number;
    char symbol;
    
    do
    {
    if (isdigit(ins.peek()))
    {
    ins >> number;
    numbers.push(number);
    }
    else 
    { 
    ins >> symbol;
    evaluate_expression( numbers, symbol);
    }
    
    }
    while (ins && ins.peek() != '\n');
    
    return numbers.top();
    
    }
    
    void evaluate_expression(stack<int>& numbers, char& symbol)
    {
    int operand2, operand1;
    
    
    operand2 = numbers.top();
    numbers.pop();
    operand1 = numbers.top();
    numbers.pop();
    
    switch(symbol)
    {
    case '+': numbers.push(operand1 + operand2);
    break;
    
    case '-': numbers.push(operand1 - operand2);
    break;
    
    
    }
    }
    Code tags added by Kermi3

  2. #2
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    To use code tags, try this:
    |code|
    paste your code here
    |/code|

    Of course, change | | to [] according to the right direction, like html tags.

  3. #3
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    Ok before the normal code tags post I wanted to add this:

    You say sorry for no code tags you don't know how because this is your first time posting here. Well not to be mean, but if you don't know something, and more importantly if it's your first time posting here, think it might be a good idea to read the thread labeled: "*READ THIS FIRST* Guidelines to the Board and hints for help" before you post?

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  4. #4
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    First off you can't take in an expression like "3+3" and have it put the threes into ints and the + into a char. The program will try to put the whole expression into an int which will cause an error. You need to to take it all in as a string and parse it character by character. If the numbers can only be single digit, then to convert '3' to 3 you just subtract '0'. If the numbers can be more than one digit, then you need to create a function to convert it.

    Second, unless the project stated that you must do this, I don't see any reason to pass an istream object to your funtion. Personally I would do something like:
    Code:
    int read_and_evaluate(void)
    {
    string input;
    stack<int> numbers;
    int number;
    char symbol;
    
    cin>>input;
    
    for(int x=0; x<input.length(); x++)
    {
         // if input[x] is a number, add to stack (after checking to see how many digits)
         // if input[x] is + or - then evaluate
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Expression: Convert infix notation to postfix notation.
    By Nutshell in forum C Programming
    Replies: 7
    Last Post: 02-27-2010, 07:44 AM
  2. Replies: 4
    Last Post: 03-12-2006, 02:17 PM
  3. Just a postfix clarification.
    By SlyMaelstrom in forum C++ Programming
    Replies: 3
    Last Post: 12-08-2005, 09:47 AM
  4. Infix to Postfix
    By dat in forum C Programming
    Replies: 6
    Last Post: 06-16-2003, 08:46 AM
  5. postfix
    By datainjector in forum C Programming
    Replies: 11
    Last Post: 11-20-2002, 08:33 PM