4 Replies - 1348 Views - Last Post: 14 September 2014 - 04:46 PM Rate Topic: -----

#1 nairy991   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 10-October 13

How to loop through a stack and printout each of its elements

Posted 14 September 2014 - 10:33 AM

I have a program that consist of a postfix calculator for a class assigment, my program is basically done, however I am missing how to loop through the stack and print each element of it. My output is suppose to look something like this:

Token: 4+5 (my token would be the infix expression that the user inputs)
output:45+ (my output would be the postfix expression)
Stack(bottom to top) :empty + + empty (This is suppose to be the process of the stack, but I do not know how to get this values to print out)

and I already have the token and output part, however I do not know how could I get the stack values, in order to print them out.

My program is the following:
#include <sstream>
#include<iostream>
#include<cstring>
#include<iomanip>
#include <vector>
#include <math.h>
#include <windows.h>
using namespace std;

///////////////////////////////////// this is our stack class of type template ///////////////////////////////////////////////////

template <class type>
class Stack{
    int top;
    int max;
    type * array;
public:
    Stack(int n = 10);
    bool push (type data);
    bool pop(type & data);
    bool isFull();
    bool isEmpty();
    ~Stack();
};

template <class type>
Stack<type>::Stack(int n){
    top = -1;
    max = n;
    array = new type[max];
}

template <class type>
bool Stack<type>::push (type data){
    if(!isFull()){
        array[++top] = data;
        return true;
    }
    else
        return false;
}

template <class type>
bool Stack<type>::pop(type & data){
    if(!isEmpty()){
        data = array[top--];
        return true;
    }
    return false;
}
template <class type>
bool Stack<type>::isFull(){
    if(top == max-1)
        return true;
    else
        return false;
}
template <class type>
bool Stack<type>::isEmpty(){
    if(top==-1)
        return true;
    else
        return false;
}
template <class type>
Stack<type>::    ~Stack(){
    delete [] array;
}

////////////////////////////////////////////stack functions ends here ////////////////////////////////////////////////////


////////////////////////////////////////////////////Declared functions//////////////////////////////////////////////////////

int convert(char B)/>/>; ///////////////////convert char to integer/////////////////////////////////////////////////////////
int presi(char B)/>/>; ///////////////////// check presidence /////////////////////////////////////////////////////////////
bool rpn(char*,int&); //////////// evaluate post fix expression////////////////////////////////////////////////
void postfix(char*,char*,Stack<char>&a);////////// convert infix to postfix////////////////////////////////////////////
bool check(char); //////////////////////////// check whether our required operator comes or not //////////////////////
void print(char*);///////////////////////////////print post fix///////////////////////////////////////////////////////

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

