Isis 3 Programmer Reference
InfixToPostfix.h
1 
6 /* SPDX-License-Identifier: CC0-1.0 */
7 
8 #ifndef INFIXTOPOSTFIX_H_
9 #define INFIXTOPOSTFIX_H_
10 
11 #include <stack>
12 #include <iostream>
13 
14 #include <QList>
15 #include <QString>
16 
17 namespace Isis {
18  class InfixOperator;
19  class InfixFunction;
20 
47  public:
49  virtual ~InfixToPostfix();
50 
51  QString convert(const QString &infix);
52  QString tokenizeEquation(const QString &equation);
53 
54  protected:
55 
56  virtual bool isKnownSymbol(QString representation);
57  virtual InfixOperator *findOperator(QString representation);
58 
59  QList<InfixOperator *> p_operators;
60 
61  private:
62  void initialize();
63  void uninitialize();
64 
65  QString formatFunctionCalls(QString equation);
66  QString cleanSpaces(QString equation);
67 
68  void closeParenthesis(QString &postfix, std::stack<InfixOperator> &theStack);
69  void addOperator(QString &postfix, const InfixOperator &op, std::stack<InfixOperator> &theStack);
70  bool isFunction(QString representation);
71  void checkArgument(QString funcName, int argNum, QString argument);
72  };
73 
81  class InfixOperator {
82  public:
83  InfixOperator(int prec, QString inString, bool isFunc = false) {
84  m_precedence = prec;
85  m_inputString = inString;
86  m_outputString = inString;
87  m_isFunction = isFunc;
88  }
89 
90  InfixOperator(int prec, QString inString, QString outString,
91  bool isFunc = false) {
92  m_precedence = prec;
93  m_inputString = inString;
94  m_outputString = outString;
95  m_isFunction = isFunc;
96  }
97 
98  const QString &inputString() const {
99  return m_inputString;
100  }
101 
102  const QString &outputString() const {
103  return m_outputString;
104  }
105 
106  int precedence() const {
107  return m_precedence;
108  }
109 
110  bool isFunction() const {
111  return m_isFunction;
112  }
113 
114 
115  private:
116  int m_precedence;
117  QString m_inputString;
118  QString m_outputString;
119  bool m_isFunction;
120  };
121 
122 
130  class InfixFunction : public InfixOperator {
131  public:
132  InfixFunction(QString inString, int argCount) :
133  InfixOperator(-1, inString, true) {
134  m_numArguments = argCount;
135  }
136 
137  InfixFunction(QString inString, QString outString, int argCount) :
138  InfixOperator(-1, inString, outString, true) {
139  m_numArguments = argCount;
140  }
141 
142  int argumentCount() const {
143  return m_numArguments;
144  }
145 
146  private:
147  int m_numArguments;
148  };
149 };
150 
151 #endif
Isis::InfixToPostfix::findOperator
virtual InfixOperator * findOperator(QString representation)
This method will return a pointer to the operator represented by 'representation.
Definition: InfixToPostfix.cpp:347
Isis::InfixFunction
InfixOperator and InfixFunction are helper classes for InfixToPostfix.
Definition: InfixToPostfix.h:130
QList
This is free and unencumbered software released into the public domain.
Definition: BoxcarCachingAlgorithm.h:13
Isis::InfixToPostfix::tokenizeEquation
QString tokenizeEquation(const QString &equation)
This method will add spaces between all operators and numbers, making it possible to get each element...
Definition: InfixToPostfix.cpp:369
Isis::InfixToPostfix::convert
QString convert(const QString &infix)
This method converts infix to postfix.
Definition: InfixToPostfix.cpp:139
Isis::InfixToPostfix::InfixToPostfix
InfixToPostfix()
Constructor.
Definition: InfixToPostfix.cpp:19
Isis::InfixOperator
InfixOperator and InfixFunction are helper classes for InfixToPostfix.
Definition: InfixToPostfix.h:81
Isis::InfixToPostfix
Converter for math equations.
Definition: InfixToPostfix.h:46
Isis::InfixToPostfix::cleanSpaces
QString cleanSpaces(QString equation)
This function takes a space-delimited string and removes empty delimiters.
Definition: InfixToPostfix.cpp:109
Isis::InfixToPostfix::addOperator
void addOperator(QString &postfix, const InfixOperator &op, std::stack< InfixOperator > &theStack)
This is straight from the algorithm found on page 159 of "Data Structures & Algorithms in Java" Secon...
Definition: InfixToPostfix.cpp:287
Isis::InfixToPostfix::uninitialize
void uninitialize()
This cleans the known operators/functions list.
Definition: InfixToPostfix.cpp:89
Isis::InfixToPostfix::isFunction
bool isFunction(QString representation)
This method will return true if 'representation' is a known function.
Definition: InfixToPostfix.cpp:270
Isis::InfixToPostfix::isKnownSymbol
virtual bool isKnownSymbol(QString representation)
This method will return true if it believes the argument represents a valid function or operator.
Definition: InfixToPostfix.cpp:253
Isis::InfixToPostfix::closeParenthesis
void closeParenthesis(QString &postfix, std::stack< InfixOperator > &theStack)
This is straight from the algorithm found on page 159 of "Data Structures & Algorithms in Java" Secon...
Definition: InfixToPostfix.cpp:316
Isis::InfixToPostfix::initialize
void initialize()
This populates the known operators/functions list.
Definition: InfixToPostfix.cpp:31
Isis::InfixToPostfix::formatFunctionCalls
QString formatFunctionCalls(QString equation)
This method looks through equation for function calls, parenthesizes them, and calls itself again wit...
Definition: InfixToPostfix.cpp:479
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16