Beispiel #1
0
        private double CalculateBinary(TerminalExpression a, TerminalExpression b)
        {
            // double hasil = 0;
            if (op == '+')
            {
                AddExpression hasil = new AddExpression(a, b);
                //hasil = a + b;
                return(hasil.solve());
            }
            else if (op == '-')
            {
                SubstractExpression hasil = new SubstractExpression(a, b);
                //hasil = a - b;
                return(hasil.solve());
            }
            else if (op == '*')
            {
                MultiplyExpression hasil = new MultiplyExpression(a, b);
                //hasil = a * b;
                return(hasil.solve());
            }
            else if (op == '÷')
            {
                DivisionExpression hasil = new DivisionExpression(a, b);
                //hasil = a / b;
                return(hasil.solve());
            }

            return(0);
        }
Beispiel #2
0
 // Function to perform arithmetic operations.
 public Expression applyOp(Expression val1, Expression op, Expression val2)
 {
     if (((Operator)op).GetOp() == '+')
     {
         BinaryExpression Add = new AddExpression(val1, val2);
         return(Add);
     }
     else if (((Operator)op).GetOp() == '-')
     {
         BinaryExpression Substract = new SubstractExpression(val1, val2);
         return(Substract);
     }
     else if (((Operator)op).GetOp() == '*')
     {
         BinaryExpression Multiply = new MultiplyExpression(val1, val2);
         return(Multiply);
     }
     else if (((Operator)op).GetOp() == '/')
     {
         BinaryExpression Division = new DivisionExpression(val1, val2);
         return(Division);
     }
     else     /*if (((Operator)op).GetOp() == '^')*/
     {
         BinaryExpression Appointment = new AppointmentExpression(val1, val2);
         return(Appointment);
     }
 }
 public static BinaryExpression Create(Expression left, Expression right, char oper)
 {
     BinaryExpression expr = null;
     switch(oper)
     {
         case '+' : expr = new AdditiveExpression(left,right); break;
         case '-': expr = new SubstractExpression(left, right); break;
         case '*' : expr = new MultiplicativeExpression(left,right); break;
         case '/' : expr = new DivisionExpression(left,right); break;
     }
     expr.oper = oper;
     return expr;
 }
        public override void Run()
        {
            var context = new Context();
            int x = 5, y = 6, z = 7;

            context.SetValue("x", x);
            context.SetValue("y", y);
            context.SetValue("z", z);

            var expression = new SubstractExpression(
                new AddExpression(new NumberExpression("x"), new NumberExpression("y")), new NumberExpression("z"));

            var result = expression.Interpret(context);

            Console.WriteLine("x+y-z = " + result);
        }
