Calculate the value of a mathematical expression (Binary Tree) Problem description] iven a mathematical expression, calculate its value by constructing binary tre lathematical operations in the expression indude +, -, * 1, 0. Th athematical expression begins and ends with #. Output the postorde aversal sequence of the constructed binary tree at the same time. Because th inary tree corresponding to a mathematical expression is not unique, th reorder traversal sequence is also not unique. Any feasible preorder traversa equence is ok. Basic requirements and Example] /hat you need to show in the terminal(the back part is outputted by yo nd the blue part is inputted by the user, i.e, teacher): ease input the mathematical expression: 1+2*(4-3)-8/2#

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
100%

Please write the algorithm how the code is working (DataStructure)

#include<iostream>

#include<string>

#include<stack>

#include <algorithm>

using namespace std;

//checking for precedence of the operators in the stack:

// As per priority basis multiplication and division has the highest priority,than addition and subtraction

int precedence(char t)

{

if (t == '*' || t == '/')//priorities check

return 2;

else if (t == '+' || t == '-')

return 1;

else

return -1;

}

//while calculating the value of given expression, through the below function we will apply the operation and return the integer value

int applyOp(int a, int b, char op){

switch(op){

case '+': return a + b;

case '-': return a - b;

case '*': return a * b;

case '/': return a / b;

}

}

//this function calculates the expression value and return it.

int calculate(string tokens){

int i;

stack <int> values;

stack <char> ops;

for(i = 0; i < tokens.length(); i++){

if(tokens[i] == '('){

ops.push(tokens[i]);

}

else if(isdigit(tokens[i])){

int val = 0;

while(i < tokens.length() &&

isdigit(tokens[i]))

{

val = (val*10) + (tokens[i]-'0');

i++;

}

values.push(val);

i--;

}

else if(tokens[i] == ')')

{

while(!ops.empty() && ops.top() != '(')

{

int val2 = values.top();

values.pop();

int val1 = values.top();

values.pop();

char op = ops.top();

ops.pop();

values.push(applyOp(val1, val2, op));

}

if(!ops.empty())

ops.pop();

}

else

{

while(!ops.empty() && precedence(ops.top())

>= precedence(tokens[i])){

int val2 = values.top();

values.pop();

int val1 = values.top();

values.pop();

char op = ops.top();

ops.pop();

values.push(applyOp(val1, val2, op));

}

ops.push(tokens[i]);

}

}

while(!ops.empty()){

int val2 = values.top();

values.pop();

int val1 = values.top();

values.pop();

char op = ops.top();

ops.pop();

values.push(applyOp(val1, val2, op));

}

return values.top();

}

//this function converts the infix expression to postfix expression and this postfix expression is again used to find the prefix expression

string inpost(string s)

{

string result = "\0";

stack<char> st;

char c;

for (int i = 0; i < s.size(); i++)

{

c = s[i];

if (c >= '0' && c <= '9')

result += c;

else if (c == '(')

st.push(c);

else if (c == ')')

{

while (st.top() != '(')

{

result += st.top();

st.pop();

}

st.pop();

}

else

{

while (!st.empty() && precedence(s[i]) < precedence(st.top()))

{

result += st.top();

st.pop();

}

st.push(c);

}

}

while (!st.empty())

{

result += st.top();

st.pop();

}

return result;

}

//convert(string) function prints the prefix expression on the terminal

void convert(string infix)

{

int l = infix.size();

reverse(infix.begin(), infix.end());

for (int i = 0; i < l; i++) {

if (infix[i] == '(') {

infix[i] = ')';

i++;

}

else if (infix[i] == ')') {

infix[i] = '(';

i++;

}

}

string prefix = inpost(infix);

reverse(prefix.begin(), prefix.end());

cout<<"The postorder traversal sequence is:"<<endl;

cout<<prefix;

}

int main(){

string math_exp;

cout<<"Please input the mathematical expression:"<<endl;

cin>>math_exp;

math_exp = math_exp.substr(1 , math_exp.size()-2);

cout<<"The value is:"<<endl;

cout<<calculate(math_exp)<<endl;

convert(math_exp);

return 0;

}

O "C:\Users\Nel\Desktop\2021data structure\calBinary\bin\Debug\calBinary.exe"
Please input the mathematical expression:
#1+2* (4-3)-8/2#
The value is:
-1
The postorder traversal sequence is:
+1*2-43/82
Process returned 0 (0x0)
Press any key to continue.
execution time : 32.573 s
Transcribed Image Text:O "C:\Users\Nel\Desktop\2021data structure\calBinary\bin\Debug\calBinary.exe" Please input the mathematical expression: #1+2* (4-3)-8/2# The value is: -1 The postorder traversal sequence is: +1*2-43/82 Process returned 0 (0x0) Press any key to continue. execution time : 32.573 s
8. Calculate the value of a mathematical expression (Binary Tree)
[Problem description]
Given a mathematical expression, calculate its value by constructing binary tree.
Mathematical operations in the expression indude +, , * 1, 0 The
mathematical expression begins and ends with #. Output the postorder
traversal sequence of the constructed binary tree at the same time. Because the
binary tree corresponding to a mathematical expression is not unique, the
preorder traversal sequence is also not unique. Any feasible preorder traversal
sequence is ok.
[Basic requirements and Example]
What you need to show in the terminal(the back part is outputted by you
and the blue part is inputted by the user, i.e., teacher):
Please input the mathematical expression:
#1+2*(4-3)-8/2#
The value is:
-1
The postorder traversal sequence is:
-+1*2-43/82
Transcribed Image Text:8. Calculate the value of a mathematical expression (Binary Tree) [Problem description] Given a mathematical expression, calculate its value by constructing binary tree. Mathematical operations in the expression indude +, , * 1, 0 The mathematical expression begins and ends with #. Output the postorder traversal sequence of the constructed binary tree at the same time. Because the binary tree corresponding to a mathematical expression is not unique, the preorder traversal sequence is also not unique. Any feasible preorder traversal sequence is ok. [Basic requirements and Example] What you need to show in the terminal(the back part is outputted by you and the blue part is inputted by the user, i.e., teacher): Please input the mathematical expression: #1+2*(4-3)-8/2# The value is: -1 The postorder traversal sequence is: -+1*2-43/82
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY