Exemplo n.º 1
0
        void ShowExpression(Notation notation)
        {
            Notation   ntn      = notation;
            Expression constExp = new ConstExp(double.Parse(ConstExpressionTextBox.Text));
            Expression monoExp  = GetMonoExpression(MonomialExpressionTextBox.Text);

            Expression binExp1 = GetMonoExpression(BinaryExpression1TextBox.Text);
            Expression binExp2 = GetMonoExpression(BinaryExpression2TextBox.Text);
            Expression binExp  = null;

            ListExpresstionTextBox.AppendText(constExp.toString(ntn) + "\n");
            ListExpresstionTextBox.AppendText(monoExp.toString(ntn) + "\n");

            string ope = OperatorTextBox.Text;

            switch (ope)
            {
            case "+":
                binExp = new AddExp(binExp1, binExp2);
                break;

            case "-":
                binExp = new MinusExp(binExp1, binExp2);
                break;

            case "*":
                binExp = new MulExp(binExp1, binExp2);
                break;

            case "/":
                binExp = new DivExp(binExp1, binExp2);
                break;
            }
            ListExpresstionTextBox.AppendText(binExp.toString(ntn) + "\n");
        }
Exemplo n.º 2
0
        static void Main(string[] args)
        {
            try
            {
                //IExpression exp = new DivExp(new ConstExp(0), MonomialExp.createInstance(3, 0));
                IExpression exp = new DivExp(new MulExp(new ConstExp(10), MonomialExp.createInstance(3, 0)), new AddExp(new MulExp(MonomialExp.createInstance(5, 2), new ConstExp(4)), MonomialExp.createInstance(3, 2)));
                //IExpression exp = new DivExp(MonomialExp.createInstance(3, 2), new ConstExp(-2));
                double eValue = exp.evaluate(3);
                Console.WriteLine("Value:");
                Console.WriteLine("The value of expression when x = 3 : {0}", eValue);
                // ---------------
                Console.WriteLine("\nNotations:");
                INotation infixNtn  = new InfixNotation();
                INotation prefixNtn = new PrefixNotation();
                INotation posfixNtn = new PosfixNotation();

                Console.WriteLine("Infix Notation: {0}", exp.toString(infixNtn));

                Console.WriteLine("Prefix Notation: {0}", exp.toString(prefixNtn));

                Console.WriteLine("Posfix Notation: {0}", exp.toString(posfixNtn));

                // ---------------
                Console.WriteLine("\nDerivative with notations:");
                Console.WriteLine("Infix Notation: {0}", exp.derive().toString(infixNtn));

                Console.WriteLine("Prefix Notation: {0}", exp.derive().toString(prefixNtn));

                Console.WriteLine("Posfix Notation: {0}", exp.derive().toString(posfixNtn));
            }
            catch (Exception e)
            {
                Console.WriteLine("Error : {0}", e.Message);
            }
        }
Exemplo n.º 3
0
        private void DeriveButton_Click(object sender, EventArgs e)
        {
            ListExpresstionTextBox.Clear();

            Notation   ntn      = new InfixNotation();
            Expression constExp = new ConstExp(double.Parse(ConstExpressionTextBox.Text));
            Expression monoExp  = GetMonoExpression(MonomialExpressionTextBox.Text);

            Expression binExp1 = GetMonoExpression(BinaryExpression1TextBox.Text);
            Expression binExp2 = GetMonoExpression(BinaryExpression2TextBox.Text);
            Expression binExp  = null;

            constExp = constExp.Derive();
            monoExp  = monoExp.Derive();

            ListExpresstionTextBox.AppendText(constExp.toString(ntn) + "\n");
            ListExpresstionTextBox.AppendText(monoExp.toString(ntn) + "\n");


            string ope = OperatorTextBox.Text;

            switch (ope)
            {
            case "+":
                binExp = new AddExp(binExp1, binExp2).Derive();
                break;

            case "-":
                binExp = new MinusExp(binExp1, binExp2).Derive();
                break;

            case "*":
                binExp = new MulExp(binExp1, binExp2).Derive();
                break;

            case "/":
                binExp = new DivExp(binExp1, binExp2).Derive();
                break;
            }
            ListExpresstionTextBox.AppendText(binExp.toString(ntn) + "\n");
        }