Beispiel #5
0
        //Load Document
        private void MenuItem_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openfile = new OpenFileDialog();

            openfile.DefaultExt = ".xml";
            openfile.Filter     = "(.xml)|*xml";
            //openfile.ShowDialog();

            var browsefile = openfile.ShowDialog();

            if (browsefile == true)
            {
                try

                {
                    var path = openfile.FileName;


                    EDITOR_SCREEN.Children.Clear();
                    EDITOR_SCREEN.UpdateLayout();

                    var xmlStr = File.ReadAllText(@path);
                    var str    = XElement.Parse(xmlStr);

                    foreach (var item in str.Elements())
                    {
                        if (item.Name.LocalName.ToString() == "DATABASE")
                        {
                            ((App)App.Current).Server       = item.Attribute("INSTANCE").Value.ToString();
                            ((App)App.Current).DatabaseName = item.Attribute("DATABAZA").Value.ToString();
                            MyIntances.Items.Add(new ComboBoxItem {
                                Content = ((App)App.Current).DatabaseName
                            });
                        }
                        else

                        if (item.Name.LocalName.ToString() == XMLCREATOR.TEKST)
                        {
                            var txt = new Text();
                            var top = item.Attribute(XMLCREATOR.TOP).Value.ToString();
                            txt.Txt.Text = item.Attribute("VALUE").Value.ToString();
                            Canvas.SetTop(txt, double.Parse(top));
                            Canvas.SetLeft(txt, double.Parse(item.Attribute(XMLCREATOR.LEFT).Value.ToString()));
                            txt.Width  = double.Parse(item.Attribute(XMLCREATOR.WIDTH).Value);
                            txt.Height = double.Parse(item.Attribute(XMLCREATOR.HEIGHT).Value.ToString());
                            EDITOR_SCREEN.Children.Add(txt);
                        }

                        else if (item.Name.LocalName.ToString() == XMLCREATOR.FIELD)
                        {
                            var f = new Field();
                            Canvas.SetTop(f, double.Parse(item.Attribute(XMLCREATOR.TOP).Value.ToString()));
                            Canvas.SetLeft(f, double.Parse(item.Attribute(XMLCREATOR.LEFT).Value.ToString()));
                            f.DataBaseData    = new Tuple <string, string>(item.Elements("DB").FirstOrDefault().Attribute("TN").Value.ToString(), item.Elements("DB").FirstOrDefault().Attribute("CN").Value.ToString());
                            f.LblName.Content = f.DataBaseData.Item2;
                            f.Width           = double.Parse(item.Attribute(XMLCREATOR.WIDTH).Value);
                            f.Height          = double.Parse(item.Attribute(XMLCREATOR.HEIGHT).Value);
                            EDITOR_SCREEN.Children.Add(f);
                        }

                        else if (item.Name.LocalName.ToString() == XMLCREATOR.TABELE)
                        {
                            var t = new Tabele();
                            Canvas.SetTop(t, double.Parse(item.Attribute(XMLCREATOR.TOP).Value.ToString()));
                            Canvas.SetLeft(t, double.Parse(item.Attribute(XMLCREATOR.LEFT).Value.ToString()));
                            t.Width  = double.Parse(item.Attribute(XMLCREATOR.WIDTH).Value);
                            t.Height = double.Parse(item.Attribute(XMLCREATOR.HEIGHT).Value);

                            foreach (XElement colums in item.Element(XMLCREATOR.COLUMNS).Elements())
                            {
                                var kol = new Kolone();

                                t.Container.ColumnDefinitions.Add(new ColumnDefinition());
                                Grid.SetColumn(kol, t.Container.ColumnDefinitions.Count - 1);

                                if (colums.Elements("DB").FirstOrDefault() != null)
                                {
                                    kol.FieldColumn.DataBaseData    = new Tuple <string, string>(colums.Elements("DB").FirstOrDefault().Attribute("TN").Value.ToString(), colums.Elements("DB").FirstOrDefault().Attribute("CN").Value.ToString());
                                    kol.FieldColumn.LblName.Content = kol.FieldColumn.DataBaseData.Item2;
                                }
                                else
                                {
                                    var rows = colums.Element("FX").Elements().ToList();
                                    var e1   = new DataExpression();
                                    e1.ColumnName = new Tuple <string, string>(rows[0].Attribute("TN").Value, rows[0].Attribute("CN").Value);
                                    var OPERATOR = rows[1].Value;
                                    var e2       = new DataExpression();
                                    e2.ColumnName = new Tuple <string, string>(rows[2].Attribute("TN").Value, rows[2].Attribute("CN").Value);

                                    if (OPERATOR == "+")
                                    {
                                        var a = new AddExpression(e1, e2);
                                        kol.FieldColumn.Exp             = a;
                                        kol.FieldColumn.LblName.Content = a.getFunctionAsString();
                                    }
                                    else if (OPERATOR == "*")
                                    {
                                        var a = new MultipleExpression(e1, e2);
                                        kol.FieldColumn.Exp             = a;
                                        kol.FieldColumn.LblName.Content = a.getFunctionAsString();
                                    }
                                    else if (OPERATOR == "-")
                                    {
                                        var a = new SubstractExpression(e1, e2);
                                        kol.FieldColumn.Exp             = a;
                                        kol.FieldColumn.LblName.Content = a.getFunctionAsString();
                                    }
                                    else if (OPERATOR == "/")
                                    {
                                        var a = new MultipleExpression(e1, e2);
                                        kol.FieldColumn.Exp             = a;
                                        kol.FieldColumn.LblName.Content = a.getFunctionAsString();
                                    }
                                }

                                kol.ColHeader.Text = colums.Attribute(XMLCREATOR.COLUMN_HEADER).Value.ToString();
                                if (colums.Attribute("TOT") != null)
                                {
                                    if (colums.Attribute("TOT").Value.ToString() == "True")
                                    {
                                        kol.HasTotal = true;
                                    }
                                }
                                t.Container.Children.Add(kol);
                            }
                            EDITOR_SCREEN.Children.Add(t);
                        }

                        //schema DATA
                        else if (item.Name.LocalName == "SCHEMA")
                        {
                            SchemaCreator.CurrentChoise = new TableRelationShips();
                            SchemaCreator.CurrentChoise.Parent_Table = item.Attribute("PT").Value.ToString();
                            SchemaCreator.CurrentChoise.ParentKey    = item.Attribute("PK").Value.ToString();
                            SchemaCreator.CurrentChoise.Child_Table  = item.Attribute("CT").Value.ToString();
                            SchemaCreator.CurrentChoise.ChildKey     = item.Attribute("PK").Value.ToString();
                            SchemaCreator.CurrentChoise.MAIN_KEY.Key = item.Attribute("FILTER").Value.ToString();
                            try
                            {
                                ((App)App.Current).Server       = item.Attribute("INSTANCE").Value.ToString();
                                ((App)App.Current).DatabaseName = item.Attribute("DATABASE").Value.ToString();
                            }
                            catch
                            {
                            }
                            var nextlist = item.Element("NEXT").Elements().ToList();

                            foreach (var item2 in nextlist)
                            {
                                var pt  = item2.Attribute("PT").Value.ToString();
                                var pk  = item2.Attribute("PK").Value.ToString();
                                var ct  = item2.Attribute("CT").Value.ToString();
                                var cky = item2.Attribute("CK").Value.ToString();
                                SchemaCreator.CurrentChoise.NextRelationShips.Add(new TableRelationShips {
                                    Parent_Table = pt, ParentKey = pk, Child_Table = ct, ChildKey = cky
                                });
                            }

                            SchemaCreator.CurrentChoise.LoadColums();
                        }
                    }
                }

                catch (Exception xe)
                {
                    MessageBox.Show("Dokumenti nuk mund te hapet, mund te jete korruptuar ose formati nuk eshte i sakte !");
                }
            }
        }
Beispiel #6
0
        // Menghasilkan output dari list of string dari input
        public static dynamic solver(List <string> input)
        {
            dynamic        Ret   = 0;
            Boolean        isNow = false;
            Stack <double> NumS  = new Stack <double>();
            Stack <string> OpS   = new Stack <string>();

            // Memisahkan ke dalam stack
            for (int i = 0; i < input.Count; i++)
            {
                // Kalau operator masuk sini
                if (input[i].Equals("+") || input[i].Equals("-") || input[i].Equals("x") || input[i].Equals(":") || input[i].Equals("√"))
                {
                    if (!isNow && (input[i].Equals("x") || input[i].Equals(":") || input[i].Equals("-")))
                    {
                        isNow = true;
                        OpS.Push(input[i]);
                    }
                    else if (isNow && input[i].Equals("-"))
                    {
                        if (input[i - 1].Equals("+"))
                        {
                            dynamic Num1 = input[i + 1];
                            NegativeExpression <dynamic> E = new NegativeExpression <dynamic>(new TerminalExpression <dynamic>(Num1));
                            NumS.Push(E.solve());
                            i++;
                            isNow = false;
                        }
                        else if (input[i - 1].Equals("-"))
                        {
                            dynamic Num1 = input[i + 1];
                            NegativeExpression <dynamic> E = new NegativeExpression <dynamic>(new TerminalExpression <dynamic>(Num1));
                            NumS.Push(E.solve());
                            i++;
                            isNow = false;
                        }
                        else
                        {
                            dynamic Num1 = input[i + 1];
                            NegativeExpression <dynamic> E = new NegativeExpression <dynamic>(new TerminalExpression <dynamic>(Num1));

                            dynamic Num3 = E.solve();
                            dynamic Num4 = NumS.Pop();  //Convert.ToDouble(input[i-2]);

                            if (input[i - 1].Equals("x"))
                            {
                                MultiplyExpression <dynamic> E2 = new MultiplyExpression <dynamic>(new TerminalExpression <dynamic>(Num3), new TerminalExpression <dynamic>(Num4));
                                NumS.Push(E2.solve());
                                Ret   = E2.solve();
                                isNow = false;
                                i++;
                            }
                            else
                            {
                                DivideExpression <dynamic> E2 = new DivideExpression <dynamic>(new TerminalExpression <dynamic>(Num4), new TerminalExpression <dynamic>(Num3));
                                NumS.Push(E2.solve());
                                Ret   = E2.solve();
                                isNow = false;
                                i++;
                            }
                        }
                    }
                    else
                    {
                        OpS.Push(input[i]);
                    }
                }
                else // Kalau angka masuk sini
                {
                    if (isNow)
                    {
                        string Op = OpS.Pop(); // OMengambil Operatornya dan solve
                        if (Op.Equals("x"))
                        {
                            dynamic Num1 = NumS.Pop();
                            dynamic Num2 = Convert.ToDouble(input[i]);
                            MultiplyExpression <dynamic> E = new MultiplyExpression <dynamic>(new TerminalExpression <dynamic>(Num1), new TerminalExpression <dynamic>(Num2));

                            NumS.Push(E.solve());
                            Ret = E.solve();
                        }
                        else if (Op.Equals(":"))
                        {
                            dynamic Num1 = NumS.Pop();
                            dynamic Num2 = Convert.ToDouble(input[i]);
                            DivideExpression <dynamic> E = new DivideExpression <dynamic>(new TerminalExpression <dynamic>(Num1), new TerminalExpression <dynamic>(Num2));

                            NumS.Push(E.solve());
                            Ret = E.solve();
                        }
                        else if (Op.Equals("-"))
                        {
                            dynamic Num1 = input[i];
                            NegativeExpression <dynamic> E = new NegativeExpression <dynamic>(new TerminalExpression <dynamic>(Num1));
                            if (i != 1 && (!input[i - 2].Equals("+") && !input[i - 2].Equals("√")))
                            {
                                OpS.Push("+");
                            }
                            NumS.Push(E.solve());
                        }
                        isNow = false;
                    }
                    else
                    {
                        double P = Convert.ToDouble(input[i]);
                        NumS.Push(P);
                    }
                }
            }

            // Solve dari Stack
            int count = OpS.Count;

            for (int i = 0; i < count; i++)
            {
                string Op = OpS.Pop();
                if (Op.Equals("+"))
                {
                    dynamic Num1 = NumS.Pop();
                    dynamic Num2 = NumS.Pop();
                    AddExpression <dynamic> E = new AddExpression <dynamic>(new TerminalExpression <dynamic>(Num1), new TerminalExpression <dynamic>(Num2));

                    NumS.Push(E.solve());
                    Ret = E.solve();
                }
                else if (Op.Equals("-"))
                {
                    dynamic Num1 = NumS.Pop();
                    dynamic Num2 = NumS.Pop();
                    SubstractExpression <dynamic> E = new SubstractExpression <dynamic>(new TerminalExpression <dynamic>(Num2), new TerminalExpression <dynamic>(Num1));

                    NumS.Push(E.solve());
                    Ret = E.solve();
                }
                else if (Op.Equals("√"))
                {
                    dynamic Num1 = NumS.Pop();
                    RootExpression <dynamic> E = new RootExpression <dynamic>(new TerminalExpression <dynamic>(Num1));

                    double temp = Convert.ToDouble(E.solve());
                    NumS.Push(temp);
                    Ret = E.solve();
                }
            }
            return(Ret);
        }
