HiveBrain v1.2.0
Get Started
← Back to all entries
patterncppMinor

Math expression evaluator

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
expressionevaluatormath

Problem

It is my second attempt to make a math expression evaluator.

To evaluate an expression, I created a class that works like this:

-it removes all the blank spaces

-if brackets are present, it processes them by creating another evaluator with the expression inside those brackets

-it gets all the numbers and does first all the exponentiations, then the multiplications/divisions and finally sums all the numbers

For example, let's say I have the expression $$3(3+4)^2+3(5+6)$$

-replaces (3+4) and (5+6) with 7 and 11 $$37^2+311$$

-does 7^2 $$349+311$$

-does the multiplications $$147+33$$

-sums all the numbers $$180$$

If you know if there's something wrong or to optimize I'd really appreciate it

MathSolver.h

```
#include
#include
#include
#include
#include
#include
#include

void changeCharacters(std::string chars, std::string toChange, std::string &str)
{
int i = -1;
std::for_each(str.begin(), str.end(), chars, toChange, &i, &str
{
i++;
for (auto &j : chars)
{
if (c == j)
{
str.erase(str.begin() + i);
str.insert(i, toChange);
break;
}
}
});
}

class MathSolver
{
private:
std::string exp;
std::vector nums;
double result;
std::function isPlusMinus = [](char c) {return c == '+' || c == '-'; };
std::function isMultDiv = [](char c) {return c == '*' || c == '/'; };

public:
MathSolver(std::string exp)
{
exp.erase(std::remove(exp.begin(), exp.end(), ' '), exp.end());
if (exp.find_first_of("+-") != 0)
exp.insert(0, "+");
for (int i = 0; i exp = exp;
this->processBrackets();
this->parse();
}
void countBracks(std::vector> &bracks)
{
int parOC = 0;
for (int i = 0; i > bracks;
countBracks(bracks);
int count = bracks.size();
for (int i = 0; i j = bracks[0];
MathSolver solve(exp.s

Solution

Here are some thoughts about your already quite nice code:

-
use std::isspace for whitespace detection. In that case you can do

str.erase(std::remove_if(str.begin(), str.end(), std::isspace), str.end());

-
Don't use unnecessary abbreviations: bracks vs brackets is not really a gain at all.

-
You should use a std::stack for bracket checking. So whenever you encounter an opening bracket you put it on the stack and check the next one.

-
You might want to get a separate function for extraction of the inner bracket content such like this:

std:string innerBracketString(const std::string& expr, 
                              size_t& startPos, 
                              size_t& endPos) {
    startPos = expr.find_first_of("([{", endPos)+1;
    endPos   = expr.find_first_of(")]}", startPos);
    return expr.substr(startPos, endPos - startPos);
}

Code Snippets

std:string innerBracketString(const std::string& expr, 
                              size_t& startPos, 
                              size_t& endPos) {
    startPos = expr.find_first_of("([{", endPos)+1;
    endPos   = expr.find_first_of(")]}", startPos);
    return expr.substr(startPos, endPos - startPos);
}

Context

StackExchange Code Review Q#148090, answer score: 3

Revisions (0)

No revisions yet.