0% found this document useful (0 votes)
10 views7 pages

DSAL Stack - 240926 - 230009

DSA assignment 2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

DSAL Stack - 240926 - 230009

DSA assignment 2
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Program :-

stack.h :

#ifndef stack
#define stack
#include<iostream>
using namespace std;

template <class s>


class node
{
. public:
. s data;
. node<s> *next;
. node()
. {
. . data = 0;
. . next = NULL;
. }
};

template <class s>


class Stack
{
. public :
. node<s> *top;
. Stack()
. {
. . top = NULL;
. }
. bool isEmpty();
. void push(s element);
. void pop();
. void display();
. s peek();
};

#endif
stack.cpp :
#include "stack.h"
#include<strings.h>
#include<iostream>
using namespace std;

template <class s>


bool Stack<s> :: isEmpty()
{
. if(top == NULL)
. . return true;
. return false;
}
template <class s>
void Stack<s> :: push(s element)
{
. node<s> *temp = new node<s>;
. temp->data = element;
. if(isEmpty())
. . temp->next = NULL;
. else
. . temp->next = top;
. top = temp;
}
template <class s>
void Stack<s> :: pop()
{
. if(isEmpty())
. {
. . cout<<"\nStack Empty!";
. . return;
. }
. node<s> *temp = new node<s>;
. temp = top;
. top = top->next;
. delete temp;
}
template <class s>
void Stack<s> :: display()
{
. if(isEmpty())
. {
. . cout<<"\nStack Empty!";
. . return;
. }
. node<s> *temp=top;
. while(temp != NULL)
. {
. . cout<<", "<<temp->data;
. . temp = temp->next;
. }
}
template <class s>
s Stack<s> :: peek()
{
. return top->data;
}
implement.cpp :

#include "stack.h"
#include "stack.cpp"
#include<iostream>
#include<strings.h>
#include<cmath>
using namespace std;
struct op_val
{
. string operand;
. float value;
};
class expression
{
. public :
. int precedence(char c);
. string infix_to_postfix(string str);
. string rev_string(string str);
. string infix_to_prefix(string str);
. void postfix_eval(string str);
. void prefix_eval(string str);
};
int expression :: precedence(char c)
{
. int p;
. if(c == '+' || c == '-')
. . p = 1;
. else if(c == '*' || c == '/')
. . p = 2;
. else
. . p = 3;
. return p;
}

string expression :: infix_to_postfix(string str)


{
. string postfix = "";
. Stack<char> s;
. for(char c : str)
. {
. . if(c == '(')
. . . s.push(c);
. . else if(c == ')')
. . {
. . . while(s.peek() != '(')
. . . {
. . . . postfix += s.peek();
. . . . s.pop();
. . . }
. . . s.pop();
. . }
. . else if(c == '+' || c == '-' || c == '*' || c == '/' || c == '$' || c == '~')
. . {
. . . if((!s.isEmpty()) && (s.peek() != '(') && (precedence(c) <= precedence(s.peek())))
. . . {
. . . . postfix += s.peek();
. . . . s.pop();
. . . }
. . . s.push(c);
. . }
. . else
. . {
. . . postfix += c;
. . }
. }
. while(s.top != NULL)
. {
. . postfix += s.peek();
. . s.pop();
. }
. return postfix;
}

string expression :: rev_string(string str)


{
. string rev = "";
. for(char c : str)
. {
. . if(c == '(')
. . . rev += ')';
. . else if(c == ')')
. . . rev += '(';
. . else
. . . rev += c;
. }
. return rev;
}

string expression :: infix_to_prefix(string str)


{
. string rev_input = rev_string(str);
. string prefix = rev_string(infix_to_postfix(rev_input));
. return prefix;
}

void expression :: postfix_eval(string str)


