0% found this document useful (0 votes)
23 views1 page

Lab 13

Uploaded by

SUJAL GUPTA
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)
23 views1 page

Lab 13

Uploaded by

SUJAL GUPTA
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/ 1

/*LAB 13

Program for prefix evaluation*/


#include <bits/stdc++.h>
using namespace std;

double evaluatePrefix(string prefixExp) {

stack<double> operendStack;
int size = prefixExp.size() - 1;

for (int i = size; i >= 0; i--) {

if (isdigit(prefixExp[i]))
operendStack.push(prefixExp[i] - '0');
else {
double o1 = operendStack.top();
operendStack.pop();
double o2 = operendStack.top();
operendStack.pop();
if( prefixExp[i] == '+')
operendStack.push(o1 + o2);
else if( prefixExp[i] == '-')
operendStack.push(o1 - o2);
else if( prefixExp[i] == '*')
operendStack.push(o1 * o2);
else if( prefixExp[i] == '/')
operendStack.push(o1 / o2);
else{
cout<<"Invalid Expression";
return -1;
}
}
}
return operendStack.top();
}

int main()
{
string prefixExp = "*+69-31";
cout<<"The result of evaluation of expression "<<prefixExp<<" is
"<<evaluatePrefix(prefixExp);
return 0;
}
OUTPUT:
The result of evaluation of expression *+69-31 is 30

You might also like