Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,606 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 905 people online right now. Registration is fast and FREE... Join Now!




stack implementation

 
Reply to this topicStart new topic

stack implementation, C++ stack implementation evaluating postfix expressions

nawa
post 8 Jul, 2008 - 03:24 AM
Post #1


New D.I.C Head

*
Joined: 2 Jun, 2008
Posts: 1

cpp
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<process.h>
#include<iomanip.h>
struct node
{
int data;
node *next;
};
node *top;
//node *s;
void initialize()
{
top=NULL;
}

void push(int x)
{
node *p=new node;
if(p!=NULL)
{
if(top==NULL)
{
p->data=x;
p->next=NULL;
top=p;
}
else
{
p->next=top;
top=p;
}
}
else
{
cout<<"\t\tError! The system run out of memory"<<endl;
exit(1);
}
cout<<top->data;
}

int pop()
{
node *p=top;
if(top!=NULL)
{
top=p->next;
return p->data;
}
cout<<"\t\tError! The stack is empty"<<endl;
return 0;
}

void main()
{
clrscr();
int value,x; int y,z;

initialize();
const int size=50;
int e[size];
cout<<"\n\n\t\t\tEnter Postfix expression\n"<<endl;
cout<<"\n\n\t\t\t============================="<<endl;
cin.getline(e,100)
for(int i=0;i<strlen(e);i++)
{
if(e!='\0')
if((e[i]>='0')&&(e[i]<='9'))
{
int x;
x=<int>(e[i]);//casting
push(x);
}
else if(e[i]=='+' || e[i]=='*' || e[i]=='-' || e[i]=='/')
{
y=pop();
z=pop();
}
if(e[i]=='+')
{
value=y+z;
push (value);
}
else if(e[i]=='-')
{
value=y-z;
push (value);
}
else if(e[i]=='*')
{
value=y*z;
push (value);
}
else if(e[i]=='/')
{
value=y/z;
push (value);
}
}
getch();
}


/* This program generates errors, but I can't correct it . Thank you ery much for your help ! */


Mod Edit:
Please code.gif
Thanks, gabehabe smile.gif
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 8 Jul, 2008 - 03:26 AM
Post #2


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,439



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


You might get better responses in the C/C++ forum.

Also, please post the errors that you are receiving.

*** Moved to C/C++ ***
User is online!Profile CardPM

Go to the top of the page

captainhampton
post 8 Jul, 2008 - 04:24 AM
Post #3


Jawsome++;

Group Icon
Joined: 17 Oct, 2007
Posts: 518



Thanked 2 times

Dream Kudos: 825
My Contributions


Are you just showing us the code? What problems are you having?
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 02:31AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month