public void RaisedToNumberThenAddTest()
    {
        var actual = AddOp.Create(PowerOp.Create(A, One), Two);

        Assert.AreEqual(ExprParser.ParseOrThrow("a^1+2"), actual);
        Assert.AreEqual(A.Raise(One) + Two, actual.Simplify());
    }
    public void ExprTest()
    {
        const string input  = "12 * 3 + foo(-3, x)() * (2 + 1)";
        var          actual = AddOp.Create(
            MultiplyOp.Create(
                Literal.Create(12),
                Literal.Create(3)
                ),
            MultiplyOp.Create(
                Call.Create(
                    Call.Create(
                        new Identifier("foo"),
                        NegativeOp.Create(Literal.Create(3)),
                        new Identifier("x")
                        )
                    ),
                AddOp.Create(
                    new Two(),
                    new One()
                    )
                )
            ).Simplify();
        var expected = ExprParser.ParseOrThrow(input);

        Assert.AreEqual(expected.Simplify(), actual);
    }
    static void Main()
    {
        int expressionsLength = 2;
        int operatorsLength   = 1;

        var expressions = new var[expressionsLength];

        expressions[0] = new IntegerValue(3);
        expressions[1] = new DoubleValue(4.3);

        var operators = new var[operatorsLength];

        operators[0] = new AddOp();

        int i = 0;

        for (; i < 2; i = i + 1)
        {
            int j = 0;
            while (j < operatorsLength)
            {
                int k = 0;
                for (; k < 2; k = k + 1)
                {
                    var op1    = expressions[i];
                    var op     = operators[j];
                    var op2    = expressions[k];
                    var result = Evaluate(op1, op, op2);
                    Console.WriteLine(op1.ToString() + " " + op.ToString() + " " + op2.ToString() + " = " + result.ToString());
                }
                j = j + 1;
            }
        }
    }
    public void BracketsTest()
    {
        var actual   = ExprParser.ParseOrThrow("1+[2+y]/{x*(y+4)}");
        var expected = new One() + (AddOp.Create(new Two(), new Identifier("y")) / MultiplyOp.Create(new Identifier("x"), AddOp.Create(4, new Identifier("y"))));

        Assert.AreEqual(expected, actual);
    }
Exemplo n.º 5
0
        private Node ArithExprP(Node term)
        {
            string first  = "+ - or";
            string follow = "eq neq lt gt leq geq ] ) ; ,";

            this.SkipErrors(first, follow);

            var    lookaheadToken = this.TokenStream.Peek();
            string lookahead      = lookaheadToken.AToCCFormat();

            if (first.HasToken(lookahead))
            {
                this.ApplyDerivation("arithExprP -> addOp term arithExprP");

                var arithExpr = new AddOp(lookaheadToken.SourceLocation);

                string op           = AddOp();
                Node   nextTerm     = Term();
                Node   trailingExpr = ArithExprP(nextTerm);

                arithExpr.LHS      = term;
                arithExpr.Operator = op;
                arithExpr.RHS      = trailingExpr;

                return(arithExpr);
            }

            if (follow.HasToken(lookahead))
            {
                this.ApplyDerivation("arithExprP -> EPSILON");
                return(term);
            }

            return(null);
        }
Exemplo n.º 6
0
        public static void Test()
        {
            // Initialize the math solving engine.
            MathSolver.Init();

            EvalData evalData = ConstructEvalData();

            // Object deriving from ExComp representing a algebraic variable.
            AlgebraComp x = new AlgebraComp("x");

            ExComp complexExpression = ConstructEx("3x^2 - 3", evalData);

            ExComp combined = AddOp.StaticCombine(x, complexExpression);

            // Square the expression.
            ExComp squared = PowOp.RaiseToPower(complexExpression, new ExNumber(2.0), ref evalData, false);

            // String containing the evaluated result.
            string result = squared.ToAlgTerm().FinalToDispStr();

            ExComp left  = ConstructEx(¨ln(x - 2) ¨, evalData);
            ExComp right = ConstructEx(¨3¨, evalData);

            // The object containing the functionality used to solve algebraic equations.
            AlgebraSolver agSolver = new AlgebraSolver();

            // Any additional information regarding the result will be stored in the evalData object.
            ExComp solveResult = agSolver.SolveEq(left, right, new AlgebraVar(¨x¨), evalData);
        }
    public void QuantityTwoMinus_a_raisedTo1Plus3Test()
    {
        const string input    = "(2-a)^1+3";
        var          actual   = AddOp.Create(PowerOp.Create(SubtractOp.Create(new Two(), new Identifier("a")), new One()), Literal.Create(3)).Simplify();
        var          expected = ExprParser.ParseOrThrow(input).Simplify();

        Assert.AreEqual(expected, actual);
    }
    public void Test1()
    {
        const string input    = "-(2-a)^1+3";
        var          actual   = AddOp.Create(NegativeOp.Create(PowerOp.Create(SubtractOp.Create(new Two(), new Identifier("a")), new One())), Literal.Create(3)).Simplify();
        var          expected = ExprParser.ParseOrThrow(input);

        Assert.AreEqual(expected.Simplify(), actual);
    }
