예제 #1
0
        public void Visit(SubExpr subExpr, object[] args)
        {
            subExpr.FirstOp.Accept(this);
            subExpr.SecondOp.Accept(this);

            RightValue v1 = readRightValue(subExpr.FirstOp);
            RightValue v2 = readRightValue(subExpr.SecondOp);


            DataType resultType = readAlgoOperand(v1, v2, subExpr.Location);

            if (resultType == DataType.Int)
            {
                IntConst result = new IntConst();
                result.Value       = Convert.ToInt32(v1.GetValueObject()) - Convert.ToInt32(v2.GetValueObject());
                subExpr.RightValue = result;
                //subExpr.DataType = DataType.Int;
            }
            else if (resultType == DataType.Float)
            {
                FloatConst result = new FloatConst();
                result.Value       = Convert.ToDouble(v1.GetValueObject()) - Convert.ToDouble(v2.GetValueObject());
                subExpr.RightValue = result;
                //subExpr.DataType = DataType.Float;
            }
            else
            {
                throw new Exception();
            }
        }
        public static void Run()
        {
            var expression = new SubExpr <int>(new AddExpr <int>(new ValExpr <int>(2), new ValExpr <int>(2)), new ValExpr <int>(2));

            var result = expression.Eval(algebra: (
                                             add: (x, y) => x + y,
                                             sub: (x, y) => x + y,
                                             val: v => v)
                                         );
        }
예제 #3
0
        static void Main(string[] args)
        {
            Console.WriteLine("1_______________________________________________________________________________");
            // (3*x^2 + 7) / (x + 5)   82 / 10 = 8.2
            Expression exp = new DivExpr(
                new AddExpr(
                    MonomialExpr.createObject(3, 2),
                    ConstExpr.createObject(7)),
                new AddExpr(
                    MonomialExpr.createObject(1, 1),
                    ConstExpr.createObject(5)));

            Console.WriteLine("f(x)=" + exp.ToString());
            Console.WriteLine("f(5)=" + exp.Evaluate(5));
            Console.WriteLine("f'(x)=" + exp.Derive().ToString());

            Console.WriteLine("2_______________________________________________________________________________");
            // (0 + 10) - 0 * (x^1 + 0)
            Expression exp5 = new AddExpr(MonomialExpr.createObject(0, 2),
                                          ConstExpr.createObject(10));

            Expression exp6 = new SubExpr(exp5,
                                          new MulExpr(ConstExpr.createObject(0),
                                                      new AddExpr(MonomialExpr.createObject(1, 1),
                                                                  ConstExpr.createObject(0))));

            Console.WriteLine("f(x)=" + exp6.ToString());
            Console.WriteLine("f(2)=" + exp6.Evaluate(2));
            Console.WriteLine("f'(x)=" + exp6.Derive().ToString());


            Console.WriteLine("3_______________________________________________________________________________");
            //(((x^2 + 2) / (x + 1)) + 10) / (x^2 + 5)
            Expression exp3 = new DivExpr(
                new AddExpr(
                    MonomialExpr.createObject(1, 2),
                    ConstExpr.createObject(2)),
                new AddExpr(
                    MonomialExpr.createObject(1, 1),
                    ConstExpr.createObject(1)));

            Expression exp4 = new DivExpr(
                new AddExpr(
                    exp3,
                    ConstExpr.createObject(10)),
                new AddExpr(
                    MonomialExpr.createObject(1, 2),
                    ConstExpr.createObject(5)));

            Console.WriteLine("f(x)=" + exp4.ToString());
            Console.WriteLine("f(1)=" + exp4.Evaluate(1));
            Console.WriteLine("f'(x)=" + exp4.Derive().ToString());
        }
예제 #4
0
        private Expr parseAddExpr()
        {
            Expr expr = parseMulExpr();

            while (true)
            {
                int token = parseToken();

                switch (token)
                {
                case '+':
                    expr = new AddExpr(expr, parseMulExpr());
                    break;

                case '-':
                    expr = new SubExpr(expr, parseMulExpr());
                    break;

                default:
                    _peekToken = token;
                    return(expr);
                }
            }
        }
예제 #5
0
 /// <summary>
 /// Validate a subtraction expression.
 /// </summary>
 /// <param name="subex">
 ///            is the expression. </param>
 /// <returns> null. </returns>
 public virtual object visit(SubExpr subex)
 {
     printBinExpr("SUB", subex);
     return(null);
 }
    //--------------------------------------------------------------------
    #region 内部方法
    ExprBase Parse(Queue <string> stringQueue)
    {
        string   word = null;
        ExprBase first = null, prev = null, last = null;

        while (stringQueue.Count > 0)
        {
            word = stringQueue.Dequeue();
            if (word == "(")
            {
                ExprBase subExpr = Parse(stringQueue);
                if (subExpr == null)
                {
                    return(null);
                }
                last = new SubExpr(subExpr);
            }
            else if (word[0] == '\'')
            {
                int endPos = word.LastIndexOf('\'');
                if (endPos > 0)
                {
                    last = new StrExpr(word.Substring(1, endPos - 1));
                }
                else
                {
                    return(null);
                }
            }
            else if (word[0] == '\"')
            {
                int endPos = word.LastIndexOf('\"');
                if (endPos > 0)
                {
                    last = new StrExpr(word.Substring(1, endPos - 1));
                }
                else
                {
                    return(null);
                }
            }
            else if (word == "true")
            {
                last = new ConstExpr(1);
            }
            else if (word == "false")
            {
                last = new ConstExpr(0);
            }
            else if (word[0] == '-' || Char.IsDigit(word[0]))
            {
                if (word.Contains("."))
                {
                    last = new ConstExpr(float.Parse(word));
                }
                else
                {
                    last = new ConstExpr(int.Parse(word));
                }
            }
            else
            {
                last = ParseFunc(word, stringQueue);
            }

            if (last == null)
            {
                return(null);
            }

            if (first == null)
            {
                first = prev = last;
            }
            else
            {
                prev.next = last;
                prev      = last;
            }

            // 条件连接关系
            if (stringQueue.Count > 0)
            {
                word = stringQueue.Dequeue();
                if (word == ")")
                {
                    break;
                }

                last.connector = ExprConnectors.Get(word);
                if (last.connector == null)
                {
                    return(null);
                }
            }

            last = null;
        }
        return(first);
    }
예제 #7
0
 /// <param name="ex">
 ///            is the sub expression. </param>
 /// <returns> a new function. </returns>
 public virtual object visit(SubExpr ex)
 {
     ex.left().accept(this);
     ex.right().accept(this);
     return(null);
 }
예제 #8
0
        /// <param name="e">
        ///            is the minus expression. </param>
        /// <returns> new sub expression </returns>
        public virtual object visit(MinusExpr e)
        {
            SubExpr se = new SubExpr(make_int_lit(0), e.arg());

            return(se.accept(this));
        }
예제 #9
0
 /// <param name="subex">
 ///            is the sub expression. </param>
 /// <returns> a new function. </returns>
 public virtual object visit(SubExpr subex)
 {
     return(make_ArithOp(subex, new QName("fs", "minus", OpFunctionLibrary.XPATH_OP_NS)));
 }