You are on page 1of 8

// Implementation of stack

#include<iostream>

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

#define stack_size 5
using namespace std;
class Stack {
private:
int item, i;
int s[10];
int top;
public:
Stack()
{
top = -1;

void push(){
if (top == stack_size-1)
cout<<"\n## Stack is Full!";
else {
cout<<"\nEnter The Value to be pushed : ";
cin>>item;
s[++top]=item;
}
}

void pop(){
if (top == -1)
cout<<"\n## Stack is Empty!";
else
top--;
}

void display()
{
if (top == -1)
cout<<"\n## Stack is Empty!";
else
for (i = 0; i <= top; i++)
{
cout<<"\n## Position : "<< i <<", Value :"<< s[i];
}
}
};

int main(){
int choice;
Stack obj;
//cout<<"\nSimple Stack Example - Class and Member Functions - C+
+";
for(;;)
{
cout<<"\n\nStack Main Menu";

cout<<"\n1.Push \n2.Pop \n3.Display \nOthers to exit";


cout<<"\nEnter Your Choice : ";
cin>>choice;
switch (choice)
{
case 1:
obj.push();
break;
case 2:
obj.pop();
break;
case 3:
obj.display();
break;
default: exit(0);
}

}
getch();
return 0;

// infix to postfix program

#include <iostream>
#include <conio.h>
#include <string.h>
#include<stdlib.h>
using namespace std;
#define MAX_SIZE 5

class stack
{
public:
char symbol, s[30];
char infix[30];
char postfix[30];

int top,i,j;
stack()
{
top=-1;
}

int G(char symbol)


{
switch(symbol)
{
case'+':
case '-': return 1;
case'*':
case '/': return 3;
case'^':
case'$': return 6;
case'(': return 9;
case')': return 0;
default:return 7;
}
}

int F (char symbol)

{
switch(symbol)
{
case'+':
case '-': return 2;
case'*':
case '/': return 4;
case'^':
case'$': return 5;
case'(': return 0;
case'#': return -1;
default:return 8;
}
}
void infix_postfix ()
{
s[++top]='#';
j=0;
for(i=0;i<strlen(infix);i++)
{
symbol=infix[i];
while(F(s[top])>G(symbol))
{
postfix[j]=s[top--];
j++;
}
if(F(s[top] )!=G(symbol))
s[++top]=symbol;
else
top--;
}
while(s[top]!='#')
{
postfix[j++]=s[top--];
}
postfix[j]='\0';
//cout<<postfix;
}

void display()
{
cout<< " enter infix expression\n";
cin>>infix;

infix_postfix();
cout<<postfix<<endl;
}
};

int main()
{
stack obj;
obj.display();
getch();
return 0;
}
//Evaluation of postfix expression
#include<iostream>
#include<conio.h>
#include<math.h>
#include<stdio.h>
#include<string.h>
using namespace std;
class eval
{
public:
int s[20],res;
double op1,op2;
int top;
char postfix[20],symbol;
double op(char symbol,double op1,double op2)
{
switch(symbol)
{
case'+': return op1+op2;
case'-': return op1-op2;
case'*': return op1*op2;
case'/': return op1/op2;
case'$':
case'^': return pow(op1,op2);
}
}
eval()
{
top=-1;
}

void evaluation()
{
cout<<"enter postfix expression\n";
cin>>postfix;
for(int i=0;i<strlen(postfix);i++)
{
symbol=postfix[i];
if (isdigit(symbol))
s[++top]=symbol-'0';
else
{
op2=s[top--];
op1=s[top--];
res=op(symbol,op1,op2);
s[++top]=res;
}
}
res=s[top--];
cout<<res<<endl;
}
};
int main()
{
eval obj;
obj.evaluation();
getch();
return 0;
}

You might also like