need help with a stack assignment

Discussion in 'C++' started by KraKen, May 9, 2012.

  1. KraKen

    KraKen New Member

    May 9, 2012
    Likes Received:
    Trophy Points:
    ok this is for a data structures class, i just need help getting the code to run correctly

    the assignment is as follows below.

    Your assignment is to write a program that converts a prefix expression to a postfix expression.
    The input file "" contains a series of error-free, simple arithmetic expressions in prefix notation. There is one expression per line. The expressions are made up of two types of symbols: (1) single-letter operands and (2) the operators +, -, *, and /. There may be zero, one, or more blanks between symbols.
    All output should be written to the output file, "Postfix.out". Each prefix expression from the input should be echoprinted, with an appropriate label. After your program converts the input expression to its postfix equivalent, the postfix expression should be printed to the output file, with an appropriate label.
    Prefix: + * A B / C D
    Postfix: A B * C D / +
    Prefix: * A + B / C D
    Postfix: A B C D / + *
    Prefix: - * A + B / C D E
    Postfix: A B C D / + * E -
    Prefix: * + A B - C D
    Postfix: A B + C D - *
    Although your program does not process or print infix, here are the four infix expressions that correspond to the above forms:
    A * B + C / D
    A * (B + C / D)
    A * (B + C / D) - E
    (A + B) * (C - D)
    The key idea here is that after a portion of an expression has been converted, it is to be treated as a single operand. Assume that every operator is associated with a flag. The flag is initially "off" to signal that neither of the two operands corresponding to the given operator has been processed yet. When the first operand is processed, the flag is switched "on." When the second operand is processed, the given operator may immediately be appended to the output string.
    Below is a description of a simple prefix-to-postfix conversion algorithm that works on one input expression:
    1. Initialize the input string, the output string, and the operators stack.
    2. Repeat steps 3-5 until there are no more input symbols.
    3. Get the next input symbol.
    4. If it is an operator, put it on the stack with its associated flag off.
    5. If it is an operand, append it to the output string. Furthermore, the operand must correspond to the top operator on the stack. Of course, the stack cannot be empty at this point. (Why?) Finally, here is the important question: is the operand just processed the first or the second operand associated with the top operator? If it is the second, it is time to append the top operator to the output string. Now, remembering the key idea at the beginning of this section, determine what happens to the remaining operators on the stack.

    #include <iostream>
    #include <string>
    #include <fstream>
    using namespace std;
    bool isOperator(char symbol) //definition of isOperator
    if( (symbol == '*') || (symbol == '+') || (symbol == '/') || (symbol == '-' ) )
    return true;
    return false;
    class Stack {
    int MaxStack;
    int EmptyStack;
    char* items;
    int top;
    int TOP();
    void push(char);
    char pop();
    int empty();
    int full();
    Stack::Stack(int size) {
    MaxStack = size;
    EmptyStack = -1;
    top = EmptyStack;
    items = new char[MaxStack];
    Stack::~Stack() {delete[] items;}
    int Stack::TOP(){
    return items[top];
    void Stack::push(char c) {
    items[++top] = c;
    char Stack::pop() {
    return items[top--];
    int Stack::full() {
    return top + 1 == MaxStack;
    int Stack::empty() {
    return top == EmptyStack;
    int main()
    cout <<"WELCOME TO PROJECT 4 PROGRAM"<<endl;
    // basic components
    string line;
    string outputLine;
    Stack stack(20);
    char c;
    ifstream myfile ("");
    if (myfile.is_open())
    while ( myfile.good() )
    cout << line << endl;
    else cout << "Unable to open file";
    //flags, operator method
    while (line.length()!=0) //while not end of blank line
    Stack flags(20);
    Stack operators(20);
    for(int i=0; i < line.size(); i ++) //reiterate through ‘line’
    char symbol = line [i];
    if( isOperator(symbol)) //if the symbol is an operator
    operators.push(symbol); //push operator on stack
    flags.push(0); //push associated flag on stack
    if((symbol != ' ') && !isOperator(symbol)) //then it’s a operand
    outputLine += symbol; //append
    while(flags.TOP()) //while top flag is ON on stack
    outputLine += operators.TOP() ; //append the associated op
    operators.pop(); //remove operator from stack
    flags.pop(); //remove flag from stack
    flags.pop(); //set next flag ON
    }//end of for
    if(!operators.empty() || (!flags.empty()) )
    cout << "SOMETHING WENT WRONG. Prob incorrect input" << endl;
    cout << line << endl;
    cout << outputLine << endl;
    } //end of while eof
    ofstream outfile ("Postfix.out");
    if (outfile.is_open())
    outfile << outputLine << endl;
    outfile << line << endl;
    else cout << "Unable to open file";
    return 0;
    basically the program is compiling but when i run it it goes crazy and crashes, the program is supposed to do the following:
    read prefix from file one of the prefix stated in the assignment )
    store in stack, turn flag off and on for op, rearange to postfix notation
    cout the Postfix
    send postfix to file POSTFIX.out
    but its not doing this at all , my logic should be correct as my instructor gave me a the implementation of this in the driver. so any edit to this that can get it running and functional to the specification of the assignment is GREATLY appreciated

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice