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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Write the report of this course that is the explanation of the algorithm in this program

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

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education