Beispiel #7
0
    private double solvePostfixQueue()
    {
        /** DESKRIPSI **/
        /* Menyelesaikan ekspresi Postfix di queue menjadi nilai */

        /** KAMUS DATA **/
        Stack <TerminalExpression> operationStack;     // Stack untuk menyimpan nilai-nilai operasi
        Elemen <string>            queueTemp;
        TerminalExpression         term, term1, term2;
        Expression exp;

        /** ALGORITMA **/
        printQueue(this.expressionPostfixQueue);
        operationStack = new Stack <TerminalExpression>();
        while (this.expressionPostfixQueue.Count != 0)
        {
            queueTemp = this.expressionPostfixQueue.Dequeue();
            if (queueTemp.GetItem1() == "#")
            {
                term = new TerminalExpression(queueTemp.GetItem2());
                operationStack.Push(term);
                Console.WriteLine(queueTemp.GetItem2());
            }
            else
            {
                switch (queueTemp.GetItem1())
                {
                case "+":
                    term2 = operationStack.Pop();
                    term1 = operationStack.Pop();
                    exp   = new AddExpression(term1, term2);
                    term  = new TerminalExpression(exp.solve());
                    operationStack.Push(term);
                    break;

                case "-":
                    term2 = operationStack.Pop();
                    term1 = operationStack.Pop();
                    exp   = new SubstractExpression(term1, term2);
                    term  = new TerminalExpression(exp.solve());
                    operationStack.Push(term);
                    break;

                case "*":
                    term2 = operationStack.Pop();
                    term1 = operationStack.Pop();
                    exp   = new MultiplyExpression(term1, term2);
                    term  = new TerminalExpression(exp.solve());
                    operationStack.Push(term);
                    break;

                case "/":
                    term2 = operationStack.Pop();
                    term1 = operationStack.Pop();
                    exp   = new DivideExpression(term1, term2);
                    term  = new TerminalExpression(exp.solve());
                    operationStack.Push(term);
                    break;

                case "akar":
                    term1 = operationStack.Pop();
                    exp   = new RootExpression(term1);
                    term  = new TerminalExpression(exp.solve());
                    operationStack.Push(term);
                    break;
                }
            }
        }

        term = operationStack.Pop();
        return(term.solve());
    }
