LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-16-2004, 05:11 AM   #1
miguetoo
Member
 
Registered: Mar 2003
Location: soCal..
Distribution: lfs server.. slackware workstation..
Posts: 58

Rep: Reputation: 15
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;

}

Last edited by miguetoo; 03-16-2004 at 05:13 AM.
 
Old 03-16-2004, 12:51 PM   #2
lucasbl3
LQ Newbie
 
Registered: Feb 2004
Posts: 8

Rep: Reputation: 0
I am sorry but what is your question?
Do you get an error or something?
 
Old 03-16-2004, 02:11 PM   #3
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
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.
 
Old 03-16-2004, 10:25 PM   #4
miguetoo
Member
 
Registered: Mar 2003
Location: soCal..
Distribution: lfs server.. slackware workstation..
Posts: 58

Original Poster
Rep: Reputation: 15
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..?

Last edited by miguetoo; 03-16-2004 at 10:29 PM.
 
Old 03-17-2004, 03:02 AM   #5
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
POSIX message queues(Solaris) to SYS V message queues(Linux) devershetty Programming 1 01-22-2007 11:15 AM
cout conversion to binary... audibel Programming 3 03-12-2005 08:06 AM
undefined symbol cout Tazzie76 Linux - Software 8 12-10-2004 03:09 PM
c++ cout not working :( Longinus Programming 10 08-01-2004 08:32 PM
C++ boolean in cout statement AMMullan Programming 2 06-08-2004 01:44 PM


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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration