/// <summary>
 /// 添加函数
 /// </summary>
 /// <param name="sender"></param>
 /// <param name="e"></param>
 private void button1_Click(object sender, EventArgs e)
 {
     using (frmAddFunction frmaddf = new frmAddFunction())
     {
         frmaddf.Function = textBox1.Text;
         if (frmaddf.ShowDialog() == System.Windows.Forms.DialogResult.OK)
         {
             textBox1.Text = frmaddf.Function;
             //一元
             if (textBox1.Text.ToLower().Contains('x') && !textBox1.Text.ToLower().Contains('y') && !textBox1.Text.ToLower().Contains('z'))
             {
                 UnaryFunction func = (new SyntaxManager().ParseUnaryFunction(textBox1.Text));
                 unaryFunctionDrawingBoard1.Function = func;
                 tabControl1.SelectedIndex           = 0;
             }
             //二元
             else if (textBox1.Text.ToLower().Contains('x') && textBox1.Text.ToLower().Contains('y') && !textBox1.Text.ToLower().Contains('z'))
             {
                 BinaryFunction func = (new SyntaxManager().ParseBinaryFunction(textBox1.Text));
                 binaryFunctionDrawingBoard1.BinaryFunction = func;
                 tabControl1.SelectedIndex = 1;
             }
             //三元
             else
             {
                 MultiFunction func = (new SyntaxManager().ParseMultiFunction(textBox1.Text));
                 MessageBox.Show("三元函数图像无法绘制!");
             }
         }
     }
 }
Beispiel #2
0
        public void EvaluateMultiFunctionWithTwoFunctions()
        {
            Function func1 = this.MakeFunction("f(0) -> 1.");
            Function func2 = this.MakeFunction("f(1) -> 2.");
            MultiFunction mfunc = new MultiFunction(new Function[] { func1, func2 });

            Assert.AreEqual(2, mfunc.Apply(null, new object[] { 1 }));
        }
Beispiel #3
0
        public void EvaluateMultiFunctionWithTwoFunctions()
        {
            Function      func1 = this.MakeFunction("f(0) -> 1.");
            Function      func2 = this.MakeFunction("f(1) -> 2.");
            MultiFunction mfunc = new MultiFunction(new Function[] { func1, func2 });

            Assert.AreEqual(2, mfunc.Apply(null, new object[] { 1 }));
        }
Beispiel #4
0
        public void EvaluateMultiFunctionWithOneFunction()
        {
            Function func = this.MakeFunction("f(0) -> 1.");
            MultiFunction mfunc = new MultiFunction(new Function[] { func });

            Assert.IsNotNull(mfunc.Functions);
            Assert.AreEqual(1, mfunc.Functions.Count);

            Assert.AreEqual(1, mfunc.Apply(null, new object[] { 0 }));
        }
Beispiel #5
0
        public void EvaluateMultiFunctionWithOneFunction()
        {
            Function      func  = this.MakeFunction("f(0) -> 1.");
            MultiFunction mfunc = new MultiFunction(new Function[] { func });

            Assert.IsNotNull(mfunc.Functions);
            Assert.AreEqual(1, mfunc.Functions.Count);

            Assert.AreEqual(1, mfunc.Apply(null, new object[] { 0 }));
        }
Beispiel #6
0
        public void RaiseIfNoClauseToMatch()
        {
            Function func1 = this.MakeFunction("f(0) -> 1.");
            Function func2 = this.MakeFunction("f(1) -> 2.");
            MultiFunction mfunc = new MultiFunction(new Function[] { func1, func2 });

            try
            {
                mfunc.Apply(null, new object[] { 2 });
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.AreEqual("no function clause to match", ex.Message);
            }
        }
Beispiel #7
0
        public object Evaluate(Context context, bool withvars = false)
        {
            int arity = this.expressions[0].ParameterExpressions.Count;

            if (!this.expressions.Skip(1).All(f => f.ParameterExpressions.Count == arity))
                throw new Exception("head mismatch");

            IList<Function> functions = new List<Function>();

            foreach (var form in this.Expressions)
                functions.Add((Function)form.Evaluate(context, true));

            var func = new MultiFunction(functions);

            return func;
        }
Beispiel #8
0
        public void RaiseIfNoClauseToMatch()
        {
            Function      func1 = this.MakeFunction("f(0) -> 1.");
            Function      func2 = this.MakeFunction("f(1) -> 2.");
            MultiFunction mfunc = new MultiFunction(new Function[] { func1, func2 });

            try
            {
                mfunc.Apply(null, new object[] { 2 });
                Assert.Fail();
            }
            catch (Exception ex)
            {
                Assert.AreEqual("no function clause to match", ex.Message);
            }
        }
Beispiel #9
0
        public object Evaluate(Context context)
        {
            string name = this.forms[0].Name;
            int arity = this.forms[0].ParameterExpressions.Count;

            if (!this.forms.Skip(1).All(f => f.Name == name && f.ParameterExpressions.Count == arity))
                throw new Exception("head mismatch");

            IList<Function> functions = new List<Function>();

            foreach (var form in this.Forms)
                functions.Add((Function)form.Evaluate(context));

            var func = new MultiFunction(functions);

            context.SetValue(string.Format("{0}/{1}", name, arity), func);

            return func;
        }
Beispiel #10
0
        public object Evaluate(Context context, bool withvars = false)
        {
            int arity = this.expressions[0].ParameterExpressions.Count;

            if (!this.expressions.Skip(1).All(f => f.ParameterExpressions.Count == arity))
            {
                throw new Exception("head mismatch");
            }

            IList <Function> functions = new List <Function>();

            foreach (var form in this.Expressions)
            {
                functions.Add((Function)form.Evaluate(context, true));
            }

            var func = new MultiFunction(functions);

            return(func);
        }
Beispiel #11
0
        public object Evaluate(Context context)
        {
            string name  = this.forms[0].Name;
            int    arity = this.forms[0].ParameterExpressions.Count;

            if (!this.forms.Skip(1).All(f => f.Name == name && f.ParameterExpressions.Count == arity))
            {
                throw new Exception("head mismatch");
            }

            IList <Function> functions = new List <Function>();

            foreach (var form in this.Forms)
            {
                functions.Add((Function)form.Evaluate(context));
            }

            var func = new MultiFunction(functions);

            context.SetValue(string.Format("{0}/{1}", name, arity), func);

            return(func);
        }