Expression

Public Methods

  • DONE Expression(string) - Creation constructor. Sets function to the passed string, checks the validity of the function and converts to infix.
  • DONE AddSetVariable(string, double) - Adds/sets a double variable and sets the value.
  • DONE AddSetVariable(string, bool) - Adds/sets a boolean variable and sets the value.
  • DONE Clear() - Removes the variables and functions.
  • DONE ClearVariables() - Removes the variables.
  • DONE double EvaluateD() - Evaluates the function using the given variables and values as a double expression.
  • DONE bool EvaluateB() - Evaluates the function as a boolean expression.
  • DONE Function property - Gets and sets the infix function. "Set" validates the function.
  • DONE PostFix property - Gets the postfix function.
  • DONE override string ToString() - Returns the infix function followed by the variables and their values. Ex: "(x+1)*(y-2); x = 1, y = 2"
  • ArrayList of strings GetFunctionVariables() - Returns the variables in the function.
  • Expression(Expression) - Copy constructor.

Protected Methods

  • DONE validate - Checks the validity of an infix function's syntax.
  • DONE string infix2Postfix(string) - Converts a string infix function to a postfix function.
  • DONE expand (string) - spaces the operators.
  • DONE getPrecedence(string) - returns an operators precedance.
  • DONE isOperand(string) - Checks to see if a string is on operand.
  • DONE isOperator(string) - Checks to see if a string is on operator.
  • DONE isVariable(string) - Checks to see if a string is a variable.

Protected Members

  • double cosnt TRUE = 1
  • double cosnt FALSE = 0
  • string postFunction
  • string inFunction

  • ArrayList variables - collection of variables
  • Class Variable
    • string name
    • double value

Exceptions

  • A syntactically invalid function.
  • Evaluation of a function when all variables are not present.

Remarks

  • Operators that are permitted and their precedence (low to high).
    +, -, ||
    *, /, &&
    ^, ==, >=, <=, <, >, !=, sign 
    abs, neg
    
  • Functions should not have to be preformatted to work
    • (1+2)^2 should be the same at ( 1 + 2 ) ^ 2
  • Use string.Replace("op", " op ") to add spaces around all the operators. then use string = Regex.Replace(string, @"[ ]+", @" ") and string.Trim() to remove any extra spacing from the function. After that, it should be possible to tokenize the function.

Syntax

  • Failure to adhere to the following syntax will result in undefined results.
  • To use ths abs function, surround the vaule to be absoluted with parenthesis.
    • abs(1-3) = 2
    • abs(neg5) = 5
  • To use the neg operator, if the value or variable is beside the operator, the value does not need to be surrounded with parenthesis. However, if the value is the result of other operators, you must surround the values with parenthesis.
    • neg 2 = -2
    • neg(abs(1-3)) = -2
    • neg(1-3) = 2

Topic revision: r8 - 09 Dec 2004, WillGray
 

This site is powered by FoswikiCopyright © 2013-2022 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Vanderbilt Biostatistics Wiki? Send feedback