LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   cout queues (https://www.linuxquestions.org/questions/programming-9/cout-queues-158330/)

miguetoo 03-16-2004 04:11 AM

cout queues
 
i want to output a prefix expression to a postfix expression.. most of the stuff are converted using queues and stacks..

here's the code:

Code:

#include <ctype.h>

#include <iostream>

#include <string>

#include "/root/Projects/lab2/include/utility.h"

#include "/root/Projects/lab2/include/queue.h"

#include "/root/Projects/lab2/include/stack.h"

using namespace std;



void input (Queue &);

void pre_to_post (Queue &, Queue &);

void output (Queue);

bool is_operator (char);



main ()

{

/*        Pre:        iostream.h, utility.h, Queue.h, and Stack.h included.

        Post:        Program has completed successfully. */

        Queue prefixQ, postfixQ;

        char answer;



        do

        {

                input (prefixQ);

                pre_to_post (prefixQ, postfixQ);

                output (postfixQ);

                cout << "Continue? N for NO: ";

                cin >> answer;

        }

        while( !( answer == 'n' || answer == 'N' ) );

        return 0;

}







bool is_operator (char c)

{

//        Pre:        None.

//        Post:        Returns true if valid arithmetic operator, false if not.

       

        return ((c == '+') || (c == '-') || (c == '*') || (c == '/')) ? true : false;

}







void input( Queue& prefixQ )

{

//        Pre:        prefixQ in main has been initialized.

//        Post:        prefixQ now contains a valid prefix expression.

       

        bool error;

        char temp[MAXQUEUE];

        int i;

        do

        {

                error = true;

                cout << "\nEnter a prefix expression: ";

                cin.getline(temp, MAXQUEUE, '\n' );



                for( i = 0; temp[i] != NULL && i < MAXQUEUE; i++ )

                {

                        if( !( isalnum( temp[i] ) ||  is_operator( temp[i] ) ) )

                        {

                                error = true;

                                cout << "\nERROR... Invalid characters. Try again.";

                        }

                        else

                                error = false;

                }

        }

        while( error );



        for( i = 0; temp[i] != NULL; i++ )

                prefixQ.append( temp[i] );

}







void pre_to_post( Queue& prefixQ, Queue& postfixQ ){

//        Pre:        prefixQ hold valid prefix expression. postfixQ initialized.

//        Post:        postfixQ holds the equivalent postfix expression( to prefixQ ).

        char c;

        Stack opStack;

        Stack_entry temp;

       

        while( !prefixQ.empty() )

        {

                prefixQ.retrieve (c);

               

                if (is_operator (c))

                {               

                        temp.element = c;

                        temp.flag = false;

                        opStack.push( temp );



//                        cout << "\n\n";

//                        opStack.showContents();

//                        cout << "--------opStack--------\n" ;

//                        cout << "Found an operator, pushing onto stack.\n";

//                        cout << "prefixQ->\n";

//                        prefixQ.changeToString();

//                        cout << "postfixQ->\n";

//                        postfixQ.changeToString();

                }

                else if (isalnum (c))

                {

                        postfixQ.append (c);                        //append char to postfixQ

                        opStack.top (temp);                        //view top of stack & copy

                        if (temp.flag)

                        {                                //if flag is on

                                do

                                {

                                        postfixQ.append (temp.element);

                                        opStack.pop();                       



//                                        cout << "\n\n";

//                                        opStack.showContents();

//                                        cout << "--------opStack--------\n";

//                                        cout << "True flag found, appending to postfixQ.\n";



                                        opStack.top (temp);

                                }

                                while (temp.flag);                //continue to append true ops to postfixQ

                        }

                        else

                        {                                                        //else flag is not on

                                temp.flag = true;                                //switch flag of copy on

                                opStack.pop ();                                        //pop old false op
                                opStack.push (temp);                        //push new with true op

//                                cout << "\n\n";
//                                opStack.showContents();
//                                cout << "--------opStack--------\n";
//                                cout << "Flag changed from false to true.\n";
                        }//  /*-A+BCDE
                }               
                prefixQ.serve();

        } //while
       
        postfixQ.append( temp.element );
        opStack.pop();


//        cout << "\n\n";
//        opStack.showContents();
//        cout << "--------opStack--------\n";
//        cout << "Last guy.\n";

        while( !prefixQ.empty() ) prefixQ.serve();
}



void output( Queue postfixQ )
/*        Pre:        postfixQ holds valid postfix expression.
        Post:        postfixQ now output to screen. */
{
        int i=0;
        cout << "blah postfixQ blah";
        for (i; i<MAXQUEUE; i++)
                cout << "Postfix is " << postfixQ.retrieve() << endl;

}




this is the only part i'm concerned with..

Code:

void output( Queue postfixQ )
/*        Pre:        postfixQ holds valid postfix expression.
        Post:        postfixQ now output to screen. */
{
        int i=0;
        cout << "blah postfixQ blah";

        for (i; i<MAXQUEUE; i++)
              cout << "Postfix is " << postfixQ.retrieve() << endl;
}




this is my queue.h file..

Code:

#ifndef QUEUE_H

#define QUEUE_H



typedef char Queue_entry;



const int MAXQUEUE = 10;



class Queue{

public:

        Queue();

        bool empty() const;

        Error_code serve();

        Error_code append(const Queue_entry &item);

        Error_code retrieve(Queue_entry &item) const;

//////////////////////////////////////////////////////////

        void changeToString();

///////////^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

protected:

        int count;

        int front, rear;

        Queue_entry entry[MAXQUEUE];

};



#endif


this is the queue.cpp file..

Code:

#include <iostream.h>

//////////^^^^^^^^^^^^^^^^^^^^^^^^^^^^

#include "/root/Projects/lab2/include/utility.h"

#include "/root/Projects/lab2/include/stack.h"

#include "/root/Projects/lab2/include/queue.h"



Queue::Queue(){

/* Post: Queue initialized to empty. */

        count = 0;

        rear = MAXQUEUE - 1;

        front = 0;

}



bool Queue::empty() const{

/* Post Returns true if the Queue is empty, otherwise return false. */

        return count == 0;

}



Error_code Queue::append( const Queue_entry &item ){

/* Post:        "item" is added to the read of the Queue. If the Queue is full, return an

                        Error_code of overflow and leave the Queue unchanged. */

        if( count >= MAXQUEUE) return overflow;

        count++;

        rear = ( ( rear + 1 ) == MAXQUEUE ) ? 0 : ( rear + 1 );

        entry[rear] = item;

        return success;

}



Error_code Queue::serve(){

/* Post:        The front of the Queue is removed. If the Queue is empty return an

                        Error_code of underflow. */

        if( count <= 0 ) return underflow;

        count --;

        front = ( ( front + 1 ) == MAXQUEUE ) ? 0 : ( front + 1 );

        return success;

}



Error_code Queue::retrieve( Queue_entry &item ) const{

/* Post:        The front of the Queue retrieved to the output parameter item. If the

                        Queue is empty return an Error_code of underflow. */

        if( count <= 0 ) return underflow;

        item = entry[front];

        return success;

}


lucasbl3 03-16-2004 11:51 AM

I am sorry but what is your question?
Do you get an error or something?

Mohsen 03-16-2004 01:11 PM

Check your program to determine a part of it if it has some problems (the program posted is too large!, I don't like to read all of that). Please specify a short (5~35 lines) of your code.

miguetoo 03-16-2004 09:25 PM

i want to output the value of postfixQ in the output () function..

the queue.h and queue.cpp file was only included if you needed to take a peek at how class Queue works..

postfixQ and prefixQ are both classes of Queue.. to put it short, i rearranged prefixQ to look different in postfixQ using queues in the pre_to_post () function.. i am unfamiliar on how to output contents of a queue (FIFO)..

suggestions..?

Mohsen 03-17-2004 02:02 AM

I'm not sure, but if your postfix wxpression is succesfully made, just output them until underflow returned from output().
What's the problem with output() function?
To extract output of a Q you should just DeQueue() while your Q is not empty.


All times are GMT -5. The time now is 03:09 PM.