You are on page 1of 23

Sankhasubhra Ghosal

18BCE1064

Day 3
#include<iostream>

#include<conio.h>

using namespace std;

#define MAX_SIZE 20

int stack[MAX_SIZE];

int top=-1;

int isFull()

if(top==MAX_SIZE)

return 1;

else

return 0;

int isEmpty()

if(top==-1)

return 1;

else

return 0;

void push(int val)


{

if(isFull())

cout<<"Stack Overflow!!!";

exit(0);

else

top++;

stack[top]=val;

void pop()

if(isEmpty())

cout<<"Stack Underflow";

else

stack[top]=NULL;

top--;

void peek()
{

cout<<stack[top];

void disp()

if(isFull())

cout<<"Stack overflow!!!";

exit(0);

else

while(top>-1)

cout<<"<-"<<stack[top];

top--;

int main()

int ch;

do

{
cout<<"1.Push\n2.Pop\n3.Peek\n4.Display\n5.Exit\n\n";

cin>>ch;

switch(ch)

case 1:

int data;

cout<<"Enter the data to be inserted into the stack : ";

cin>>data;

push(data);

break;

case 2:

pop();

break;

case 3:

peek();

break;

case 4:

disp();

break;

case 5:

break;

default:

cout<<"Please enter a suitable number as a choice!!!";

}
cin>>ch;

}while(ch!=5);

return 0;

}
#include<iostream>

using namespace std;

#define MAX_SIZE 20

int s1[]={2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};

int s2[MAX_SIZE];

int s3[MAX_SIZE];

int top1=18,top2=-1,top3=-1;

void transfer2()

top2++;

s2[top2]=s1[top1];

top1--;

void transfer3()

top3++;

s3[top3]=s2[top2];

top2--;

int main()

{
while(top2<MAX_SIZE-2)

transfer2();

while(top3<MAX_SIZE-2)

transfer3();

cout<<"\nStack 1 : ";

for(int i=top3;i>-1;i--)

cout<<s1[i]<<"->";

cout<<"\nStack 2 : ";

for(int i=top3;i>-1;i--)

cout<<s2[i]<<"->";

cout<<"\nStack 3 : ";

for(int i=top3;i>-1;i--)

cout<<s3[i]<<"->";

return 0;

}
#include<iostream>

#include<string.h>

using namespace std;

#define MAX_SIZE 20

char stack[MAX_SIZE];

string exp;

int top=-1;

void push(char c)

//if(top>=MAX_SIZE)

//{

// cout<<"Overflow!!!\nExiting the program!";

// exit(0);

//}

//else

//{

top++;

stack[top]=c;

//}

void pop()

stack[top]=NULL;
top--;

int comparison(char c,int n)

if(stack[n]=='{'&&c=='}')

return 1;

else if(stack[n]=='('&&c==')')

return 1;

else if(stack[n]=='['&&c==']')

return 1;

else

return 0;

int main()

cout<<"Enter the algebric expression, complete with brackets : \n";

cin>>exp;

for(int i=0;i<exp.length();i++)

if(exp[i]=='{'||exp[i]=='('||exp[i]=='[')

push(exp[i]);

else

if(comparison(exp[i],top)==1)

pop();
}

if(top==-1)

cout<<"Balanced";

else

cout<<"Not balanced";

return 0;

}
#include <bits/stdc++.h>

using namespace std;

bool isOperator(char c)

return (!isalpha(c) && !isdigit(c));

int getPriority(char C)

if (C == '-' || C == '+')

return 1;

else if (C == '*' || C == '/')

return 2;

else if (C == '^')

return 3;

return 0;

string infixToPostfix(string infix)

infix = '(' + infix + ')';

int l = infix.size();

stack<char> char_stack;

string output;

for (int i = 0; i < l; i++) {


if (isalpha(infix[i]) || isdigit(infix[i]))

output += infix[i];

else if (infix[i] == '(')

char_stack.push('(');

else if (infix[i] == ')') {

while (char_stack.top() != '(') {

output += char_stack.top();

char_stack.pop();

char_stack.pop();

else {

if (isOperator(char_stack.top())) {

while (getPriority(infix[i])

<= getPriority(char_stack.top())) {

output += char_stack.top();

char_stack.pop();

char_stack.push(infix[i]);
}

return output;

string infixToPrefix(string infix)

int l = infix.size();

reverse(infix.begin(), infix.end());

for (int i = 0; i < l; i++) {

if (infix[i] == '(') {

infix[i] = ')';

i++;

else if (infix[i] == ')') {

infix[i] = '(';

i++;

string prefix = infixToPostfix(infix);

reverse(prefix.begin(), prefix.end());
return prefix;

int main()

string s;

cout<<"Enter the Infix notation : ";

cin>>s;

cout<<"\n1.Infix to Postfix\n2.Infix to Prefix\n\nYour choice : ";

int ch;

cin>>ch;

if(ch==2)

cout << infixToPrefix(s) << std::endl;

if(ch==1)

cout<< infixToPostfix(s)<<std::endl;

return 0;

}
#include <iostream>

using namespace std;

bool isOperand(char c) { return (c >= '0' && c <= '9'); }

int value(char c) { return (c - '0'); }

int evaluate(char *exp)

if (*exp == '\0') return -1;

int res = value(exp[0]);

for (int i = 1; exp[i]; i += 2)

char opr = exp[i], opd = exp[i+1];

if (!isOperand(opd)) return -1;

if (opr == '+') res += value(opd);

else if (opr == '-') res -= value(opd);

else if (opr == '*') res *= value(opd);

else if (opr == '/') res /= value(opd);

else return -1;

return res;
}

int main()

char exp[20];

cout<<"Enter the expression : ";

scanf("%s",&exp);

int res = evaluate(exp);

(res == -1)? cout << exp << " is " << "Invalid\n":

cout << "Value of " << exp << " is " << res << endl;

return 0;

}
#include<stdio.h>

main()

float u=0.3;

float v;

float *pu;

float *pv;

pu=&u;

v=*pu;

pv=&v;

printf("\nu=%f &u=%X pu=%X *pu=%f",u,&u,pu,*pu);

printf("\nv=%f &v=%X pv=%X *pv=%f",v,&v,pv,*pv);

}
#include<stdio.h>

main()

double u=(0.3)*1045;

double v;

double *pu;

double *pv;

pu=&u;

v=*pu;

pv=&v;

printf("\nu=%lf &u=%X pu=%X *pu=%lf",u,&u,pu,*pu);

printf("\nv=%lf &v=%X pv=%X *pv=%lf",v,&v,pv,*pv);

}
#include<stdio.h>

main()

char u='C';

char v;

char *pu;

char *pv;

pu=&u;

v=*pu;

pv=&v;

printf("\nu=%c &u=%X pu=%X *pu=%c",u,&u,pu,*pu);

printf("\nv=%c &v=%X pv=%X *pv=%c",v,&v,pv,*pv);

}
#include<stdio.h>

main()

float v=0.3;

float *pv;

pv=&v;

printf("\n*pv=%f v=%f",*pv,v);

*pv=0;

printf("\n\n*pv=%f v=%f",*pv,v);

}
#include<stdio.h>

main()

double v=0.3*1045;

double *pv;

pv=&v;

printf("\n*pv=%lf v=%lf",*pv,v);

*pv=0;

printf("\n\n*pv=%lf v=%lf",*pv,v);

}
#include<stdio.h>

main()

char v='C';

char *pv;

pv=&v;

printf("\n*pv=%c v=%c",*pv,v);

*pv=0;

printf("\n\n*pv=%c v=%c",*pv,v);

You might also like