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"<
I Need A Code In C++ Language
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();
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images