CS 311 Data Structures and Algorithms Assignment 2 Due on Wednesday, September 30, 11:59PM. Write a C++ program to implement the function (i.e., evalExpression) below. Your program must be able to...

1 answer below »
Needs to be in C++ language it's for CS311 or third semester computer science


CS 311 Data Structures and Algorithms Assignment 2 Due on Wednesday, September 30, 11:59PM. Write a C++ program to implement the function (i.e., evalExpression) below. Your program must be able to correctly evaluate a mathematical expression (i.e., the exp parameter) containing non-negative numbers (e.g., 16, 33.9, 0.6) and six basic types of operators (i.e., +, -, *, /, (, )). For example, 3.5+20/4, 9.2*(6-31/(2+8)). In addition, your program must be able to detect and report the expression errors (e.g., parenthesis mismatch, unrecognized operators) and the division by zero error. double evalExpression(const char * exp); If you think it is necessary, your program can require that the input expression be embraced by the ‘#’ signs. E.g., #3.5+20/4#. However, this is optional for this assignment. Note: 1. Space should be allowed between numbers and operators in an expression. 2. Your program MUST implement and use your own Stack class. You cannot use the Stack class provided by the C++ library. 3. Put all the code (including the main function) in a .cpp file. 4. Please include your name, student id, and email address as comments in the code that you submit.
Answered Same DaySep 27, 2021CS311

Answer To: CS 311 Data Structures and Algorithms Assignment 2 Due on Wednesday, September 30, 11:59PM. Write a...

Parth answered on Sep 29 2021
148 Votes
#include
using namespace std;
int prec_dmas(char op){
if(op == '+'||op == '-')
retu
rn 1;
if(op == '*'||op == '/')
return 2;
return 0;
}
double final_out(double a, double b, char op){
switch(op){
case '+': return a + b;
case '-': return a - b;
case '*': return a * b;
case '/': if(b == 0)
{
cout<<"Error:Divide by Zero"<<'\n';
return 0;
}
return a / b;
}
}
double evalExpression(const char* tokens){
int i;
stack vals;
stack ops;
for(i = 0; i < strlen(tokens); i++){
if(tokens[i] == ' ')
continue;
else if(tokens[i] == '('){
ops.push(tokens[i]);
}
else if(isdigit(tokens[i])){
double value = 0.0;
int flag = 0;
int j = i;

while(j < strlen(tokens) &&
isdigit(tokens[j]) || tokens[j] == '.')
{

...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here