Skip to content

Commit 26e6a8e

Browse files
committed
Added MathParser lib for the project.
1 parent 003667a commit 26e6a8e

File tree

6 files changed

+281
-0
lines changed

6 files changed

+281
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
#include "MathParser.h"
2+
3+
bool MATHPARSER_hasElement(std::vector<MathParser::Token>& array, std::string const& element) {
4+
for (auto const& e : array) {
5+
if (e.TokenText == element) {
6+
return true;
7+
}
8+
}
9+
return false;
10+
}
11+
12+
int MATHPARSER_expectElement(std::vector<MathParser::Token>& array, std::string const& element) {
13+
for (MathParser::Token const& e : array) {
14+
if (e.TokenText == element) {
15+
auto it = std::find(begin(array), end(array), e);
16+
return (int) std::distance(array.begin(), it);
17+
}
18+
}
19+
return 0;
20+
}
21+
22+
void MATHPARSER_operations(std::vector<MathParser::Token>& array, int& caseVector, std::string const& op) {
23+
MathParser::Token n1 = array[caseVector - 1];
24+
MathParser::Token n2 = array[caseVector + 1];
25+
26+
double result = 0;
27+
28+
if (op == "-") {
29+
result = n1 - n2;
30+
} else if (op == "+") {
31+
result = n1 + n2;
32+
} else if (op == "*") {
33+
result = n1 * n2;
34+
} else if (op == "/") {
35+
result = n1 / n2;
36+
}
37+
38+
array[caseVector - 1].TokenText = std::to_string(result);
39+
array[caseVector].TokenText = "N/A";
40+
array[caseVector + 1].TokenText = "N/A";
41+
42+
while (MATHPARSER_hasElement(array, "N/A")) {
43+
for (MathParser::Token const& e : array) {
44+
if (e.TokenText == "N/A") {
45+
auto it = std::find(begin(array), end(array), e);
46+
array.erase(it);
47+
}
48+
}
49+
}
50+
}
51+
52+
double MATHPARSER_Parser(std::vector<MathParser::Token> contentSplitted) {
53+
int totalSizeVector = contentSplitted.size();
54+
int i = 0;
55+
56+
while (i < totalSizeVector) {
57+
58+
while (MATHPARSER_hasElement(contentSplitted, "*")) {
59+
auto caseVectorMulti = MATHPARSER_expectElement(contentSplitted, "*");
60+
MATHPARSER_operations(contentSplitted, caseVectorMulti, "*");
61+
}
62+
63+
64+
while (MATHPARSER_hasElement(contentSplitted, "/")) {
65+
auto caseVectorDiv = MATHPARSER_expectElement(contentSplitted, "/");
66+
MATHPARSER_operations(contentSplitted, caseVectorDiv, "/");
67+
}
68+
69+
while (MATHPARSER_hasElement(contentSplitted, "+")) {
70+
auto caseVectorAdd = MATHPARSER_expectElement(contentSplitted, "+");
71+
MATHPARSER_operations(contentSplitted, caseVectorAdd, "+");
72+
}
73+
74+
while (MATHPARSER_hasElement(contentSplitted, "-")) {
75+
auto caseVectorRemove = MATHPARSER_expectElement(contentSplitted, "-");
76+
MATHPARSER_operations(contentSplitted, caseVectorRemove, "-");
77+
}
78+
i++;
79+
}
80+
81+
return convertDouble(contentSplitted[0].TokenText);
82+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <string>
2+
#include <vector>
3+
#include <sstream>
4+
#include <iostream>
5+
#include <algorithm>
6+
7+
#include "Utils/Converts.h"
8+
#include "MathTokenizer.h"
9+
10+
bool MATHPARSER_hasElement(std::vector<MathParser::Token>& array, std::string const& element);
11+
int MATHPARSER_expectElement(std::vector<MathParser::Token>& array, std::string const& element);
12+
void MATHPARSER_operations(std::vector<MathParser::Token>& array, int& caseVector, std::string const& op);
13+
double MATHPARSER_Parser(std::vector<MathParser::Token> contentSplitted);
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
#include "MathTokenizer.h"
2+
3+
namespace MathParser {
4+
std::vector<Token> TokenBuilding::ParserTokens(const std::string &content) {
5+
std::vector<Token> tokens;
6+
Token currentToken;
7+
8+
for (auto const& element : content) {
9+
switch (element) {
10+
case '0':
11+
case '1':
12+
case '2':
13+
case '3':
14+
case '4':
15+
case '5':
16+
case '6':
17+
case '7':
18+
case '8':
19+
case '9':
20+
if (currentToken.TokenType == WHITESPACE) {
21+
currentToken.TokenType = INT;
22+
currentToken.TokenText.append(1, element);
23+
} else if (currentToken.TokenType == MAYBE_DOUBLE) {
24+
currentToken.TokenType = DOUBLE;
25+
currentToken.TokenText.append(1, element);
26+
} else {
27+
currentToken.TokenText.append(1, element);
28+
}
29+
break;
30+
31+
case '.':
32+
if (currentToken.TokenType == WHITESPACE) {
33+
currentToken.TokenType = MAYBE_DOUBLE;
34+
currentToken.TokenText.append(1, element);
35+
} else if (currentToken.TokenType == INT) {
36+
currentToken.TokenType = DOUBLE;
37+
currentToken.TokenText.append(1, element);
38+
} else {
39+
TokenBuilding::endToken( currentToken, tokens);
40+
currentToken.TokenType = OPERATOR;
41+
currentToken.TokenText.append(1, element);
42+
TokenBuilding::endToken(currentToken, tokens);
43+
}
44+
break;
45+
46+
case '+':
47+
case '-':
48+
case '/':
49+
case '*':
50+
TokenBuilding::endToken(currentToken, tokens);
51+
currentToken.TokenType = OPERATOR;
52+
currentToken.TokenText.append(1, element);
53+
TokenBuilding::endToken(currentToken, tokens);
54+
break;
55+
56+
default:
57+
break;
58+
}
59+
}
60+
TokenBuilding::endToken(currentToken, tokens);
61+
return tokens;
62+
}
63+
64+
void TokenBuilding::endToken(Token &token, std::vector<Token> &tokens) {
65+
if (token.TokenType != WHITESPACE) {
66+
tokens.push_back(token);
67+
}
68+
69+
if (token.TokenType == MAYBE_DOUBLE)
70+
{
71+
if (token.TokenText == ".") {
72+
token.TokenType = OPERATOR;
73+
} else {
74+
token.TokenType = MAYBE_DOUBLE;
75+
}
76+
}
77+
78+
token.TokenType = WHITESPACE;
79+
token.TokenText.erase();
80+
}
81+
82+
bool operator==(Token const& a, Token const& b) {
83+
return a.TokenText == b.TokenText;
84+
}
85+
86+
double operator+(Token const& a, Token const& b) {
87+
try {
88+
double v = std::stod(a.TokenText);
89+
double v2 = std::stod(b.TokenText);
90+
return v + v2;
91+
}
92+
catch (std::invalid_argument const& ex) {
93+
std::cout << "It can not be a double/int value !" << " Final error: " << ex.what() << std::endl;
94+
}
95+
return 0;
96+
}
97+
98+
double operator-(Token const& a, Token const& b) {
99+
try {
100+
double v = std::stod(a.TokenText);
101+
double v2 = std::stod(b.TokenText);
102+
return v - v2;
103+
}
104+
catch (std::invalid_argument const& ex) {
105+
std::cout << "It can not be a double/int value !" << " Final error: " << ex.what() << std::endl;
106+
}
107+
return 0;
108+
}
109+
110+
double operator*(Token const& a, Token const& b) {
111+
try {
112+
double v = std::stod(a.TokenText);
113+
double v2 = std::stod(b.TokenText);
114+
return v * v2;
115+
}
116+
catch (std::invalid_argument const& ex) {
117+
std::cout << "It can not be a double/int value !" << " Final error: " << ex.what() << std::endl;
118+
}
119+
return 0;
120+
}
121+
122+
double operator/(Token const& a, Token const& b) {
123+
try {
124+
double v = std::stod(a.TokenText);
125+
double v2 = std::stod(b.TokenText);
126+
return v / v2;
127+
}
128+
catch (std::invalid_argument const& ex) {
129+
std::cout << "It can not be a double/int value !" << " Final error: " << ex.what() << std::endl;
130+
}
131+
return 0;
132+
}
133+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#pragma once
2+
3+
#include <iostream>
4+
#include <string>
5+
#include <vector>
6+
#include <stdexcept>
7+
8+
namespace MathParser {
9+
enum AllTokenTypes {
10+
WHITESPACE,
11+
INT,
12+
DOUBLE,
13+
MAYBE_DOUBLE,
14+
OPERATOR
15+
};
16+
17+
class Token {
18+
public:
19+
enum AllTokenTypes TokenType;
20+
std::string TokenText;
21+
22+
friend bool operator==(Token const& a, Token const& b);
23+
friend double operator+(Token const& a, Token const& b);
24+
friend double operator-(Token const& a, Token const& b);
25+
friend double operator*(Token const& a, Token const& b);
26+
friend double operator/(Token const& a, Token const& b);
27+
};
28+
29+
class TokenBuilding {
30+
public:
31+
static std::vector<Token> ParserTokens(const std::string& content);
32+
33+
static void endToken(Token &token, std::vector<Token> &tokens);
34+
};
35+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include "Converts.h"
2+
3+
double convertDouble(std::string& n) {
4+
try {
5+
double v = std::stod(n);
6+
return v;
7+
}
8+
catch (std::invalid_argument const& ex) {
9+
std::cout << "It can not be a double/int value !" << " Final error: " << ex.what() << std::endl;
10+
}
11+
12+
return 0;
13+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <string>
2+
#include <vector>
3+
#include <iostream>
4+
5+
double convertDouble(std::string& n);

0 commit comments

Comments
 (0)