Exemplo n.º 9
0
        public override RegisterLabelContext Visit(AddOp addOp)
        {
            var c1           = Visit(addOp.Lhs);
            var c2           = Visit(addOp.Rhs);
            var nextRegister = _state.NewRegister;

            switch (addOp.Add, c1.Type, c2.Type)
            {
    public void AddToLiteralTest()
    {
        var actual1 = AddOp.Create(new Identifier("x"), 1);
        var actual2 = AddOp.Create(1, new Identifier("x"));

        Assert.AreEqual(X + One, actual1);
        Assert.AreEqual(One + X, actual2);
    }
Exemplo n.º 11
0
 public BasicNumericOperation(AddOp add, RemoveOp remove)
 {
     if (add == null || remove == null)
     {
         throw new ArgumentNullException();
     }
     this.addImpl = add;
     this.remImpl = remove;
 }
Exemplo n.º 12
0
 public static Expr Create(Expr left, Expr right) => (left, right) switch
 {
     (Zero _, _) => - right,
     (_, Zero _) => left,
     (_, NegativeOp {
         Expr : Literal l
     }) => AddOp.Create(left, l),
     _ => new SubtractOp(left, right)
 };
    public void XRaiseToXTimesXRaiseTo2()
    {
        var x        = new Identifier("x");
        var xToX     = x.Raise(x);
        var xTo2     = x.Raise(new Two());
        var expected = x.Raise(AddOp.Create(2, x));

        Assert.AreEqual(xToX * xTo2, expected);
    }
    public void AddTest1()
    {
        var d = ExprParser.ParseOrThrow("2*x");
        var e = ExprParser.ParseOrThrow("1*x");
        var f = ExprParser.ParseOrThrow("2");
        var h = ExprParser.ParseOrThrow("2");

        Assert.AreEqual(d + e + f + h, AddOp.Create(MultiplyOp.Create(3, new Identifier("x")), 4));
    }
    public void SubtractToNegativeLiteralTest()
    {
        var num     = new One();
        var actual1 = AddOp.Create(new Identifier("x"), 1);
        var actual2 = AddOp.Create(1, new Identifier("x"));

        Assert.AreEqual(X - NegativeOp.Create(num), actual1);
        Assert.AreEqual(num - NegativeOp.Create(X), actual2);
    }
Exemplo n.º 16
0
        public async Task <IActionResult> AddOperation(AddOp addOp)
        {
            db.Operations.Add(new Operation {
                Name = addOp.Name, NameAct = addOp.NameAct, Sum = addOp.Sum, Coment = addOp.Coment, PlanId = addOp.PlId
            });
            await db.SaveChangesAsync();

            return(RedirectToAction("Operation"));
        }
    public void DecimalTest()
    {
        var onePthree = ExprParser.ParseOrThrow("1.3");

        Assert.AreEqual(onePthree, DivideOp.Create(13, 10));
        var actual = AddOp.Create(PowerOp.Create(A, onePthree), Two);

        Assert.AreEqual(ExprParser.ParseOrThrow("a^1.3+2"), actual);
        Assert.AreEqual(A.Raise(onePthree) + Two, actual.Simplify());
    }
    public void Test3()
    {
        const string input    = "-a/b^1+3-a/b^1";
        var          term1    = DivideOp.Create(NegativeOp.Create(new Identifier("a")), PowerOp.Create(new Identifier("b"), new One()));
        var          term2    = Literal.Create(3);
        var          term3    = DivideOp.Create(new Identifier("a"), PowerOp.Create(new Identifier("b"), new One()));
        var          actual   = SubtractOp.Create(AddOp.Create(term1, term2), term3).Simplify();
        var          expected = ExprParser.ParseOrThrow(input);

        Assert.AreEqual(expected.Simplify(), actual);
    }
    public void NegativeBeforeAddTest()
    {
        const string input  = "-12 + 3";
        var          actual = AddOp.Create(
            NegativeOp.Create(Literal.Create(12)),
            Literal.Create(3)
            ).Simplify();
        var expected = ExprParser.ParseOrThrow(input);

        Assert.AreEqual(expected.Simplify(), actual);
    }
    public void Test4()
    {
        const string input    = "1-2/3+1-3+3*2";
        var          a        = new One();
        var          b        = DivideOp.Create(new Two(), Literal.Create(3));
        var          c        = new One();
        var          d        = Literal.Create(3);
        var          e        = MultiplyOp.Create(Literal.Create(3), new Two());
        var          actual   = AddOp.Create(SubtractOp.Create(AddOp.Create(SubtractOp.Create(a, b), c), d), e).Simplify();
        var          expected = ExprParser.ParseOrThrow(input);

        Assert.AreEqual(expected.Simplify(), actual);
    }
Exemplo n.º 21
0
 public IActionResult AddOperation(int id)
 {
     if (id == 0)
     {
         return(RedirectToAction("Plan"));
     }
     else
     {
         AddOp addOp = new AddOp();
         addOp.id = id;
         return(View());
     }
 }
 public override Expr Visit(AddOp addOp)
 {
     return((addOp.Add, Visit(addOp.Lhs), Visit(addOp.Rhs)) switch
     {
         (Add.Plus, Int i1, Int i2) => new Int {
             Value = i1.Value + i2.Value
         },
         (Add.Minus, Int i1, Int i2) => new Int {
             Value = i1.Value - i2.Value
         },
         (Add.Plus, Str s1, Str s2) => new Str {
             Value = s1.Value + s2.Value
         },
         (_, Expr e1, Expr e2) => new AddOp {
             Add = addOp.Add, Lhs = e1, Rhs = e2
         }
     });
Exemplo n.º 23
0
 public override void Visit(AddOp addOp)
 {
     if (addOp.RHS is Node rhs)
     {
         if (addOp.LHS is Node lhs)
         {
             if (lhs.SemanticalType != rhs.SemanticalType)
             {
                 ErrorManager.Add($"Inconsistent types: Cannot perform {lhs.SemanticalType} {addOp.Operator} {rhs.SemanticalType}", addOp.Location);
             }
         }
         addOp.SemanticalType = rhs.SemanticalType;
     }
     else
     {
         ErrorManager.Add("There is no RHS for this add operation.", addOp.Location);
     }
 }
    public void SubtractThenMultTest()
    {
        var input  = "(d-12) * 3";
        var actual = MultiplyOp.Create(
            SubtractOp.Create(new Identifier("d"), Literal.Create(12)),
            Literal.Create(3)
            ).Simplify();
        var expected = ExprParser.ParseOrThrow(input);

        Assert.AreEqual(expected.ToString(), "3×(d-12)");
        Assert.AreEqual(expected.Simplify(), actual);
        input  = "(d+12) * 3";
        actual = MultiplyOp.Create(
            AddOp.Create(new Identifier("d"), Literal.Create(12)),
            Literal.Create(3)
            ).Simplify();
        expected = ExprParser.ParseOrThrow(input).Simplify();
        Assert.AreEqual(expected, actual);
    }
Exemplo n.º 25
0
        /// <summary>
        /// 线程同步,“阻塞”调用,两个线程工作
        /// </summary>
        static void Demo04()
        {
            Console.WriteLine("******* 线程同步,“阻塞”调用,两个线程工作 *********");
            Console.WriteLine("Main() invokee on thread {0}.", Thread.CurrentThread.ManagedThreadId);
            //将委托实例指向Add()方法
            AddOp        pAddOp = new AddOp(Add);
            IAsyncResult iftAR  = pAddOp.BeginInvoke(10, 10, null, null);

            //判断委托线程是否执行完任务,
            //没有完成的话,主线程就做其他的事
            while (!iftAR.IsCompleted)
            {
                Console.WriteLine("Main()方法工作中.......线程ID是:{0}", Thread.CurrentThread.ManagedThreadId);
                Thread.Sleep(1000);
            }
            //获得返回值
            int answer = pAddOp.EndInvoke(iftAR);

            Console.WriteLine("10 + 10 = {0}.", answer);
            Console.ReadLine();
        }
Exemplo n.º 26
0
        /// <summary>
        /// 线程异步 委托异步线程 两个线程“同时”工作
        /// </summary>
        static void Demo03()
        {
            Console.WriteLine("******* 委托异步线程 两个线程“同时”工作 *********");
            //显示主线程的唯一标示
            Console.WriteLine("调用Main()的主线程的线程ID是:{0}.", Thread.CurrentThread.ManagedThreadId);
            //将委托实例指向Add()方法
            AddOp pAddOp = new AddOp(Add);

            //开始委托次线程调用。委托BeginInvoke()方法返回的类型是IAsyncResult,
            //包含这委托指向方法结束返回的值,同时也是EndInvoke()方法参数
            IAsyncResult iftAR = pAddOp.BeginInvoke(10, 10, null, null);
            //int i = 0;
            //while (!iftAR.IsCompleted)
            //{
            //    Console.WriteLine("Main()方法中执行其他任务........线程ID是:{0},执行第{1}次", Thread.CurrentThread.ManagedThreadId, i.ToString());
            //    i++;
            //}

            int sum = pAddOp.EndInvoke(iftAR);

            Console.WriteLine("10 + 10 = {0}.", sum);
            Console.ReadLine();
        }
Exemplo n.º 27
0
 public override void Visit(AddOp addOp)
 {
     this.AddToStack(addOp);
 }
 public override void Visit(AddOp addOp)
 {
     addOp.NodeMemorySize = Sizes[addOp.SemanticalType];
 }
 public void AddToIdentifierDifferent()
 {
     Assert.AreEqual(i1 + i3, AddOp.Create(new Identifier("x"), new Identifier("y")));
     Assert.AreEqual(i3 + i1, AddOp.Create(new Identifier("y"), new Identifier("x")));
 }
Exemplo n.º 30
0
 public virtual Result Visit(AddOp addOp)
 {
     return(default(Result));
 }