PssMathParser  1.0.0
Parser of mathemathical expressions
 All Classes Namespaces Files Functions Variables Enumerations Macros Pages
pssmathparser.h
Go to the documentation of this file.
1 
23 #ifndef PSSMATHPARSER_H
24 #define PSSMATHPARSER_H
25 
26 #include "pssmathparser_global.h"
27 #include <math.h>
28 #include <stdlib.h>
29 #include <iostream>
30 #include <stdint.h>
31 #include <unordered_map>
32 #include <vector>
33 #include <string>
34 #include <ctype.h>
35 #include <algorithm>
36 #include <iomanip>
37 #include <sstream>
38 
49 namespace PssMathParser {
50 
51 using namespace std;
52 
56 enum class EntityType {
57  None,
58  Operator,
59  OperatorInDoubleOutDouble,
60  OperatorInIntOutInt,
61  OperatorInDoubleDoubleOutDouble,
62  OperatorInDoubleIntOutDouble,
63  Argument,
64  ArgumentConstant,
65  ArgumentUserConstant,
66  ArgumentVariable,
67  ArgumentGenerated,
68  ArgumentGeneratedFromOneArg,
69  ArgumentGeneratedFromTwoArg
70 };
71 
75 enum class OperatorPrecedence {
76  Function = 9,
77  Multiplication = 6,
78  Addition = 3
79 };
80 
87 class Entity {
88 
89 public:
90  Entity(const string &a_name,
91  const EntityType a_type = EntityType::None);
92  ~Entity();
93 
94  const Entity *getEntity() const;
95  EntityType entityType() const;
96 
97 protected:
98  string m_name;
100 };
101 
111 class Operator : public Entity {
112 
113 public:
114  Operator(const string &a_name,
115  EntityType a_type = EntityType::Operator,
116  OperatorPrecedence m_precedence = OperatorPrecedence::Function,
117  double (*a_ddOperator)(const double) = nullptr,
118  double (*a_dddOperator)(const double, const double) = nullptr,
119  double (*a_ddiOperator)(const double, const int) = nullptr);
120 
121  double dddOperator(double a_arg1, double a_arg2) const;
122  double ddOperator(double a_arg1) const;
123  uint16_t precedence() const;
124 private:
126  double (*m_ddOperator)(const double);
127  double (*m_dddOperator)(const double, const double);
128  double (*m_ddiOperator)(const double, const int);
129 };
130 
136 class Argument : public Entity {
137 public:
138  Argument(const string &a_name,
139  EntityType a_type = EntityType::Argument,
140  int a_ivalue = 0,
141  double a_dvalue = 0);
142 
143  void setDoubleValue(const double a_dvalue);
144  double getDoubleValue() const;
145  void setIntValue(const int a_ivalue);
146  int getIntValue();
147 private:
148  double m_dvalue;
149  int m_ivalue;
150 };
151 
162 class Generator : public Entity {
163 public:
164  Generator(const string &a_name,
165  EntityType a_entityType,
166  const Operator *a_op = nullptr,
167  const Argument *a_arg1 = nullptr,
168  const Argument *a_arg2 = nullptr,
169  Argument *a_myArg = nullptr);
170 
171  double generateDoubleValue() const;
172  const string &getName() const;
173 
174 private:
175  const Operator *m_op;
176  const Argument *m_arg1;
177  const Argument *m_arg2;
179 };
180 
187 class PSSMATHPARSER_EXPORT_PUBLIC MathParser
188 {
189 public:
190 
191  virtual ~MathParser() = 0;
192 
193  static MathParser *makeMathParser();
194 
195  virtual const string expression() const = 0;
196  virtual void setExpression(const string &a_expression) = 0;
197  virtual void setMath(const string &a_expression) = 0;
198  virtual const string reversePolish() const = 0;
199  virtual bool setArgumentMap(const string &a_reversePolish) = 0;
200  virtual bool setArgumentMap() = 0;
201  virtual bool expressionToReversePolish() = 0;
202  virtual const string mathToString() const = 0;
203  virtual const string mathToStringFull() const = 0;
204  virtual void setVariableDouble(const string &a_name,
205  const double a_value) = 0;
206  virtual bool expandMathExpression() = 0;
207  virtual double calculateExpression() = 0;
208  virtual void setMathPrintPrecision(const uint16_t &mathPrintPrecision) = 0;
209  virtual uint16_t getMathPrintPrecision() const = 0;
210  virtual uint16_t getVariableSize() const = 0;
211  virtual void clear() = 0;
212 };
213 
214 
222 {
223 public:
224 
225  MathExpression();
226  ~MathExpression();
227 
228  static vector<char> specialChars;
229  static unordered_map<string, Operator> operatorMap;
230  static unordered_map<string, Argument> constantMap;
232  static double add(const double a_arg1, const double a_arg2);
233  static double subtract(const double a_arg1, const double a_arg2);
234  static double multiply(const double a_arg1, const double a_arg2);
235  static double divide(const double a_arg1, const double a_arg2);
236  static bool isNumber(const string &a_str);
237  static bool isSpecialCharacter(const char &a_char);
238  static bool isSpecialNoParenthesis(const char &a_char);
239  static double convertStringToDouble(const string &a_str);
240  static double getResultdddOperator(
241  double a_arg1,
242  double a_arg2,
243  const string &a_operatorKey);
244 
245  const string expression() const;
246  void setExpression(const string &a_expression);
247 
248  void setMath(const string &a_expression);
249 
250  const string reversePolish() const;
251  bool setArgumentMap(const string &a_reversePolish);
252  bool setArgumentMap();
253 
254  bool expressionToReversePolish();
255 
256  double calculateExpression();
257 
258  uint32_t reversePolishErrorNum();
259  const string reversePolishErrorString();
260 
261  uint32_t expressionErrorNum();
262  const string expressionErrorString();
263 
264  const string mathToString() const;
265  const string mathToStringFull() const;
266 
267  bool expandMathExpression();
268 
269  void clear();
270 
271  void setMathPrintPrecision(const uint16_t &mathPrintPrecision);
272  uint16_t getMathPrintPrecision() const;
273 
274  void setVariableDouble(const string &a_name, const double a_value);
275 
276  uint16_t getVariableSize() const;
277 
278 private:
279  size_t operatorMapSize();
280  bool hasOperatorMap(const string &a_operatorName) const;
281  bool hasConstantMap(const string &a_constantName) const;
282  bool hasArgumentMap(const string &a_argumentName) const;
283  bool hasGeneratorMap(const string &a_generatorName) const;
284  bool isOperator(const string a_entityName) const;
285  bool isArgument(const string a_entityName) const;
286  bool isGenerator(const string a_entityName) const;
287  bool isOperatorOneArg(const string a_entityName) const;
288  bool isOperatorTwoArg(const string a_entityName) const;
289  bool generateArgArgOp(const string &a_key1,
290  const string &a_key2,
291  const string &a_key3);
292  bool generateArgOp(const string &a_key1,
293  const string &a_key2);
294  double getArgumentDoubleValue(const string &a_key) const;
295  double getConstantDoubleValue(const string &a_key) const;
296  const Argument *getConstant(const string &a_key) const;
297  const Operator *getOperator(const string &a_key) const;
298  Generator *getGenerator(const string &a_key);
299  const Argument *getArgument(const string &a_key) const;
300  uint16_t getEntitySize(EntityType a_entityType) const;
301  EntityType entityType(const string &a_key) const;
302  void setDoubleValueToArgument(const string &a_key, const double a_value);
303  const string createNewUserConstantName() const;
304  bool pushToReversePolish(const string &a_str, const char &a_char);
305  void appendToReversePolishString(const string &a_str);
306 
309  uint32_t m_expressionError;
311  string m_expression;
314  unordered_map<string, Argument> m_argumentMap;
315  vector<Generator> m_generatorVec;
316  vector<string> m_math;
317  vector<string> m_RPstack;
318 };
319 
320 }
321 
322 #endif // PSSMATHPARSER_H
uint32_t m_reversePolishError
Definition: pssmathparser.h:307
uint32_t m_expressionError
Definition: pssmathparser.h:309
EntityType m_type
Definition: pssmathparser.h:99
The export point of this library.
Definition: pssmathparser.h:187
static unordered_map< string, Operator > operatorMap
Available operators in a map.
Definition: pssmathparser.h:229
const Argument * m_arg2
Definition: pssmathparser.h:177
const Operator * m_op
Definition: pssmathparser.h:175
string m_reversePolish
Definition: pssmathparser.h:312
Argument * m_myArg
Definition: pssmathparser.h:178
const Argument * m_arg1
Definition: pssmathparser.h:176
Derived from Entity, used for intermediate steps of calcualtion.
Definition: pssmathparser.h:162
Derived from Entity, used for storing pointers to math functions.
Definition: pssmathparser.h:111
vector< Generator > m_generatorVec
Definition: pssmathparser.h:315
string m_reversePolishErrorString
Definition: pssmathparser.h:308
OperatorPrecedence
Enum defines precedence of operators.
Definition: pssmathparser.h:75
static unordered_map< string, Argument > constantMap
Available constants in a map.
Definition: pssmathparser.h:230
vector< string > m_RPstack
Definition: pssmathparser.h:317
string m_name
Definition: pssmathparser.h:98
Global headers for the PssMathParser.
int m_ivalue
Definition: pssmathparser.h:149
Base class for math expression entities.
Definition: pssmathparser.h:87
EntityType
Enum defines all the types of functions for the operator.
Definition: pssmathparser.h:56
uint16_t m_mathPrintPrecision
Definition: pssmathparser.h:313
string m_expression
Definition: pssmathparser.h:311
double m_dvalue
Definition: pssmathparser.h:148
Derived from Entity, used for all that is not Operator.
Definition: pssmathparser.h:136
string m_expressionErrorString
Definition: pssmathparser.h:310
OperatorPrecedence m_precedence
Definition: pssmathparser.h:125
static vector< char > specialChars
Special characters found in the infix notation.
Definition: pssmathparser.h:228
unordered_map< string, Argument > m_argumentMap
Definition: pssmathparser.h:314
Main functionality of this library.
Definition: pssmathparser.h:221
vector< string > m_math
Definition: pssmathparser.h:316