{
. Stack<float> s;
. struct op_val listopval[str.length()];
. float c;
. cout<<endl;
. int i = 0;
. for(char ch : str)
. {
. . listopval[i].operand = ch;
. . if(ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '$' || ch == '~')
. . {
. . . float b = s.peek();
. . . s.pop();
. . . float a = s.peek();
. . . s.pop();
. . . c = 0;
. . . switch(ch)
. . . {
. . . . case '+' : c = a + b;
. . . . break;
. . . . case '-' : c = a - b;
. . . . break;
. . . . case '*' : c = a * b;
. . . . break;
. . . . case '/' : if(b == 0)
. . . . . c = b / a;
. . . . else
. . . . . c = a / b;
. . . . break;
. . . . default : c = pow(a, b);
. . . . break;
. . . }
. . . s.push(c);
. . }
. . else
. . {
. . . cout<<listopval[i].operand<<" Value : ";
. . . cin>>listopval[i].value;
. . . s.push(listopval[i].value);
. . }
. . i++;
. }
. cout<<"\nResult = "<<c<<endl;
}

void expression :: prefix_eval(string str)


{
. string rev = rev_string(str);
. struct op_val listopval[rev.length()];
. Stack<float> s;
. cout<<endl;
. for(int i = 0; i < rev.length(); i++)
. {
. . char ch = reve[i];
. . listopval[i].operand = ch;
. . if(ch == '+' || ch == '-' || ch == '/' || ch == '*')
. . {
. . . float a = s.peek();
. . . s.pop();
. . . float b = s.peek();
. . . s.pop();
. . . float c;
. . . switch(ch)
. . . {
. . . . case '+' : c = b + a;
. . . . break;
. . . . case '-' : c = b - a;
. . . . break;
. . . . case '/' : c = b / a;
. . . . break;
. . . . case '*' : c = b * a;
. . . . break;
. . . }
. . . s.push(c);
. . }
. . else
. . {
. . . cout<<listopval[i].operand<<" Value : ";
. . . cin>>listopval[i].value;
. . . s.push(listopval[i].value);
. . }
. }
. cout<<"Result = "<<s.peek()<<endl;
}

int main()
{
. expression e;
. string s;
. int num;
. char ch;
. string post, pre;
. cout<<"\nEnter string : ";
. cin>>s;
. do
. {
. . cout<<"\n\nEnter '1' for Postfix, '2' for Prefix, '3' for Postfix Eval & '4' for Prefix Eval : ";
. . cin>>num;
. . switch(num)
. . {
. . . case 1 : post = e.infix_to_postfix(s);
. . . cout<<"\nPostfix : "<<post<<endl;
. . . break;
. . . case 2 : pre = e.infix_to_prefix(s);
. . . cout<<"\nPrefix : "<<pre<<endl;
. . . break;
. . . case 3 : post = e.infix_to_postfix(s);
. . . cout<<"\nPostfix : "<<post<<endl;
. . . e.postfix_eval(post);
. . . break;
. . . case 4 : pre = e.infix_to_prefix(s);
. . . cout<<"\nPrefix : "<<pre<<endl;
. . . e.prefix_eval(pre);
. . . break;
. . . default : cout<<"\nERROR! TRY AGAIN!!"<<endl;
. . . break;
. . }
. . cout<<"\nDo you wish to continue? Y/N : ";
. . cin>>ch;
. }while(toupper(ch) == 'Y');
. return 0;
}
Output :-

Enter string : abc+*d-

Enter '1' for Postfix, '2' for Prefix, '3' for Postfix Eval & '4' for Prefix Eval : 1

Postfix : abcd*-+

Do you wish to continue? Y/N : y

Enter '1' for Postfix, '2' for Prefix, '3' for Postfix Eval & '4' for Prefix Eval : 2

Prefix : abcd*-+

Do you wish to continue? Y/N : y

Enter '1' for Postfix, '2' for Prefix, '3' for Postfix Eval & '4' for Prefix Eval : 3

Postfix : abcd*-+

a Value : 10
b Value : 50
c Value : 10
d Value : 5

Result = 10

Do you wish to continue? Y/N : y

Enter '1' for Postfix, '2' for Prefix, '3' for Postfix Eval & '4' for Prefix Eval : 4

Prefix : abcd*-+

a Value : 10
b Value : 5
c Value : 10
d Value : 5
Result = -35

Do you wish to continue? Y/N : y

Enter '1' for Postfix, '2' for Prefix, '3' for Postfix Eval & '4' for Prefix Eval : 5

ERROR! TRY AGAIN!!

Do you wish to continue? Y/N : n

You might also like