Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 7

Data Structures and Algorithms

Lab Journal - Lab 3 Stacks II


Name: _________________________________

Enrollment #: _________________________________

Class/Section: _________________________________
QUESTION 01

Header File
#pragma once
#include<iostream>
#include<conio.h>
#include<fstream>

using namespace std;


int const size=100;
class DSA_3s2
{
private:
int op1;
int op2;
int result;
char e[size];
int top;
int stack[size];
public:
DSA_3s2(void);
~DSA_3s2(void);
void Push(char);
int Pop();
bool IsEmpty();
bool IsFull();
void file();
};

Cpp File
#include "DSA_3s2.h"

DSA_3s2::DSA_3s2(void)
{
top=-1;
op1=0;
op2=0;
result=0;
}

DSA_3s2::~DSA_3s2(void)
{}
void DSA_3s2 ::file()
{
int nn;
ifstrea00m exp;
exp.open("evaluate.txt");
exp>>e;
cout<<"EXpression is :"<<e<<endl;
for(int i=0;i<strlen(e);i++)
{
if(isdigit (e[i]))
{ nn=e[i]-'0';
cout<<nn<<endl;
Push(nn);
}
if(e[i]=='*' ||e[i]=='/'||e[i]=='-'||e[i]=='+')
{ op1=Pop();
op2=Pop();
switch(e[i])
{
case '+':
result=op2+op1;
break;
case'*':
result=op2*op1;
break;
case '-':
result=op2-op1;
break;
case'/':
result=op2/op1;
break;
}
Push(result);
}
}
cout<<"After evaluation is:"<<Pop()<<endl;
}
bool DSA_3s2 ::IsEmpty()
{
if(top==-1)

return true;

else
return false;
}
bool DSA_3s2 ::IsFull()
{
if(top==size-1)

return true;

else
return false;
}
void DSA_3s2:: Push(char x)
{
if(!IsFull())
{
top++;
stack[top]=x;
}
}
int DSA_3s2 :: Pop()
{
int r;
if(!IsEmpty())
{
r=stack[top];
top--;
return r;
}
else
cout<<"Stack underflow"<<endl;
return -1;
}

Main File
#include"DSA_3s2.h"
#include<iostream>
#include<conio.h>
#include<fstream>
using namespace std;
void main()
{
DSA_3s2 D;
D.file();
getch();
}

Output:

Name: _________________________________

QUESTION 2
Header file
#pragma once
#include<iostream>
#include<conio.h>
#include<fstream>
#include<string>
using namespace std;
int const size=100;
class DSA_3s
{
private:
int top;
char stack[size];
char exp[size];
char *s;

public:

DSA_3s(void);
~DSA_3s(void);
void Push(char);
char Pop();
int Priority(char);
void Display();
bool IsEmpty();
bool IsFull();
void conv();
};

Cpp File
#include "DSA_3s.h"

DSA_3s::DSA_3s(void)
{
top=-1;
}

DSA_3s::~DSA_3s(void)
{
}
bool DSA_3s:: IsEmpty()
{
if(top==-1)
return true;
else
return false;

}
bool DSA_3s::IsFull()
{
if(top==size-1)
return true;
else
return false;

}
void DSA_3s::Push(char x)
{
top++;
stack[top]=x;
}
char DSA_3s::Pop()
{
char z;
z=stack[top];
top--;
return z;
}
int DSA_3s ::Priority(char x)
{
if(x=='(')
return 0;
else if(x=='+' || x=='-')
return 1;
else if(x=='*' || x=='/')
return 2;
else
return -1;
}
void DSA_3s::conv()
{ char z;
cout<<"enter infix expression"<<endl;
cin>>exp;
s=exp;
while(*s!='\0')
{
if(isalpha(*s))
cout<<*s;
else if(*s=='(')
Push(*s);
else if (*s== ')')
{
while(z=Pop()!='(')
cout<<z;

}
else
{ if(Priority(stack[top])>=Priority(*s))
cout<<Pop();
Push(*s);
}

s++;
}
while(top!=-1)
{
cout<<Pop();

}
}

Main File
#include"DSA_3s.h"
#include<iostream>
#include<conio.h>
#include<fstream>

using namespace std;

void main()
{
DSA_3s D;
D.conv();
//D.Display();
_getch();

Output:

You might also like