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;
}