Esempio n. 1
0
 public void Refresh(ActionHolder actionHolder, IAlgorithmContext algoContext)
 {
     ActionHolder     = actionHolder;
     AlgorithmContext = algoContext;
     _checkerAction   = (CheckerAction)actionHolder.Action;
     textBlock.Text   = _checkerAction.ComparisonType.Caption;
 }
Esempio n. 2
0
 public void Refresh(ActionHolder actionHolder, IAlgorithmContext algoContext)
 {
     AlgorithmContext = algoContext;
     ActionHolder     = actionHolder;
     _action          = (CheckerAction)actionHolder.Action;
     action1View.Refresh(_action.TargetAction1Holder, algoContext);
     action2View.Refresh(_action.TargetAction2Holder, algoContext);
     comparisonView.Refresh(actionHolder, algoContext);
     Action2EqualizeToAction1();
 }
Esempio n. 3
0
        /// <summary>
        /// Calculates the function value depending on the argument passed.
        /// </summary>
        /// <param name="argumentValue"></param>
        /// <returns></returns>
        public double Value(double argumentValue)
        {
            UnaryAction   unaryAction;
            BinaryAction  binaryAction;
            CheckerAction checkerAction;

            actionResults = new double[actions.Count];

            for (int i = 0; i < actions.Count; ++i)
            {
                string currentActionString = actions[i];

                if (string.IsNullOrWhiteSpace(currentActionString))
                {
                    throw new FunctionActionSyntaxException("Action string for action number " + i + " should not be empty.");
                }

                unaryAction   = new UnaryAction(x => x);
                binaryAction  = new BinaryAction((x, y) => (x + y));
                checkerAction = new CheckerAction(x => (x == 0));

                // Binary or unary action flag.
                // -
                bool binary = true;

                // If no user-defined error messages in the action,
                // remove all whitespace.
                // -
                if (currentActionString.IndexOf('#') == -1)
                {
                    currentActionString = currentActionString.Replace(" ", "");
                }
                // Otherwise, do some magic.
                // -
                else
                {
                    StringBuilder stringWithoutWhitespace = new StringBuilder();
                    bool          isInsideErrorMessage    = false;

                    for (int j = 0; j < currentActionString.Length; j++)
                    {
                        if (currentActionString[j] == '#')
                        {
                            isInsideErrorMessage = !isInsideErrorMessage;
                        }
                        else if (currentActionString[j] != ' ' || isInsideErrorMessage)
                        {
                            stringWithoutWhitespace.Append(currentActionString[j]);
                        }
                    }

                    currentActionString = stringWithoutWhitespace.ToString();
                }

                double a, b;
                string operand1, operand2, operand3;

                switch (currentActionString.getActionSubString())
                {
                case "ret": binary = false; unaryAction = new UnaryAction(x => x); break;

                case "+": binaryAction = new BinaryAction((x, y) => (x + y)); break;

                case "-": binaryAction = new BinaryAction((x, y) => (x - y)); break;

                case "*": binaryAction = new BinaryAction((x, y) => (x * y)); break;

                case "/": binaryAction = new BinaryAction((x, y) => (x / y)); break;

                case "^": binaryAction = new BinaryAction(Math.Pow); break;

                case "abs": binary = false; unaryAction = new UnaryAction(Math.Abs); break;

                case "floor": binary = false; unaryAction = new UnaryAction(Math.Floor); break;

                case "ceil": binary = false; unaryAction = new UnaryAction(Math.Ceiling); break;

                case "round": binary = false; unaryAction = new UnaryAction(number => Math.Round(number, MidpointRounding.AwayFromZero)); break;

                case "sin": binary = false; unaryAction = new UnaryAction(Math.Sin); break;

                case "cos": binary = false; unaryAction = new UnaryAction(Math.Cos); break;

                case "tg": binary = false; unaryAction = new UnaryAction(Math.Tan); break;

                case "ctg": binary = false; unaryAction = new UnaryAction(x => (1 / Math.Tan(x))); break;

                case "arcsin": binary = false; unaryAction = new UnaryAction(Math.Asin); break;

                case "arccos": binary = false; unaryAction = new UnaryAction(Math.Acos); break;

                case "arctg": binary = false; unaryAction = new UnaryAction(Math.Atan); break;

                case "sinh": binary = false; unaryAction = new UnaryAction(Math.Sinh); break;

                case "cosh": binary = false; unaryAction = new UnaryAction(Math.Cosh); break;

                case "log": binary = true; binaryAction = new BinaryAction(Math.Log); break;

                case "lg": binary = false; unaryAction = new UnaryAction(Math.Log10); break;

                case "sqrt": binary = false; unaryAction = new UnaryAction(Math.Sqrt); break;

                case "ln": binary = false; unaryAction = new UnaryAction(Math.Log); break;

                case "exp": binary = false; unaryAction = new UnaryAction(Math.Exp); break;

                case "sign": binary = false; unaryAction = new UnaryAction(x => Math.Sign(x)); break;

                case ">":
                case "=":
                case ">=":
                case "<=":
                case "<":
                case "!=": goto CONDITION;

                default: throw new FunctionActionSyntaxException("Unknown function name in the list of actions.");
                }

                if (binary)
                {
                    operand1 = currentActionString.getFirstOperand();
                    operand2 = currentActionString.getSecondOperand();

                    try
                    {
                        a = operandMeaning(operand1, composedFunc, actionResults, i, argumentValue);
                        b = operandMeaning(operand2, composedFunc, actionResults, i, argumentValue);
                    }
                    catch (Exception actionExecutionException)
                    {
                        throw new FunctionActionExecutionException(actionExecutionException.Message, i);
                    }

                    double operationResult; // результат операции

                    try
                    {
                        operationResult = binaryAction(a, b);
                    }
                    catch
                    {
                        operationResult = double.NaN;
                    }

                    actionResults[i] = operationResult;
                    goto RETURNER;
                }
                else
                {
                    operand1 = currentActionString.getFirstOperand();

                    try
                    {
                        a = operandMeaning(operand1, composedFunc, actionResults, i, argumentValue);
                    }
                    catch (Exception xxx)
                    {
                        if (xxx is FunctionActionUserThrownException)
                        {
                            throw;
                        }
                        else
                        {
                            throw new FunctionActionExecutionException(xxx.Message, i);
                        }
                    }

                    double operationResult;

                    try { operationResult = unaryAction(a); }
                    catch { operationResult = double.NaN; }

                    actionResults[i] = operationResult;
                    goto RETURNER;
                }

                // This block executes in case of the conditional action operator.
                // -
CONDITION:

                if (i == 0)
                {
                    throw new FunctionActionSyntaxException("No conditional operators are allowed in the action number 0.");
                }

                // Should declare additional variable since the action is ternary.
                // -
                double c;

                operand1 = currentActionString.getFirstOperand();
                operand2 = currentActionString.getSecondOperand();
                operand3 = currentActionString.getThirdOperand();

                try
                {
                    a = operandMeaning(operand1, composedFunc, actionResults, i, argumentValue);
                }
                catch (Exception xxx)
                {
                    if (xxx is FunctionActionUserThrownException)
                    {
                        throw;
                    }
                    throw new Exception("Ошибка при выполнении действия " + i + ". " + xxx.Message);
                }

                switch (currentActionString.getActionSubString())
                {
                case ">": checkerAction = new CheckerAction(x => (x > 0)); break;

                case "<": checkerAction = new CheckerAction(x => (x < 0)); break;

                case "=": checkerAction = new CheckerAction(x => (x == 0)); break;

                case ">=": checkerAction = new CheckerAction(x => (x >= 0)); break;

                case "<=": checkerAction = new CheckerAction(x => (x <= 0)); break;

                case "!=": checkerAction = new CheckerAction(x => (x != 0)); break;
                }

                if (checkerAction(a))
                {
                    try
                    {
                        b = operandMeaning(operand2, composedFunc, actionResults, i, argumentValue);
                        actionResults[i] = b;
                    }
                    catch (Exception xxx)
                    {
                        if (xxx is FunctionActionUserThrownException)
                        {
                            throw;
                        }
                        throw new Exception("Error while performing action number " + i + ". " + xxx.Message);
                    }
                }
                else
                {
                    try
                    {
                        c = operandMeaning(operand3, composedFunc, actionResults, i, argumentValue);
                        actionResults[i] = c;
                    }
                    catch (Exception xxx)
                    {
                        if (xxx is FunctionActionUserThrownException)
                        {
                            throw;
                        }
                        throw new Exception("Error while performing action number " + i + ". " + xxx.Message);
                    }
                }

                RETURNER :;
            }

            return(actionResults[actionResults.Length - 1]);
        }