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
17namespace 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
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
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
InfixOperator and InfixFunction are helper classes for InfixToPostfix.
InfixOperator and InfixFunction are helper classes for InfixToPostfix.
Converter for math equations.
virtual InfixOperator * findOperator(QString representation)
This method will return a pointer to the operator represented by 'representation.
QString convert(const QString &infix)
This method converts infix to postfix.
bool isFunction(QString representation)
This method will return true if 'representation' is a known function.
QString cleanSpaces(QString equation)
This function takes a space-delimited string and removes empty delimiters.
void initialize()
This populates the known operators/functions list.
virtual bool isKnownSymbol(QString representation)
This method will return true if it believes the argument represents a valid function or operator.
void uninitialize()
This cleans the known operators/functions list.
InfixToPostfix()
Constructor.
QString tokenizeEquation(const QString &equation)
This method will add spaces between all operators and numbers, making it possible to get each element...
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...
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...
QString formatFunctionCalls(QString equation)
This method looks through equation for function calls, parenthesizes them, and calls itself again wit...
This is free and unencumbered software released into the public domain.
Definition Apollo.h:16