I Need A Code In C++ Language Programming Like This To Evaluate The postfix expression if all bracket not missing do the evaluate if any bracket missing will program give me mising open bracket or close bracket please\ Data Structure using c++ (stack) like this function but i need a simple one please  int evaluator(string tokens){ int i; int op_brace =0; int closing_brace = 0; stack values; stack ops; for(i = 0; i < tokens.length(); i++) { if(tokens[i] == ' ') continue; else if(tokens[i] == '('){ ops.push(tokens[i]); op_brace ++; } 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] == ')'){ closing_brace++; if(closing_brace > op_brace){ cout<<"\n missing open bracket"<= precedence(tokens[i])){ int val2 = values.top(); values.pop(); int val1 = values.top(); values.pop(); char op = ops.top(); ops.pop(); values.push(operation(val1, val2, op)); } ops.push(tokens[i]); } } if(op_brace>closing_brace) { cout<<"\n missing close bracket"<op_brace) { cout<<"\n missing open bracket"<

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 6PE
icon
Related questions
Question

I Need A Code In C++ Language Programming Like This To Evaluate The postfix expression if all bracket not missing do the evaluate if any bracket missing will program give me mising open bracket or close bracket please\

Data Structure using c++ (stack)

like this function but i need a simple one please 

int evaluator(string tokens){
int i;
int op_brace =0;
int closing_brace = 0;
stack <int> values;
stack <char> ops;

for(i = 0; i < tokens.length(); i++)
{
if(tokens[i] == ' ')
continue;
else if(tokens[i] == '('){
ops.push(tokens[i]);
op_brace ++;
}
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] == ')'){
closing_brace++;
if(closing_brace > op_brace){
cout<<"\n missing open bracket"<<endl;

exit(0);

}
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(operation(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(operation(val1, val2, op));
}
ops.push(tokens[i]);
}
}
if(op_brace>closing_brace)
{
cout<<"\n missing close bracket"<<endl;

exit(0);
}
else if(closing_brace>op_brace)
{
cout<<"\n missing open bracket"<<endl;

exit(0);
}
while(!ops.empty()){
int val2 = values.top();
values.pop();

int val1 = values.top();
values.pop();

char op = ops.top();
ops.pop();

values.push(operation(val1, val2, op));
}

return values.top();
}

 

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 2 images

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
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Programming Logic & Design Comprehensive
Programming Logic & Design Comprehensive
Computer Science
ISBN:
9781337669405
Author:
FARRELL
Publisher:
Cengage