For this assignment due 3/4/2013, I have to write a program to read in and evaluate a post fixed expression. I also have to print out the expression read in followed by the results of the evaluated expression (while handling the add, subtract, and multiply operators) and handle multi digit numbers. The expressions I'm using are in the textle is PostFix.txt, which I've attached to this article so you can look at it The thing is that I can't use stacks in my code. Instead I have to use functions to act the same way as stack member functions. I also have to use characters and not strings.

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. You don't have to advice me on displaying the results; I'll handle that. But first, can you help fix this program?

Here's my code for the assignment:

//Autthor: Connor Wells
//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;
    }
}
Attachments
5 9 +
82 41 -
3 10 4 - *
13 18 * 140 80 - +
1 2 3 4 5 6 7 + + + + + +
1998 900 - 98 -
4596 4300 96 + - 2 2 * *
55 66 + 17 3 + 20 10 + * +
5 2 * 10 40 * + 5 20 * 70 50 - 20 30 40 + + - + 3 - +
123456 654321 + 123456 654321 + -
99999 2000 *

Your STACK array screams "stack" at me. If you must not use stacks then that may mean trouble for you.

Surely your MAX should be large enough to allow input hold your input file, so why is it only 50?

Why are you filling the output file with the first MAX characters of the input file with each character on its own line? You should also probably check to be sure that your stream hasn't failed after your read each character and before you use the character.

What is the purpose of the arguments that you are giving to top and pop?

Do you really want pop to return -999 if TOS != empty()?

I notice that TOS starts at 50, so the only way it can go is down. The first time push is called it should probably move TOS to 49, not 51. Otherwise, STACK[TOS] can't have any meaning.

Edited 3 Years Ago by bguild

I wrote this other program for the same assignemnt using stacks. It builds fine but when I run it, it print out gibberishish and then crashes. Can you help?

//A four-function postfix calculator.
#include <iostream>
#include <fstream>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#define MAX 50
using namespace std;

int main()
{
  stack<double> stackObject;
  double a, b;
  string s;
  int y = 0;
  int output;
  ifstream inputFile;
  ofstream outputFile;

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

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

    for (y = 0; y < MAX ; y++)
    {
    inputFile >> s[y];
    cout << s[y] << "\n";
    outputFile << s[y] << "\n";
    }
    while (s[y] != '\n')
    {

        if((s[y] == '*') || (s[y] == '/') || (s[y] == '-') || (s[y] == '+'))
        {

        a = stackObject.top();
        stackObject.pop();
        b = stackObject.top();
        stackObject.pop();
             if (s[y] == '+')
             {
                output = a + b;
             }
             if (s[y] == '-')
             {
                output = a - b;
             }
             if (s[y] == '*')
             {
                output = a * b;
             }
             if (s[y] == '/')
             {
                output = a / b;
             }
        stackObject.push(atof(s.c_str()));
            }
            y++;
    }


    while(s != "q")
  {
    cout << "Here 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;
}

The [] operator causes undefined behaviour on std::string when you give it an index that is beyond the end of the string. You go s[y] for all y from 0 to MAX - 1 without any guarantee that the string contains even close to MAX characters. Since the no-arguments constructor of string creates an empty string, you actually have a guarantee that string will be empty, and the first thing you are doing is overwritting the null character at the end of the string.

Typically, postfix is implemented as you did, with a stack-based approach. There are alternatives. It sounds like your professor is trying you to implement this in a recursive algorithm.

This article has been dead for over six months. Start a new discussion instead.