Isis 3 Programmer Reference
|
Converter for math equations. More...
#include <InfixToPostfix.h>
Public Member Functions | |
InfixToPostfix () | |
Constructor. | |
QString | convert (const QString &infix) |
This method converts infix to postfix. | |
QString | tokenizeEquation (const QString &equation) |
This method will add spaces between all operators and numbers, making it possible to get each element of the equation one by one. | |
Protected Member Functions | |
virtual bool | isKnownSymbol (QString representation) |
This method will return true if it believes the argument represents a valid function or operator. | |
virtual InfixOperator * | findOperator (QString representation) |
This method will return a pointer to the operator represented by 'representation. | |
Protected Attributes | |
QList< InfixOperator * > | p_operators |
Private Member Functions | |
void | initialize () |
This populates the known operators/functions list. | |
void | uninitialize () |
This cleans the known operators/functions list. | |
QString | formatFunctionCalls (QString equation) |
This method looks through equation for function calls, parenthesizes them, and calls itself again with each argument in order to parse any embedded functions. | |
QString | cleanSpaces (QString equation) |
This function takes a space-delimited string and removes empty delimiters. | |
void | closeParenthesis (QString &postfix, std::stack< InfixOperator > &theStack) |
This is straight from the algorithm found on page 159 of "Data Structures & Algorithms in Java" Second Edition by Robert Lafore. | |
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" Second Edition by Robert Lafore. | |
bool | isFunction (QString representation) |
This method will return true if 'representation' is a known function. | |
void | checkArgument (QString funcName, int argNum, QString argument) |
Converter for math equations.
This class converts infix equations to postfix
2007-08-22 Steven Lambright - Changed p_operators from vector to QList (static std::vector is unsafe)
2008-01-23 Steven Lambright - Added the operators (constants) PI and e
2010-02-23 Steven Lambright - Operator/Function input strings and output strings now separated
2010-04-08 Steven Lambright - Min, max functions expanded upon
2012-01-09 Jeff Anderson - Modified to conform ISIS programming standards
2017-01-09 Jesse Mapel - Modified to allow for "_" in variables. Fixes #4581.
2017-01-09 Jesse Mapel - Added logical and, or operators. Fixes #4581.
Definition at line 46 of file InfixToPostfix.h.
Isis::InfixToPostfix::InfixToPostfix | ( | ) |
|
virtual |
Definition at line 23 of file InfixToPostfix.cpp.
|
private |
This is straight from the algorithm found on page 159 of "Data Structures & Algorithms in Java" Second Edition by Robert Lafore.
postfix | The postix generated thus far |
op | The operator |
theStack | The operator stack |
Definition at line 287 of file InfixToPostfix.cpp.
Referenced by convert().
|
private |
Definition at line 694 of file InfixToPostfix.cpp.
|
private |
This function takes a space-delimited string and removes empty delimiters.
In order words, it compresses the spaces. This is used to keep strings small, clean, and remove the necessity to check for empty tokens constantly.
That is, if your string is "a b" the result will be "a b".
equation | A space-delimited string with excessive spaces |
Definition at line 109 of file InfixToPostfix.cpp.
References Isis::IString::Token(), and Isis::IString::ToQt().
Referenced by convert(), formatFunctionCalls(), and tokenizeEquation().
|
private |
This is straight from the algorithm found on page 159 of "Data Structures & Algorithms in Java" Second Edition by Robert Lafore.
postfix | The postix generated thus far |
theStack | The operator stack |
Definition at line 316 of file InfixToPostfix.cpp.
References Isis::IException::User.
Referenced by convert().
QString Isis::InfixToPostfix::convert | ( | const QString & | infix | ) |
This method converts infix to postfix.
It uses an enhanced verion of the algorithm found on page 159 of "Data Structures & Algorithms in Java" Second Edition by Robert Lafore. First, we prep the equation with TokenizeEquation and then parse through it using the known-good algorithm.
infix | The infix equation |
Definition at line 139 of file InfixToPostfix.cpp.
References addOperator(), cleanSpaces(), closeParenthesis(), findOperator(), isFunction(), isKnownSymbol(), Isis::toDouble(), Isis::IString::Token(), tokenizeEquation(), Isis::IString::ToQt(), and Isis::IException::User.
Referenced by Isis::InlineCalculator::toPostfix().
|
protectedvirtual |
This method will return a pointer to the operator represented by 'representation.
' Because in this model a function is an operator, this will return a pointer to functions as well (in a base class pointer).
representation | The symbolic representation of the operator, such as '+' |
Reimplemented in Isis::InlineInfixToPostfix, and Isis::CubeInfixToPostfix.
Definition at line 347 of file InfixToPostfix.cpp.
References Isis::IException::User.
Referenced by convert(), Isis::InlineInfixToPostfix::findOperator(), Isis::CubeInfixToPostfix::findOperator(), formatFunctionCalls(), and isFunction().
|
private |
This method looks through equation for function calls, parenthesizes them, and calls itself again with each argument in order to parse any embedded functions.
This ensures order of operations holds for cases like sin(.5)^2. This method might add too many parentheses, but that is harmless. This does not affect operators (excepting –) or numbers. Only functions. The input should be space-delimited.
equation | The unparenthesized equation |
This code block is for multi-parameter functions. Functions with 1+ parameters are parsed here, excepting the negation (with no parentheses) which is done just above.
We figure out the arguments by looking for commas, outside of all parentheses, for each argument up until the last one. We look for an extra closing parenthesis for the last argument. When we figure out what an argument is, we recursively call FormatFunctionCalls to format any and all functionality inside of the argument.
Definition at line 479 of file InfixToPostfix.cpp.
References cleanSpaces(), findOperator(), formatFunctionCalls(), isFunction(), and Isis::IException::User.
Referenced by formatFunctionCalls(), and tokenizeEquation().
|
private |
This populates the known operators/functions list.
If the operators list is not empty, this function will do nothing.
Definition at line 31 of file InfixToPostfix.cpp.
Referenced by InfixToPostfix().
|
private |
This method will return true if 'representation' is a known function.
representation | The symbolic representation of a function, such as 'abs' |
Definition at line 270 of file InfixToPostfix.cpp.
References findOperator(), and isKnownSymbol().
Referenced by convert(), Isis::CubeInfixToPostfix::findOperator(), formatFunctionCalls(), and Isis::CubeInfixToPostfix::isKnownSymbol().
|
protectedvirtual |
This method will return true if it believes the argument represents a valid function or operator.
representation | The symbolic representation of the operator, such as 'sin' |
Reimplemented in Isis::CubeInfixToPostfix, and Isis::InlineInfixToPostfix.
Definition at line 253 of file InfixToPostfix.cpp.
Referenced by convert(), isFunction(), and Isis::InlineInfixToPostfix::isKnownSymbol().
QString Isis::InfixToPostfix::tokenizeEquation | ( | const QString & | equation | ) |
This method will add spaces between all operators and numbers, making it possible to get each element of the equation one by one.
It will also parse out the function calls, adding parenthesis where needed so the user doesn't have to. The result is an equation ready for parsing (but NOT fully parenthesized, just enough to make sure our algorithm makes no mistakes).
equation | An unformatted infix equation |
Definition at line 369 of file InfixToPostfix.cpp.
References cleanSpaces(), and formatFunctionCalls().
Referenced by convert().
|
private |
This cleans the known operators/functions list.
Definition at line 89 of file InfixToPostfix.cpp.
|
protected |
Definition at line 59 of file InfixToPostfix.h.
U.S. Department of the Interior | U.S. Geological Survey ISIS | Privacy & Disclaimers | Astrogeology Research Program To contact us, please post comments and questions on the USGS Astrogeology Discussion Board To report a bug, or suggest a feature go to: ISIS Github File Modified: 10/05/2024 02:52:24 |