Ejemplo n.º 1
0
        /// <summary>
        /// This function tries to obtain a numerical value
        /// but if not returns only equations
        /// </summary>
        /// <returns>a numerical value or an equation</returns>
        public override IArithmetic Compute()
        {
            IArithmetic output = this;

            if (this.InnerOperand != null)
            {
                IArithmetic inner = this.InnerOperand.Compute();
                switch (this.Operator)
                {
                case 'p':
                    output = this.InnerOperand.Compute();
                    break;

                case 'n':
                    output = new NumericValue(-this.InnerOperand.Compute().ToDouble());
                    break;

                case '\\':
                    double d = Convert.ToDouble(this.InnerOperand);
                    if (d != 0.0d)
                    {
                        output = new NumericValue(1 / d);
                    }
                    else
                    {
                        output = new NumericValue(Double.NaN);
                    }
                    break;

                case 'd':
                case 't':
                    output = this.InnerOperand.Compute();
                    break;
                }
            }
            return(output);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Constructor
 /// given a binary operation with two numbers
 /// </summary>
 /// <param name="op">operator id</param>
 /// <param name="n1">left number</param>
 /// <param name="n2">right number</param>
 protected BinaryOperation(char op, double n1, double n2)
 {
     this[operatorName]  = op;
     this[leftTermName]  = new NumericValue(n1);
     this[rightTermName] = new NumericValue(n2);
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Default constructor
 /// Empty data
 /// </summary>
 protected BinaryOperation()
 {
     this[operatorName]  = '+';
     this[leftTermName]  = new NumericValue(0.0d);
     this[rightTermName] = new NumericValue(0.0d);
 }
Ejemplo n.º 4
0
        /// <summary>
        /// This function tries to obtain a numerical value
        /// but if not returns only equations
        /// </summary>
        /// <returns>a numerical value or an equation</returns>
        public override IArithmetic Compute()
        {
            IArithmetic output = this;

            if (this.LeftOperand != null && this.RightOperand != null)
            {
                IArithmetic left, right;
                left  = this.LeftOperand.Compute();
                right = this.RightOperand.Compute();
                if (left is NumericValue && right is NumericValue)
                {
                    switch (this.Operator)
                    {
                    case '+':
                        output = new NumericValue(left.ToDouble() + right.ToDouble());
                        break;

                    case '-':
                        output = new NumericValue(left.ToDouble() - right.ToDouble());
                        break;

                    case '*':
                        output = new NumericValue(left.ToDouble() * right.ToDouble());
                        break;

                    case '/':
                        double d = right.ToDouble();
                        if (d != 0.0d)
                        {
                            output = new NumericValue(left.ToDouble() / d);
                        }
                        else
                        {
                            Arithmetic.RaiseEventError(new OverflowException("valeur 1 / 0"));
                            output = new NumericValue(0.0);
                        }
                        break;

                    case '^':
                        output = new NumericValue(Math.Pow(left.ToDouble(), right.ToDouble()));
                        break;

                    case 'v':
                        double r = right.ToDouble();
                        if (r == 0.0d)
                        {
                            Arithmetic.RaiseEventError(new OverflowException("valeur racine 1 / 0"));
                            output = new NumericValue(1.0d);
                        }
                        else if (r % 2 == 0)
                        {
                            if (left.ToDouble() > 0)
                            {
                                output = new NumericValue(Math.Pow(left.ToDouble(), 1 / r));
                            }
                            else if (left.ToDouble() == 0)
                            {
                                output = new NumericValue(0.0d);
                            }
                            else
                            {
                                output = new NumericValue(0.0d);
                                Arithmetic.RaiseEventError(new OverflowException("valeur sous la racine carrée négative"));
                            }
                        }
                        else
                        {
                            output = new NumericValue(Math.Pow(left.ToDouble(), 1 / r));
                        }
                        break;
                    }
                }
            }
            return(output);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Constructor
 /// given a unary operation with one number
 /// </summary>
 /// <param name="op">operator</param>
 /// <param name="n">number</param>
 protected UnaryOperation(char op, double n)
 {
     this[operatorName]     = op;
     this[innerOperandName] = new NumericValue(n);
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Default constructor
 /// Empty data
 /// </summary>
 protected UnaryOperation()
 {
     this[operatorName]     = 'p';
     this[innerOperandName] = new NumericValue(0.0d);
 }
Ejemplo n.º 7
0
        private Arithmetic calculate()
        {
            Arithmetic a = null, b = null;
            Arithmetic res = new NumericValue(0.0d);

            switch (this.stock[this.index])
            {
            case '=':
                ++this.index;
                a   = this.calculate();
                b   = this.calculate();
                res = new Equal(a, b);
                break;

            case '+':
                ++this.index;
                a   = this.calculate();
                b   = this.calculate();
                res = new Addition(a, b);
                break;

            case '-':
                ++this.index;
                a   = this.calculate();
                b   = this.calculate();
                res = new Soustraction(a, b);
                break;

            case '*':
                ++this.index;
                a   = this.calculate();
                b   = this.calculate();
                res = new Multiplication(a, b);
                break;

            case '/':
                ++this.index;
                a   = this.calculate();
                b   = this.calculate();
                res = new Division(a, b);
                break;

            case '^':
                ++this.index;
                a   = this.calculate();
                b   = this.calculate();
                res = new Power(a, b);
                break;

            case 'v':
                ++this.index;
                a   = this.calculate();
                b   = this.calculate();
                res = new Root(a, b);
                break;

            case 'p':
                ++this.index;
                string funp = this.words[(int)this.stock[this.index]];
                res = new Parenthese(Expression.Convert(funp));
                ++this.index;
                break;

            case 'g':
                ++this.index;
                string fung = this.words[(int)this.stock[this.index]];
                res = new Crochet(Expression.Convert(fung));
                ++this.index;
                break;

            case '@':
                ++this.index;
                res = new NumericValue((double)this.stock[this.index]);
                ++this.index;
                break;

            case 'ù':
                ++this.index;
                if (this.isNotLower(this.words[(int)this.stock[this.index]]))
                {
                    res = new UnknownTerm(this.words[(int)this.stock[this.index]]);
                }
                else
                {
                    res = new Coefficient(this.words[(int)this.stock[this.index]]);
                }
                ++this.index;
                break;

            case 'µ':
                ++this.index;
                string fun = this.words[(int)this.stock[this.index]];
                ++this.index;
                res = new Function(fun, this.calculate());
                break;

            case ',':
                string paramLeft = string.Empty, paramRight = string.Empty;
                if (this.stock[index] == ',')
                {
                    ++this.index;
                    a = this.calculate();
                }
                else
                {
                    b = this.calculate();
                }
                if (this.stock[index] == ',')
                {
                    ++this.index;
                    b = this.calculate();
                }
                else
                {
                    b = calculate();
                }
                List <IArithmetic> list = new List <IArithmetic>();
                if (a is Sequence)
                {
                    list.AddRange((a as Sequence).Items);
                }
                else
                {
                    list.Add(a);
                }
                if (b is Sequence)
                {
                    list.AddRange((b as Sequence).Items);
                }
                else
                {
                    list.Add(b);
                }
                res = new Sequence(list);
                break;
            }
            return(res);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// This function tries to obtain a numerical value
        /// but if not returns only equations
        /// </summary>
        /// <returns>a numerical value or an equation</returns>
        public override IArithmetic Compute()
        {
            // leaves values
            double leaves = 0.0d;
            // output not calculable
            Sum lefts = new Sum();

            lefts[listName] = new List <IArithmetic>();

            bool first = true;

            foreach (IArithmetic a in this.Items)
            {
                if (first)
                {
                    IArithmetic element = a.Compute();
                    if (element is NumericValue)
                    {
                        leaves = (element as NumericValue).Value;
                    }
                    first = false;
                }
                else
                {
                    IArithmetic element = a.Compute();
                    if (element is NumericValue)
                    {
                        leaves += (element as NumericValue).Value;
                    }
                    else
                    {
                        lefts[listName].Add(element);
                    }
                }
            }

            IArithmetic output;

            if (leaves != 0.0d)
            {
                if (lefts.Items.Count() > 0)
                {
                    lefts.Add(new NumericValue(leaves));
                    output = lefts;
                }
                else
                {
                    output = new NumericValue(leaves);
                }
            }
            else
            {
                if (lefts.Items.Count() > 0)
                {
                    output = lefts;
                }
                else
                {
                    output = new NumericValue(0.0d);
                }
            }
            return(output);
        }
Ejemplo n.º 9
0
 /// <summary>
 /// Computes a weight from a numeric value
 /// </summary>
 /// <param name="nv">numeric value object</param>
 /// <returns>weight of this object</returns>
 public static Weight ComputeWeight(NumericValue nv)
 {
     return(new Weight(ConstantValueType, nv.Value, nv));
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Constructor for a simple term
 /// </summary>
 /// <param name="constant">store a constant value</param>
 /// <param name="letter">store a coefficient</param>
 /// <param name="unknown">store an unknown term</param>
 public Term(double constant, string letter, string unknown)
 {
     this[constantName] = new NumericValue(constant);
     this[coefName]     = new Coefficient(letter, 0.0d);
     this[unknownName]  = new UnknownTerm(unknown, new NumericValue(0.0d));
 }