/////////////////////////////////////////////////////main program////////////////////////////////////////////////////////////////
int main()
{
	
Stack<char> a;
char * expression;
expression=new char[30];
char * temp;
temp=new char[30];
int result=0;
bool flag=true;




cout<<"Enter an infix expression.This program will convert the infix expression to reverse polish notation.";
cout<<"When finished, enter '!' to stop the program." << endl;
cout<<endl;

while(true)
{

	cout<<"Enter an Infix Expression:";
	cin>>expression;
	if(string(expression).at(0) == '!')
	break;
	
///////////////////////////////function calling////////////////////////////////////////////////////////////////////
	postfix(expression,temp,a); // converting post fix to infix
	
	/*for(int i=0;expression[i]!='\0';i++ )
	{
		stack[i]=expression[i];
		cout << stack[i] << endl;
	}*/
	 
//////////////////////////////////////////////Table Display////////////////////////////////////////////////////////////
	cout << setw(5) << "Token" << 
	        setw(5) << " " <<
	        setw(5) << "Output" << 
			setw(5) << " " <<
			setw(5) << "Stack(bottom to top)" << endl;
	
	for(int i=0; expression[i]!='\0'; i++)
	{
		cout << setw(5) << expression[i] << 
		        setw(5) << " " <<
		        setw(5) << temp[i] << endl;
	}
	
	//////////////////////////////////////////////End of Table Display///////////////////////////////////////////////////////////
	
	
	flag=rpn(temp,result); /////////////////// evaluating post fix expression//////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	if(flag)
	{
	
	cout<<endl;
	cout<<" The rpn expression is:"<< " " << temp << " " << "=" << " " << result <<endl;
	}
	
	else
	{
	cout<<endl;
	cout<<"Invalid Expression : "<<endl;
	cout<<endl;
	}
	
	
}   
cout << "Program Over!" << endl;
exit(0);
}
/////////////////////////////////////////////////////////End of main function///////////////////////////////////////////////
int convert(char B)/>/>
{
switch(B)/>/>
{
case '0':
return 0;
break;
case '1':
return 1;
break;

case '2':
return 2;
break;
case '3':
return 3;
break;

case '4':
return 4;
break;

case '5':
return 5;
break;

case '6':
return 6;
break;

case '7':
return 7;
break;

case '8':
return 8;
break;
case '9':
return 9;
break;
}


}
int presi(char B)/>/>
{
switch(B)/>/>
{
case '|':
return 0;
break;
case '^':
return 1;
break;
case'=':
return 2;
break;
case '>':
return 3;
break;
case '<':
return 4;
break;
case '-':
return 5;
break;
case '+':
return 6;
break;

case '*':
return 7;
break;
case '/':
return 8;
break;
case '%':
return 9;
break;
case '!':
return 10;
break;
case '#':
return 11;
break;

}

return 0;
}
///////////////////////////////////// convert infix to postfix//////////////////////////////////////////////////////////////////////////
void postfix(char*expression,char*temp,Stack<char>&a)
{
char ch;

int len;
len=strlen(expression);

int it=0;

for(int i=0;i<len;i++)
{
if(expression[i]>=48&&expression[i]<=57)
{
temp[it]=expression[i];
it++;

}
else if(a.isEmpty() && check(expression[i]) )
{
a.push(expression[i]);

}
else if(!a.isEmpty() && check(expression[i]))
{
a.pop(ch);
a.push(ch);

if(expression[i]=='(')
{
a.push(expression[i]);
}
else if(presi(expression[i])<=presi(ch))
{
a.pop(temp[it]);
a.push(expression[i]);
it++;
}
else if(presi(expression[i])>presi(ch))
{
a.push(expression[i]);

}




}
else if(expression[i]==')')
{
a.pop(ch);
temp[it]=ch;
it++;

while(ch!='(')
{
a.pop(ch);
if(ch!='(')
{
temp[it]=ch;
it++;
}
}
}


}

if(!a.isEmpty())
{
while(!a.isEmpty())
{
a.pop(ch);
temp[it]=ch;
it++;
}
}
temp[it]='\0';



}


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

//////////////////////////////////////////////////// evaluate postfix///////////////////////////////////////////////////////////////////
bool rpn(char*expression,int &temp)

{
Stack<int> a;
int c1;
int c2;
int len=strlen(expression);
for(int i=0;i<len;i++)
{
if(expression[i]>=48&&expression[i]<=57)
{
temp=convert(expression[i]);
a.push(temp);

}
if(expression[i]=='+')
{
a.pop(c1);
a.pop(c2);
temp=c1+c2;
a.push(temp);
}
if(expression[i]=='-')
{
a.pop(c1);
a.pop(c2);
temp=c2-c1;
a.push(temp);
}
if(expression[i]=='*')
{
a.pop(c1);
a.pop(c2);
temp=c1*c2;
a.push(temp);
}
if(expression[i]=='/')
{
a.pop(c1);
a.pop(c2);
temp=c2/c1;
a.push(temp);
}
if(expression[i]=='>')
{
a.pop(c1);
a.pop(c2);
temp=(c2>c1);
a.push(temp);
}
if(expression[i]=='<')
{
a.pop(c1);
a.pop(c2);
temp=c2<c1;
a.push(temp);
}

if(expression[i]=='=')
{
a.pop(c1);
a.pop(c2);
if(c1==c2)
{
a.push(1);
}
else
{
a.push(0);
}
}

if(expression[i]=='|')
{
a.pop(c1);
a.pop(c2);
if((c1==0||c1==1)&&(c2==0||c2==1))
{
if(c1==0 && c2==0)
{
a.push(0);
}
else
{
a.push(1);
}
}
else
{
return false;
break;
}
}
if(expression[i]=='^')
{
a.pop(c1);
a.pop(c2);
if((c1==0||c1==1)&&(c2==0||c2==1))
{
if(c1==1 && c2==1)
{
a.push(1);
}
else
{
a.push(0);
}
}
else
{
return false;
break;
}
}
if(expression[i]=='%')
{
int t1=0;
int t2=0;
int mod=0;

a.pop(c1);
a.pop(c2);
t1=c1;
t2=c2;

mod=t2%t1;

temp=mod;


a.push(temp);
}

if(expression[i]=='!')
{
a.pop(c1);
if(c1==0||c1==1)
{
if(c1==1)
{
a.push(0);
}
else if(c1==0)
{
a.push(1);
}
}
else 
{
return false;
break;
}
}
   if(expression[i]=='#')
   {
a.pop(c1);
int aiwi;
aiwi=c1;
int t=0;
t=~aiwi+1;
a.push(t);
}





}
a.pop(temp);
return true;






}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


