Write the report of this course that is the explanation of the algorithm in this program #include // include necessary libraries #include // include necessary libraries #include // include necessary libraries #include // include necessary libraries using namespace std; int preced(char ch) { // defining precedence function if(ch == '+' || ch == '-') { // if condition return 1; }else if(ch == '*' || ch == '/') { return 2; }else { return 0; }
Write the report of this course that is the explanation of the
#include<iostream> // include necessary libraries
#include<stack> // include necessary libraries
#include<locale> // include necessary libraries
#include <climits> // include necessary libraries
using namespace std;
int preced(char ch) { // defining precedence function
if(ch == '+' || ch == '-') { // if condition
return 1;
}else if(ch == '*' || ch == '/') {
return 2;
}else {
return 0;
}
}
string inToPost(string infix ) { // defining push function
stack<char> stk;
stk.push('#');
string postfix = "";
string::iterator it;
for(it = infix.begin(); it!=infix.end(); it++) { // for loop
if(isalnum(char(*it))) // if condition
postfix += *it;
else if(*it == '(') // if condition
stk.push('(');
else if(*it == ')') { // if condition
while(stk.top() != '#' && stk.top() != '(') {
postfix += stk.top();
stk.pop(); // pop element
}
stk.pop();
}else {
if(preced(*it) > preced(stk.top())) // if condition
stk.push(*it);
else {
while(stk.top() != '#' && preced(*it) <= preced(stk.top())) { // while loop
postfix += stk.top();
stk.pop();
}
stk.push(*it);
}
}
}
while(stk.top() != '#') { // while loop
postfix += stk.top();
stk.pop();
}
return postfix;
}
float scanNum(char ch){
int value;
value = ch;
return float(value-'0');
}
int isOperator(char ch){ // defining function to check operator or not
if(ch == '+'|| ch == '-'|| ch == '*'|| ch == '/')
return 1;
return -1;
}
int isOperand(char ch){
if(ch >= '0' && ch <= '9')
return 1;
return -1;
}
float operation(int a, int b, char op){
if(op == '+')
return b+a;
else if(op == '-')
return b-a;
else if(op == '*')
return b*a;
else if(op == '/')
return b/a;
else
return INT_MIN;
}
float postfixEval(string postfix){
int a, b;
stack<float> stk;
string::iterator it;
for(it=postfix.begin(); it!=postfix.end(); it++){
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();
}
int main() { // main method
string infix;
Label:
cout<<"\n\nEnter the infix expression : "; // print message
getline(cin,infix);
int flag=0;
for(int i=0;i<infix.size();i++)
{
if(infix[i]>='a' && infix[i]<='z'|| infix[i]>='A'&& infix[i]<='Z')
{
printf("Invalid String!! Your string should have '*','/','+','-',','(',')' and integers.\nPlease re-enter the string..");
goto Label;
}
if(infix[i]>=32 && infix[i]<=39 || infix[i]==44 || infix[i]==46 || infix[i]>=58 && infix[i]<=64 || infix[i]>=91 && infix[i]<=96 )
{
printf("Invalid String!! Your string should have '*','/','+','-',','(',')' and integers.\nPlease re-enter the expression..");
goto Label;
}
if(infix[i]=='*' || infix[i]=='/' || infix[i]=='+' || infix[i]=='-' || infix[i]=='(' || infix[i]==')')
{
flag++;
}
}
if(flag!=0)
{
string postfix;
postfix=inToPost(infix);
cout << "Postfix Form Is : " << inToPost(infix) << endl;
cout << "Postfix evaluation : "<<postfixEval(postfix)<<endl;
}
else
{
printf("Invalid String!! Your string should only have '*','/','+','-',','(',')' and integers.\nPlease re-enter the string.");
goto Label;
}
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps