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...


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;
}
}


string inToPost(string infix ) {  // defining push function
stack 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="">
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 <=>
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 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="">
getline(cin,infix);
int flag=0;
for(int i=0;i<>
{
if(infix[i]>='a' && infix[i]<='z'|| infix[i]="">='A'&& infix[i]<>
{
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)=""><>
cout < "postfix="" evaluation="" :=""><><>
}
else
{
printf("Invalid String!! Your string should only have '*','/','+','-',','(',')' and integers.\nPlease re-enter the string.");
goto Label;

}
}

Jun 09, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here