bool check(char a)
{
if (a=='('||a=='*'||a=='/'||a=='+'||a=='-'||a=='^'||a=='|'||a=='>'||a=='<'||a=='='||a=='%'||a=='#'||a=='!')
{
return true;
}
else
{
return false;
}
}

void print(char*temp)

{
for(int j=0;temp[j]!='\0';j++)
{
if(temp[j]!='(')
{
cout<<temp[j];
}
}

}




Any ideas on how to proceed with printing the stack so that it looks like my example output?

This post has been edited by nairy991: 14 September 2014 - 10:35 AM


Is This A Good Question/Topic? 0
  • +

Replies To: How to loop through a stack and printout each of its elements

#2 Salem_c   User is online

  • void main'ers are DOOMED
  • member icon

Reputation: 2295
  • View blog
  • Posts: 4,397
  • Joined: 30-May 10

Re: How to loop through a stack and printout each of its elements

Posted 14 September 2014 - 11:04 AM

http://en.wikipedia....ki/Indent_style

I'm guessing that the first 100 or so lines of nicely(ish) indented code is boilerplate given to you in a "fill in the blanks" exercise, and the other 400+ lines of code with NO indentation at all (and thus unreadable) is yours.

If you indent it, then more people will look at it.

Otherwise, we'll just go and do something less painful, like say have some root canal work done at the dentist.
Was This Post Helpful? 0
  • +
  • -

#3 nairy991   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 10-October 13

Re: How to loop through a stack and printout each of its elements

Posted 14 September 2014 - 11:10 AM

sorry, its just my program is divided by Stack.cpp and Stack.h and in order for everyone to see it better I included them in my code real quick and when I copied paste my main code with the others it past itself with no indentions.

#include<iostream>
#include<cstring>
#include<iomanip>
#include "stack.h"
using namespace std;

////////////////////////////////////////////////////local functions//////////////////////////////////////////////////////

int convert(char B)/>/>; ///////////////////convert char to integer/////////////////////////////////////////////////////////
int presi(char B)/>/>; ///////////////////// check presidence /////////////////////////////////////////////////////////////
bool rpn(char*,int&); //////////// evaluate post fix expression////////////////////////////////////////////////
void postfix(char*,char*,Stack<char>&a);////////// convert infix to postfix////////////////////////////////////////////
bool check(char); //////////////////////////// check whether our required operator comes or not //////////////////////
void print(char*);///////////////////////////////print post fix///////////////////////////////////////////////////////

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

int main()
{
	
Stack<char> a;
char * expression;
expression=new char[30];
char * temp;
temp=new char[30];
int result=0;
bool flag=true;



cout<<"Enter an infix expression.This program will convert the infix expression to reverse polish notation.";
cout<<"When finished, enter '!' to stop the program." << endl;
cout<<endl;

while(true)
{

	cout<<"Enter an Infix Expression:";
	cin>>expression;
	if(string(expression).at(0) == '!')
	break;
	
///////////////////////////////function calling////////////////////////////////////////////////////////////////////
	postfix(expression,temp,a); // converting post fix to infix
	
	
	 
	//////////////////////////////////////////////Table Display////////////////////////////////////////////////////////////
	cout << setw(5) << "Token" << 
	        setw(5) << " " <<
	        setw(5) << "Output" << 
			setw(5) << " " <<
			setw(5) << "Stack(bottom to top)" << endl;
			
		cout << setw(5) << "--------" << 
		    setw(0) << " " <<
	        setw(5) << "--------" << 
			setw(0) << " " <<
			setw(5) << "------------------------" << endl;		
	
	for(int i=0; expression[i]!='\0'; i++)
	{
		cout << setw(5) << expression[i] << 
		        setw(5) << " " <<
		        setw(5) << temp[i] << endl;
	}
	
	//////////////////////////////////////////////End of Table Display///////////////////////////////////////////////////////////
	
	
	flag=rpn(temp,result); /////////////////// evaluating post fix expression//////////////////////////////////
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	if(flag)
	{
	
	cout<<endl;
	cout<<" The rpn expression is:"<< " " << temp << " " << "=" << " " << result <<endl;
	}
	
	else
	{
	cout<<endl;
	cout<<"Invalid Expression : "<<endl;
	cout<<endl;
	}
}
cout << "Program Over!" << endl;
exit(0);
}
/////////////////////////////////////////////////////////End of main function///////////////////////////////////////////////

