Stack Implementation Code

A

AshifToday

Expression Evaluation in Post-Fix Notation Using Stack
=====================

/*
This program has been made by Ashif Zubair.
Roll # 1468, B.Sc.(Hons.) in C.S.
ALL RIGHTS ARE RESERVED TO ASHIF ZUBAIR.
PROGRAM OBJECTIVE :-

*/
#include <iostream.h>
#include <conio.h>

class stack
{
private:
char *sspace;
int tos;
public:
stack();
int ssize;
void push(char);
char pop();
int isempty();
int isfull();
}stk;

/* Class Functions */
stack::stack()
{
clrscr();
tos = -1;
cout<<"Enter Stack Size : ";
cin>>ssize;
clrscr();
}

void stack::push(char ch)
{
if(!isfull())
{
tos++;
sspace[tos] = ch;
}
}

char stack::pop()
{
if(!isempty())
{
char ch;
ch = sspace[tos--];
return ch;
}
}

int stack::isfull()
{
if(tos > ssize)
return 1;
else
return 0;
}

int stack::isempty()
{
if(tos == -1)
return 1;
else
return 0;
}
/* Class Functions Finished */


/* Main Function */

void main (void)
{
clrscr();
char exp[100];
char get=NULL;
char temp=NULL;
int i=0;
int open = 0;
int close = 0;
int high = 0;
cout<<"Enter Your Expression :\nPress x Key When Finished\n\t\t\t";
while(get != 'x'|| get != 'X')
{
get = getche();
switch(get)
{
case '0' :
exp[i++] = get;
break;
case '1' :
exp[i++] = get;
break;
case '2' :
exp[i++] = get;
break;
case '3' :
exp[i++] = get;
break;
case '4' :
exp[i++] = get;
break;
case '5' :
exp[i++] = get;
break;
case '6' :
exp[i++] = get;
break;
case '7' :
exp[i++] = get;
break;
case '8' :
exp[i++] = get;
break;
case '9' :
exp[i++] = get;
break;
case '(' :
open++;
stk.push(get);
break;
case ')' :
close++;
temp = stk.pop();
while(temp != '(')
{
exp[i++] = temp;
temp = stk.pop();
}

case '^' :
stk.push(get);
high++;
break;
case '/' :
stk.push(get);
high++;
break;
case '*' :
stk.push(get);
high++;
break;
case '%' :
stk.push(get);
high++;
break;

case '+' :
if(high == 0)
stk.push(get);
else
while(!stk.isempty())
{
temp = stk.pop();
if(temp != ')' || temp != '(')
exp[i++] = temp;
}
high = 0;
break;
case '-' :
if(high == 0)
stk.push(get);
else
while(!stk.isempty())
{
temp = stk.pop();
if(temp != ')' || temp != '(')
exp[i++] = temp;
}
high = 0;
break;
default:
cout<<"\n\nUnknow Operand/Operator\nOnly 0-9 & *,/,%,+,- Keys
Allowed\n\nContinue To input : ";
}
cout<<"\n\n"<<exp[i-1]<<"\t"<<i<<endl;
}
clrscr();
cout<<"Your Given Equaation in POST-FIX Representation.\n\n";
for(int loop = 0;loop<i; loop++)
{
cout<<exp[loop];
}
getch();
}

/*=======================*/
 
L

LaxTransplant

I assume we can just guess as to what your question is... Have you
even tried to compile this code yet?
 
A

AshifToday

its a functional code, i've posted the code jsut to share it, and any
enhancement i could get from the viewrs comments!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top