0% found this document useful (0 votes)
30 views8 pages

Lab 2 Infix To Postfix, Postfix Expression

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)
30 views8 pages

Lab 2 Infix To Postfix, Postfix Expression

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/ 8

Name :BHUKYA BHANUTEJA

Roll no:12012100

Infix to postfix 
#include<iostream>
#include<stack>
#include<locale> //for function isalnum()
using namespace std;

int preced(char ch) {


if(ch == '+' || ch == '-') {
return 1; //Precedence of + or - is 1
}else if(ch == '*' || ch == '/') {
return 2; //Precedence of * or / is 2
}else if(ch == '^') {
return 3; //Precedence of ^ is 3
}else {
return 0;
}
}

string inToPost(string infix ) {


stack<char> sto;
sto.push('#'); //add some extra character to avoid
underflow
string postfix = ""; //initially the postfix string is empty
string::iterator it;

for(it = infix.begin(); it!=infix.end(); it++) {


if(isalnum(char(*it)))
postfix += *it; //add to postfix when character is letter or
number
else if(*it == '(')
sto.push('(');
else if(*it == '^')
sto.push('^');
else if(*it == ')') {
while(sto.top() != '#' && sto.top() != '(') {
postfix += sto.top(); //store and pop until ( has found
sto.pop();
}
sto.pop(); //remove the '(' from stack
}else {
if(preced(*it) > preced(sto.top()))
sto.push(*it); //push if precedence is high
else {
while(sto.top() != '#' && preced(*it) <= preced(sto.top())) {
postfix += sto.top(); //store and pop until higher
precedence is found
sto.pop();
}
sto.push(*it);
}
}
}

while(sto.top() != '#') {
postfix += sto.top(); //store and pop until stack is not empty.
sto.pop();
}

return postfix;
}

int main() {
string put;
cout<<"enter the string"<<endl;
cin>>put;
string infix (put);
inToPost(infix);
cout << "Postfix Form Is: " << inToPost(infix) << endl;
}

TIME COMPLEXITY :O(N)

Postfix expression evaluation using stack


#include<iostream>
#include<cmath>
#include<stack>
using namespace std;

float scanNum(char ch) {


int value;
value = ch;
return float(value-'0'); //return float from character
}

int isOperator(char ch) {


if(ch == '+'|| ch == '-'|| ch == '*'|| ch == '/' || ch == '^')
return 1; //character is an operator
return -1; //not an operator
}

int isOperand(char ch) {


if(ch >= '0' && ch <= '9')
return 1; //character is an operand
return -1; //not an operand
}

float operation(int a, int b, char op) {


//operations to be performed
if(op == '+')
return b+a;
else if(op == '-')
return b-a;
else if(op == '*')
return b*a;
else if(op == '/')
return b/a;
else if(op == '^')
return pow(b,a);
else
return INT_MIN; //return negative infinity
}

float postfixEval(string postfix) {


int a, b;
stack<float> stk;
string::iterator it;

for(it=postfix.begin(); it!=postfix.end(); it++) {


// it reads elements and perform postfix evaluation
if(isOperator(*it) != -1) {
a = stk.top();
stk.pop();
b = stk.top();
stk.pop();
stk.push(operation(a, b, *it));
}else if(isOperand(*it) > 0) {
stk.push(scanNum(*it));
}
}
return stk.top();
}

main() {
string put;
cout<<"enter the string"<<endl;
cin>>put;
string post(put);
postfixEval(post);
cout << "the evaluated postfix is : "<<postfixEval(post);
}
TIME COMPLEXITY :O(N)

You might also like