int convert(char B)/>/>
{
	switch(B)/>/>
	{
		case '0':
		return 0;
		break;
		case '1':
		return 1;
		break;
		
		case '2':
		return 2;
		break;
		case '3':
		return 3;
		break;
		
		case '4':
		return 4;
		break;
		
		case '5':
		return 5;
		break;
		
		case '6':
		return 6;
		break;
		
		case '7':
		return 7;
		break;
		
		case '8':
		return 8;
		break;
		
		case '9':
		return 9;
		break;
	}

      
}
int presi(char B)/>/>
{
	switch(B)/>/>
	{
		case '|':
		return 0;
		break;
		case '^':
		return 1;
		break;
		case'=':
		    return 2;
		break;
		case '>':
		return 3;
		break;
		case '<':
		return 4;
		break;
		case '-':
		return 5;
		break;
		case '+':
		return 6;
		break;
		
		case '*':
		return 7;
		break;
		case '/':
		return 8;
		break;
		case '%':
		return 9;
		break;
		case '!':
		return 10;
		break;
		case '#':
		return 11;
		break;
		
	}
	return 0;
}
///////////////////////////////////// convert infix to postfix//////////////////////////////////////////////////////////////////////////
void postfix(char*expression,char*temp,Stack<char>&a)
{
	char ch;
	
	int len;
	len=strlen(expression);
	
	int it=0;
	
	for(int i=0;i<len;i++)
	{
		if(expression[i]>=48&&expression[i]<=57)
		{
			temp[it]=expression[i];
			it++;
	
		}
		else if(a.isEmpty() && check(expression[i]) )
		{
			a.push(expression[i]);
			//cout<<"done";
		}
		else if(!a.isEmpty() && check(expression[i]))
		{
			a.pop(ch);
			a.push(ch);
	
			if(expression[i]=='(')
			{
				a.push(expression[i]);
			}
			else if(presi(expression[i])<=presi(ch))
			{
				a.pop(temp[it]);
				a.push(expression[i]);
				it++;
			}
			else if(presi(expression[i])>presi(ch))
			{
				a.push(expression[i]);
	
			}
	
	
	
	
		}
		else if(expression[i]==')')
		{
			a.pop(ch);
			temp[it]=ch;
			it++;
	
			while(ch!='(')
			{
				a.pop(ch);
				if(ch!='(')
				{
					temp[it]=ch;
					it++;
				}
			}
		}
	
	    
	}
	
	if(!a.isEmpty())
	{
		while(!a.isEmpty())
		{
			a.pop(ch);
			temp[it]=ch;
			it++;
		}
	}
	temp[it]='\0';
	
	
	
}


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

//////////////////////////////////////////////////// evaluate postfix///////////////////////////////////////////////////////////////////
bool rpn(char*expression,int &temp)