Beispiel #8
0
        static void Main(string[] args)
        {
            Calculator Calcu = new Calculator();

            /*
             * Testing Kalkulasi
             * 1) 2+3
             * 2) 2-3
             * 3) 10:2
             * 4) 10.5x2.3
             * 5) √99
             * 6) -5+-5
             * 7) 5x-10
             * 8) √10+10
             * 9) -10+5x-10--5
             * 10) -10+-5:10--5
             * 11) 20+-10+√100
             * 12) -30-100:10+-10+√1000
             *
             * Testing Exception
             * 1) 10:0
             * 2) √-10
             * 3) +5-3
             * 4) 5++2
             * 5) 12-98-
             * 6) 12-+32
             * 7) 10√
             * 8) √:10
             * 9) 10.12√2
             * 10) 10.2.3
             *
             */
            Calcu.Input = "-30-100:10+-10+√1000";
            Console.WriteLine("Input : " + Calcu.Input);
            Calcu.Input   = Calcu.Input.Replace(".", ",");
            Calcu.Lstring = Parse.makeList(Calcu.Input);

            //Testing parse
            Console.WriteLine("\nHasil Parse Input");
            foreach (var elmt in Calcu.Lstring)
            {
                Console.WriteLine(elmt);
            }

            try
            {
                bool valid = Calcu.Validate(Calcu.Lstring);
                if (Calcu.Lstring.Count != 1)
                {
                    Calcu.Hasil = Solving.solver(Calcu.Lstring).ToString();
                    Calcu.Hasil = Calcu.Hasil.Replace(",", ".");
                    Console.WriteLine("\nHasil : " + Calcu.Hasil);
                    Calcu.Ans = Calcu.Hasil;
                }
                else
                {
                    Calcu.Ans = Calcu.Input.Replace(",", ".");
                }
            }
            catch (InvalidExpression Error)
            {
                Console.WriteLine("Hasil : " + Error.Message);
            }


            // Testing Expression
            double Num1 = 5;
            double Num2 = 4;

            Console.WriteLine("\nTestingExpression");
            AddExpression <dynamic> E = new AddExpression <dynamic>(new TerminalExpression <dynamic>(Num1), new TerminalExpression <dynamic>(Num2));

            Console.WriteLine("Terminal Ekspression 1 : " + Num1);
            Console.WriteLine("Terminal Ekspression 2 : " + Num2);
            Console.WriteLine("Hasil AddExpression : " + E.solve());

            SubstractExpression <dynamic> E2 = new SubstractExpression <dynamic>(new TerminalExpression <dynamic>(Num1), new TerminalExpression <dynamic>(Num2));

            Console.WriteLine("\nTerminal Ekspression 1 : " + Num1);
            Console.WriteLine("Terminal Ekspression 2 : " + Num2);
            Console.WriteLine("Hasil SubstractExpression : " + E2.solve());

            MultiplyExpression <dynamic> E3 = new MultiplyExpression <dynamic>(new TerminalExpression <dynamic>(Num1), new TerminalExpression <dynamic>(Num2));

            Console.WriteLine("\nTerminal Ekspression 1 : " + Num1);
            Console.WriteLine("Terminal Ekspression 2 : " + Num2);
            Console.WriteLine("Hasil MultiplyExpression : " + E3.solve());

            DivideExpression <dynamic> E4 = new DivideExpression <dynamic>(new TerminalExpression <dynamic>(Num1), new TerminalExpression <dynamic>(Num2));

            Console.WriteLine("\nTerminal Ekspression 1 : " + Num1);
            Console.WriteLine("Terminal Ekspression 2 : " + Num2);
            Console.WriteLine("Hasil DivideExpression : " + E4.solve());

            NegativeExpression <dynamic> E5 = new NegativeExpression <dynamic>(new TerminalExpression <dynamic>(Num1));

            Console.WriteLine("\nTerminal Ekspression 1 : " + Num1);
            Console.WriteLine("Hasil NegativeExpression : " + E5.solve());

            RootExpression <dynamic> E6 = new RootExpression <dynamic>(new TerminalExpression <dynamic>(Num1));

            Console.WriteLine("\nTerminal Ekspression 1 : " + Num1);
            Console.WriteLine("Hasil RootExpression : " + E6.solve());



            Console.ReadLine();
        }