Calculator and StdDev Tool 1.0
Python-based calculator with GUI and standard deviation functionality
Loading...
Searching...
No Matches
parser.EquationSolver Class Reference

Public Member Functions

None __init__ (self, str equation)
 A class for parsing and solving mathematical equations from a string representation.
 
float getResult (self)
 Returns the result of the equation.
 
List[strNumgetTokens (self)
 Returns the tokenized form of the equation.
 
 solve (self)
 Parses and solves the equation.
 

Private Member Functions

None __addbrackets (self)
 Ensures all opened brackets are closed by appending missing closing brackets.
 
None __addZeroBinary (self)
 Adds a zero before binary operators if they are at the beginning or after an opening bracket.
 
List[strNum__binaryOps (self, List[strNum] tokens, str ops)
 Evaluates binary operations like addition, subtraction, etc.
 
List[strNum__calc (self, List[strNum] tokens)
 Evaluates value of the given token list using operator precedence.
 
None __checkOpsNum (self)
 Validates the sequence of tokens for correct number/operator placement.
 
None __convertToNum (self)
 Converts numeric string tokens to float type in the token list.
 
List[strNum__handleBrackets (self, List[strNum] tokens)
 Recursively evaluates bracketed expressions in the token list.
 
bool __isFloat (self, str num)
 Checks if a string or int can be converted to a float.
 
bool __isNumOrDot (self, str char)
 Checks if a character is a digit or a decimal point.
 
bool __isOperator (self, str char)
 Checks if a character is a valid operator.
 
List[strNum__specialOps (self, List[strNum] tokens, str ops)
 Evaluates special unary operators like log, factorial, and root.
 
List[str] __textDiver (self)
 Splits the equation text into tokens.
 

Private Attributes

 __equationText
 
 __equationTokens
 
 __opsBinary
 
 __opsBracket
 
 __opsFunc
 
 __opsUnary
 
 __solved
 
 __value
 

Constructor & Destructor Documentation

◆ __init__()

None parser.EquationSolver.__init__ (   self,
str  equation 
)

A class for parsing and solving mathematical equations from a string representation.

Constructor for EquationSolver.

Parameters
equationThe equation string to be solved.
Exceptions
TypeErrorIf the equation is not a string.
ValueErrorIf the equation is an empty string.

Member Function Documentation

◆ __addbrackets()

None parser.EquationSolver.__addbrackets (   self)
private

Ensures all opened brackets are closed by appending missing closing brackets.

Exceptions
TypeErrorIf there are more closing than opening brackets.

◆ __addZeroBinary()

None parser.EquationSolver.__addZeroBinary (   self)
private

Adds a zero before binary operators if they are at the beginning or after an opening bracket.

Exceptions
ValueErrorIf the equation token list is empty.

◆ __binaryOps()

List[strNum] parser.EquationSolver.__binaryOps (   self,
List[strNum tokens,
str  ops 
)
private

Evaluates binary operations like addition, subtraction, etc.

Parameters
tokensThe token list to process.
opsThe binary operator to evaluate.
Returns
Updated token list after evaluating the binary operation.
Exceptions
ValueErrorIf the operation is malformed or operands are invalid.

◆ __calc()

List[strNum] parser.EquationSolver.__calc (   self,
List[strNum tokens 
)
private

Evaluates value of the given token list using operator precedence.

Parameters
tokensThe token list to evaluate.
Returns
The result as a single-item list or partial evaluation.
Exceptions
ValueErrorIf the input is invalid or incomplete.

◆ __checkOpsNum()

None parser.EquationSolver.__checkOpsNum (   self)
private

Validates the sequence of tokens for correct number/operator placement.

Exceptions
TypeErrorIf numbers are placed without operators or if an invalid token is found.

◆ __convertToNum()

None parser.EquationSolver.__convertToNum (   self)
private

Converts numeric string tokens to float type in the token list.

◆ __handleBrackets()

List[strNum] parser.EquationSolver.__handleBrackets (   self,
List[strNum tokens 
)
private

Recursively evaluates bracketed expressions in the token list.

Parameters
tokensList of tokens including bracketed expressions.
Returns
Evaluated token list with brackets resolved.
Exceptions
TypeErrorIf brackets are unmatched.

◆ __isFloat()

bool parser.EquationSolver.__isFloat (   self,
str  num 
)
private

Checks if a string or int can be converted to a float.

param num The string to check.

Returns
True if the string represents a float, False otherwise.

◆ __isNumOrDot()

bool parser.EquationSolver.__isNumOrDot (   self,
str  char 
)
private

Checks if a character is a digit or a decimal point.

Parameters
charThe character to check.
Returns
True if the character is a number or '.', False otherwise.

◆ __isOperator()

bool parser.EquationSolver.__isOperator (   self,
str  char 
)
private

Checks if a character is a valid operator.

Parameters
charThe character to check.
Returns
True if the character is an operator, False otherwise.

◆ __specialOps()

List[strNum] parser.EquationSolver.__specialOps (   self,
List[strNum tokens,
str  ops 
)
private

Evaluates special unary operators like log, factorial, and root.

Parameters
tokensThe token list to process.
opsThe operator to evaluate.
Returns
Updated token list after evaluating special operations.
Exceptions
ValueErrorIf the syntax is invalid or operands are missing.

◆ __textDiver()

List[str] parser.EquationSolver.__textDiver (   self)
private

Splits the equation text into tokens.

Returns
List of string tokens.

◆ getResult()

float parser.EquationSolver.getResult (   self)

Returns the result of the equation.

Returns
The evaluated result as a float.
Note
Automatically solves the equation if not already solved.

◆ getTokens()

List[strNum] parser.EquationSolver.getTokens (   self)

Returns the tokenized form of the equation.

Returns
List of equation tokens.
Note
Automatically solves the equation if not already solved.

◆ solve()

parser.EquationSolver.solve (   self)

Parses and solves the equation.

Exceptions
TypeErrorIf there are parsing or evaluation issues.
ValueErrorIf the evaluation results in an error.

Member Data Documentation

◆ __equationText

parser.EquationSolver.__equationText
private

◆ __equationTokens

parser.EquationSolver.__equationTokens
private

◆ __opsBinary

parser.EquationSolver.__opsBinary
private

◆ __opsBracket

parser.EquationSolver.__opsBracket
private

◆ __opsFunc

parser.EquationSolver.__opsFunc
private

◆ __opsUnary

parser.EquationSolver.__opsUnary
private

◆ __solved

parser.EquationSolver.__solved
private

◆ __value

parser.EquationSolver.__value
private

The documentation for this class was generated from the following file: