コード例 #1
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());
    }
コード例 #2
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);
        }
コード例 #3
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();
        }