5 Replies - 7017 Views - Last Post: 01 March 2013 - 10:49 PM Rate Topic: -----

#1 KaijuX25  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 22-February 13

Postfix Expression Evaluation

Posted 28 February 2013 - 09:41 AM

My program needs to read in and evaluate a post fix expression read from an input file. I have to print out the expression read in followed by the results of evaluating said expression. I need to handle the addition, subtraction, and multiplication operators, as well as multi-digit numbers.

Now before I do all that, I wrote the program below to get an idea on how to do this. When I build it, the only warning I get is "comparison between signed and unsigned integer expression. So I run the program, enter an expression like "4(space)3(space)+(enter)" and then the program stops working. Can you look at the code and if there's something I'm missing?

#include <iostream>
#include <sstream>
#include <stack>
#include <limits>
#include <string>
#include <cctype>
#include <cstdlib>
using namespace std;

int main()
{
    string input;
    cout << "Enter a postfix expression: " << endl;
    getline(cin, input);


    std::stack<int>operation;
    stringstream temp;
    int number1;
    int number2;
    int output;
    int i = 0;
    while (i < input.length())
    {
        if (isdigit(input[i]))
        {
            operation.push(input[i]-'0');
        }
        else
        {
          number2 = operation.top();
          temp << operation.top();
          operation.pop();
          number1 = operation.top();
          temp << operation.top();
          operation.pop();
          switch(input[i])
          {
                  case '+': output = number1 + number2;
                  break;
                  case '-': output = number1 - number2;
                  break;
                  case '*': output = number1 * number2;
                  break;
                  case '/': output = number1 / number2;
                  break;
        }
        operation.push(output);
        }
        i++;
    }
    cout << "The result is: " << temp.str() << endl;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    return 0;
}



Is This A Good Question/Topic? 0
  • +

Replies To: Postfix Expression Evaluation

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4278
  • View blog
  • Posts: 13,433
  • Joined: 25-December 09

Re: Postfix Expression Evaluation

Posted 28 February 2013 - 10:58 AM

It doesn't stop working it crashes. Look at the following snippet.
          operation.pop();
          number1 = operation.top();

What happens when you try to access "top" after you have already popped the last element? Also you don't seem to be processing the whitespace properly.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,463
  • Joined: 05-May 12

Re: Postfix Expression Evaluation

Posted 28 February 2013 - 11:51 AM

Is that your code?
http://stackoverflow...ng-a-stack-in-c

If so, should we start calling you appleLady?

You wrote it back in 2011, and you're only having problems now?

This post has been edited by Skydiver: 01 March 2013 - 12:36 PM
Reason for edit:: Fixing my abuse of the English language.

Was This Post Helpful? 2
  • +
  • -

#4 KaijuX25  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 22-February 13

Re: Postfix Expression Evaluation

Posted 01 March 2013 - 03:57 PM

Sorry, I think you have me confused. I didn't get into Computer Science until Fall 2012.

Since we're talking, I learned at college that I have to write this program without using the stack operation. Instead, I have to use function that perform the same roles as stack operations. Well, I built the program and I ended up with these errors:
These are the errors I get when I build it:
-Lines 15 and 18 have too many arguments to functions int top & bool full.
-Lines 57, 59, and 104 say "error: at this point in file".

Again, it is due 3/4/2013. I know I'm on the right, but can you help fix this program? Also, any errors you point out will be appreciated.

Here's my code for the assignment:
//Autthor: My Name
//Date: 2/27/2013
//Theme: Post Fix Expression Evaluation
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#define MAX 50
using namespace std;

int push(int v);
int top();
int pop(int d);
bool empty();
bool full();
int TOS = 50;
char input[MAX];
int STACK[MAX];
int number1;
int number2;


int main()
{

    int i = 0;
    int output;
    ifstream inputFile;
    ofstream outputFile;

    inputFile.open("PostFix.txt");
    outputFile.open("Evaluation");

    cout << "Here are the postfix expressions.\n\n";
    outputFile << "Here are the postfix expressions.\n\n";

    for (int y = 0; y < MAX ; y++)
    {
    inputFile >> input[y];
    cout << input[y] << "\n";
    outputFile << input[y] << "\n";
    }

    while (input[i] != '\n')
    {
        if (isdigit(input[i]))
        {
            int x;
            x = input[i] - '0';
            push(x);
        }
        else if((input[i] == '*') || (input[i] == '/') || (input[i] == '-') || (input[i] == '+'))
        {
        number2 = top(i);
        pop(i);
        number1 = top(i);
        pop(i);
             if (input[i] == '+')
             {
                output = number1 + number2;
             }
             if (input[i] == '-')
             {
                output = number1 - number2;
             }
             if (input[i] == '*')
             {
                output = number1 * number2;
             }
             if (input[i] == '/')
             {
                output = number1 / number2;
             }
            push(i);
            }
            i++;
    }

    cout << "\nHere are the evaluated results.\n\n";
    outputFile << "Here are the evaluated results.\n\n";

    for (int y = 0; y < MAX ; y++)
    {
    cout << output << "\n";
    outputFile << output << "\n";
    }
    inputFile.close();
    outputFile.close();
    return 0;
}

int top()
{
    if(TOS != MAX)
    {
    return STACK[TOS];
    }
}
int push(int v)
{
    if(TOS != full(v))
    {
        TOS = TOS - 1;
        STACK[TOS] = v;
        return TOS;
    }
    else
    {
        number1 = STACK[++TOS];
    }
}

int pop(int d)
{
    if(TOS != empty())
    {
        return -999;
    }
  else
  {
    return STACK[TOS--];
  }
}

bool empty()
{
    if(TOS == MAX)
    {
        return TOS == MAX;
    }
}

bool full()
{
    if(TOS == 0)
    {
        return TOS == 0;
    }
}


Was This Post Helpful? 0
  • +
  • -

#5 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3662
  • View blog
  • Posts: 11,463
  • Joined: 05-May 12

Re: Postfix Expression Evaluation

Posted 01 March 2013 - 09:14 PM

In post #1, in the code that you claimed you wrote, you knew knew how to use a switch statement and push and pop values. Somehow you forgot how to do all that in post #4. How does that happen?

Anyway, post the actual errors you are getting, not your paraphrasing of the error message. They will help us better help you. Based on the errors you've described, it's pretty obvious that the error message is correct. Compare your function declarations on lines 15 and 18 with your function definitions for the same functions. Do the parameters match?
Was This Post Helpful? 0
  • +
  • -

#6 KaijuX25  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 26
  • Joined: 22-February 13

Re: Postfix Expression Evaluation

Posted 01 March 2013 - 10:49 PM

Yesterday, I got some help in my college's math lab. The person who helped me was a fellow and she gave me the lowdown on the program. I also watched a tegrity video my professor put on Blackboard and it told me about all this, though as you see I messed it up. Sorry about that.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1