The main() function for this project is in Arithmetic.cpp; it queries the end user for a string arithmetic expression. The main() function calls the eval() function to evaluate and return the result of the arithmetic expression passed as a parameter. Lastly, the main() function displays the result to the end user. Any number of expressions may be evaluated. The main() function is completed. You are responsible for the implementation of the eval() function. The eval() function parameter will be a string consisting of a properly formed arithmetic expression using only the values 0 through 9 and + (addition), - (subtraction), * (multiplication), / (division), and/or ^ (exponentiation) operators. The expression will not include decimals, negative values, or values above 9. The expression will not include parenthesis but may include spaces, which should be skipped. The eval() function needs to use two ArrayStacks: one to store the numeric values and the other to store the operators. When a number is encountered in the string, push it on the values stack. When an operator is encountered in the string, push it on the operators stack if it has a higher operator precedence than the operator currently on the top of the stack. Otherwise, pop an operator off the operator stack, pop two numbers off the values stack, and push the result of the computation onto the values stack. This should be repeated until the operator at the top of the operators stack has a lower precedence than the current operator. Once this point is reached, push the current operator on the operators stack. Once the end of the arithmetic expression is reached, clear the stacks by popping off an operator from the operators stack, popping two numbers off the values stack, perform the computation and push the result onto the values stack. Continue until the operators stack is empty. The final result will be the remaining value on the values stack. Pop off this value and return it as the result of the eval() function. ARTHEMTIC.CPP #include #include #include "ArrayStack.h" float eval(std::string); // The following main function should not be modified int main(void) { std::string expression = "Continue"; do { std::cout << "Enter an arithmetic expression on a single line ('q' to end): "; getline(std::cin, expression); if (expression[0] != 'q') { float result = eval(expression); std::cout << expression << " = " << result << std::endl; } } while (expression[0] != 'q'); return 0; } float eval(std::string expr) { // To be implemented // Any number of support functions may also be added std::cout << "eval function not implemented" << std::endl; return 0; }
Please help me complete this project. I am very confused and lost.
The main() function for this project is in Arithmetic.cpp; it queries the end user for a string arithmetic expression. The main() function calls the eval() function to evaluate and return the result of the arithmetic expression passed as a parameter. Lastly, the main() function displays the result to the end user. Any number of expressions may be evaluated. The main() function is completed. You are responsible for the implementation of the eval() function.
The eval() function parameter will be a string consisting of a properly formed arithmetic expression using only the values 0 through 9 and + (addition), - (subtraction), * (multiplication), / (division), and/or ^ (exponentiation) operators. The expression will not include decimals, negative values, or values above 9. The expression will not include parenthesis but may include spaces, which should be skipped.
The eval() function needs to use two ArrayStacks: one to store the numeric values and the other to store the operators. When a number is encountered in the string, push it on the values stack. When an operator is encountered in the string, push it on the operators stack if it has a higher operator precedence than the operator currently on the top of the stack. Otherwise, pop an operator off the operator stack, pop two numbers off the values stack, and push the result of the computation onto the values stack. This should be repeated until the operator at the top of the operators stack has a lower precedence than the current operator. Once this point is reached, push the current operator on the operators stack.
Once the end of the arithmetic expression is reached, clear the stacks by popping off an operator from the operators stack, popping two numbers off the values stack, perform the computation and push the result onto the values stack. Continue until the operators stack is empty. The final result will be the remaining value on the values stack. Pop off this value and return it as the result of the eval() function.
ARTHEMTIC.CPP
#include <cstdlib>
#include <iostream>
#include "ArrayStack.h"
float eval(std::string);
// The following main function should not be modified
int main(void) {
std::string expression = "Continue";
do {
std::cout << "Enter an arithmetic expression on a single line ('q' to end): ";
getline(std::cin, expression);
if (expression[0] != 'q') {
float result = eval(expression);
std::cout << expression << " = " << result << std::endl;
}
} while (expression[0] != 'q');
return 0;
}
float eval(std::string expr)
{ // To be implemented
// Any number of support functions may also be added
std::cout << "eval function not implemented" << std::endl;
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 9 steps with 12 images