0% found this document useful (0 votes)
41 views3 pages

Code For Evaluate Postfix Expression

This C++ program uses a stack data structure to evaluate postfix expressions. It takes a postfix expression as a string, iterates through each character, pushes operands onto the stack and pops operands to apply operators. When it reaches the end, the final result is returned by popping the remaining element from the stack. It implements functions to push, pop, check if empty and access the top of the stack. The evaluatePostfix function scans the expression, pushes operands and pops to apply operators based on the character, with a switch statement to handle the different operations.

Uploaded by

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

Code For Evaluate Postfix Expression

This C++ program uses a stack data structure to evaluate postfix expressions. It takes a postfix expression as a string, iterates through each character, pushes operands onto the stack and pops operands to apply operators. When it reaches the end, the final result is returned by popping the remaining element from the stack. It implements functions to push, pop, check if empty and access the top of the stack. The evaluatePostfix function scans the expression, pushes operands and pops to apply operators based on the character, with a switch statement to handle the different operations.

Uploaded by

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

Code for evaluate postfix expression

// C++ program to evaluate value of a postfix expression


#include <iostream>
#include <math.h>
using namespace std;

template <class type>


class stack
{
private:
struct nodetype
{
type info;
nodetype *link;
};
int size;
nodetype *top;

public:
stack()
{
top = 0;
size = 0;
}

void push(type val)


{
nodetype *newptr = new nodetype;
newptr->info = val;
newptr->link = top;
top = newptr;
size++;
}

type pop()
{
if(empty())
{
cout<<"stack is empty. "<<endl;
return ' ';
}

else
{
nodetype *temp;
temp = top;
top = top->link;
type val = temp -> info;
delete temp;
return val;
}
size--;
}
int sizee()
{
return size;
}
~stack()
{
nodetype *curr;
curr = top;
while(curr != NULL)
{
top = top->link;
delete curr;
curr=curr->link;
}
}

type topof()
{
return top->info;
}

bool empty()
{
if(top == NULL)
return true;
else
return false;
}
int evaluatePostfix(string exp)
{
int i =0;
// Scan all characters one by one
while (exp[i])
{
//expresion = 623+-382/+*2^3+
// If the scanned character is an operand (number
here),
// push it to the stack.
if (exp[i] >= 48 && exp[i] <= 57) //if
(isdigit(exp[i]))
push(exp[i] - '0');
// If the scanned character is an operator, pop two
// elements from stack apply the operator
else
{
int val1 = pop();
int val2 = pop();
switch (exp[i])
{
case '+': {
char valp = (val2 +
val1);
push(valp);
break;
}
case '-': {
char vals = (val2 -
val1);
push(vals);
break;
}
case '*': {
char valm = (val2 *
val1);
push(valm);
break;
}
case '/': {
char vald = (val2 /
val1);
push(vald);
break;
}
case '^': {
char vale = (pow(val2,
val1));
push(vale);
break;
}
}
}
i++;
}
return pop();
}

};

int main()
{
string exp = "623+-382/+*2^3+";
stack <int>s;

cout<<"postfix evaluation: "<< s.evaluatePostfix(exp);


return 0;
}

You might also like