{
	Stack<int> a;
	int c1;
	int c2;
	int len=strlen(expression);
	for(int i=0;i<len;i++)
	{
		if(expression[i]>=48&&expression[i]<=57)
		{
			temp=convert(expression[i]);
			a.push(temp);
		
		}
		if(expression[i]=='+')
		{
			a.pop(c1);
			a.pop(c2);
			temp=c1+c2;
			a.push(temp);
		
		}
		if(expression[i]=='-')
		{
			a.pop(c1);
			a.pop(c2);
			temp=c2-c1;
			a.push(temp);
		}
		if(expression[i]=='*')
		{
			a.pop(c1);
			a.pop(c2);
			temp=c1*c2;
			a.push(temp);
		}
		if(expression[i]=='/')
		{
			a.pop(c1);
			a.pop(c2);
			temp=c2/c1;
			a.push(temp);
		}
	if(expression[i]=='>')
		{
			a.pop(c1);
			a.pop(c2);
			temp=(c2>c1);
			a.push(temp);
		}
	if(expression[i]=='<')
		{
			a.pop(c1);
			a.pop(c2);
			temp=c2<c1;
			a.push(temp);
		}
	
		if(expression[i]=='=')
		{
			a.pop(c1);
			a.pop(c2);
			if(c1==c2)
			{
				a.push(1);
			}
			else
			{
				a.push(0);
			}
		}
	
		if(expression[i]=='|')
		{
			a.pop(c1);
			a.pop(c2);
			if((c1==0||c1==1)&&(c2==0||c2==1))
			{
				if(c1==0 && c2==0)
				{
					a.push(0);
				}
				else
				{
					a.push(1);
				}
			}	
	else
	{
		return false;
		break;
	}
		}	
	if(expression[i]=='^')
		{
			a.pop(c1);
			a.pop(c2);
			if((c1==0||c1==1)&&(c2==0||c2==1))
			{
				if(c1==1 && c2==1)
				{	
					a.push(1);
				}
				else
				{
					a.push(0);
				}
			}
			else
			{
				return false;
				break;
			}
		}
	if(expression[i]=='%')
	{
		int t1=0;
		int t2=0;
		int mod=0;
	
		a.pop(c1);
		a.pop(c2);
		t1=c1;
		t2=c2;
	
		mod=t2%t1;
	
		temp=mod;
	
	
		a.push(temp);
	}
	
	if(expression[i]=='!')
	{
		a.pop(c1);
		if(c1==0||c1==1)
		{
			if(c1==1)
			{
				a.push(0);
			}
			else if(c1==0)
			{
				a.push(1);
			}
		}
		else 
		{
			return false;
			break;
		}
	}
	   if(expression[i]=='#')
	   {
		a.pop(c1);
		int aiwi;
		aiwi=c1;
		int t=0;
		t=~aiwi+1;
		a.push(t);
		}
	
	
	
	

	}
a.pop(temp);
return true;


/*
*/



}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


bool check(char a)
{
	if (a=='('||a=='*'||a=='/'||a=='+'||a=='-'||a=='^'||a=='|'||a=='>'||a=='<'||a=='='||a=='%'||a=='#'||a=='!')
	{
		return true;
	}
	else
	{
		return false;
	}
}

void print(char*temp)

{
	for(int j=0;temp[j]!='\0';j++)
	{
		if(temp[j]!='(')
		{
			cout<<temp[j];
		}
	}

}



Here is my original code which is indented, sorry and hope everyone can see it better now.

This post has been edited by nairy991: 14 September 2014 - 11:12 AM

Was This Post Helpful? 0
  • +
  • -

#4 infernorthor   User is offline

  • D.I.C Lover

Reputation: 362
  • View blog
  • Posts: 1,718
  • Joined: 07-February 14

Re: How to loop through a stack and printout each of its elements

Posted 14 September 2014 - 03:44 PM

Was it necessary to make your own Stack class? Either case maybe you should separate The Stack and the post fix stuff.

A stack for this should have some like

// make struct or class something like
struct Token{
   int type; // a number indicating whether a number or operator
   int value;
};

//and create somewhere else
Stack<Token>





And whatever you are doing in the convert function, don't use switch that could probably be written with a few lines.
Was This Post Helpful? 0
  • +
  • -

#5 nairy991   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 31
  • Joined: 10-October 13

Re: How to loop through a stack and printout each of its elements

Posted 14 September 2014 - 04:46 PM

View Postinfernorthor, on 14 September 2014 - 10:44 PM, said:

Was it necessary to make your own Stack class? Either case maybe you should separate The Stack and the post fix stuff.

A stack for this should have some like

// make struct or class something like
struct Token{
   int type; // a number indicating whether a number or operator
   int value;
};

//and create somewhere else
Stack<Token>



Thanks for the suggestion of the struct, and yes the class assigment stated that it was necessary to use the stack class like that. I would have preferred the c++ stack library but because it is a class assigment I had to do it this way.

And whatever you are doing in the convert function, don't use switch that could probably be written with a few lines.

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1