Isis Developer Reference
InfixToPostfix.h
Go to the documentation of this file.
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 
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::InfixOperator::isFunction
bool isFunction() const
Definition: InfixToPostfix.h:110
Isis::IString::DownCase
IString DownCase()
Converts all upper case letters in the object IString into lower case characters.
Definition: IString.cpp:644
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
virtual ~InfixToPostfix()
Definition: InfixToPostfix.cpp:23
Isis::InfixToPostfix::InfixToPostfix
InfixToPostfix()
Constructor.
Definition: InfixToPostfix.cpp:19
InfixToPostfix.h
Isis::InfixFunction::InfixFunction
InfixFunction(QString inString, QString outString, int argCount)
Definition: InfixToPostfix.h:137
Isis::InfixFunction::argumentCount
int argumentCount() const
Definition: InfixToPostfix.h:142
Isis::InfixOperator::precedence
int precedence() const
Definition: InfixToPostfix.h:106
Isis::toString
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
Definition: IString.cpp:211
IString.h
Isis::InfixOperator::InfixOperator
InfixOperator(int prec, QString inString, QString outString, bool isFunc=false)
Definition: InfixToPostfix.h:90
_FILEINFO_
#define _FILEINFO_
Macro for the filename and line number.
Definition: IException.h:24
Isis::InfixOperator::InfixOperator
InfixOperator(int prec, QString inString, bool isFunc=false)
Definition: InfixToPostfix.h:83
Isis::InfixToPostfix::p_operators
QList< InfixOperator * > p_operators
Definition: InfixToPostfix.h:59
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::IException
Isis exception class.
Definition: IException.h:91
IException.h
Isis::toDouble
double toDouble(const QString &string)
Global function to convert from a string to a double.
Definition: IString.cpp:149
std
Namespace for the standard library.
Isis::IString::Token
IString Token(const IString &separator)
Returns the first token in the IString.
Definition: IString.cpp:897
Isis::IString
Adds specific functionality to C++ strings.
Definition: IString.h:165
Isis::InfixFunction::InfixFunction
InfixFunction(QString inString, int argCount)
Definition: InfixToPostfix.h:132
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::IString::Remove
IString Remove(const std::string &del)
Remove all instances of any character in the string from the IString.
Definition: IString.cpp:1266
Isis
This is free and unencumbered software released into the public domain.
Definition: Apollo.h:16
Isis::IString::ToQt
QString ToQt() const
Retuns the object string as a QString.
Definition: IString.cpp:869
Isis::InfixOperator::inputString
const QString & inputString() const
Definition: InfixToPostfix.h:98
Isis::InfixOperator::outputString
const QString & outputString() const
Definition: InfixToPostfix.h:102