Esempio n. 1
0
        // ifade ve operatör alır.
        public Node NodeOlustur(string expression, char operation)
        {
            bool flag       = false;
            int  i          = expression.Length - 1;
            int  parcounter = 0;
            bool rightsided = false;

            while (!flag)
            {
                if (expression[i] == '(')
                {
                    if (rightsided)
                    {
                        parcounter--;
                    }
                    else
                    {
                        parcounter++;
                    }
                }
                else if (expression[i] == ')')
                {
                    if (rightsided)
                    {
                        parcounter++;
                    }
                    else
                    {
                        parcounter--;
                    }
                }

                if (parcounter == 0 && expression[i] == operation)
                {
                    // sağ ve sol childlar oluşur.
                    OPNode OPnod = new OPNode(operation);
                    OPnod.L = NodeOlustur(expression.Substring(0, i));
                    OPnod.R = NodeOlustur(expression.Substring(i + 1));
                    return(OPnod);
                }

                if (rightsided)
                {
                    if (i == expression.Length - 1)
                    {
                        flag = true; //ifadenin sağında işlem biter
                    }
                    i++;             //değilse ifadenin sağ tarafını okur.
                }
                else
                {
                    if (i == 0)
                    {
                        flag = true; //ifadenin solundaysak okuma döngüsü biter
                    }
                    i--;             //aksi durumda ifadenin diğer tarafı okunur
                }
            }
            return(null);
        }
Esempio n. 2
0
        public int Hesapla(Node NodeArg)
        {
            ValNode ConstaNode = NodeArg as ValNode;

            if (ConstaNode != null)
            {
                return(ConstaNode.Value);
            }

            //sağ ve sol node ları alarak işlem gerçekleştirir
            OPNode OperaNode = NodeArg as OPNode;

            if (OperaNode != null)
            {
                switch (OperaNode.Operand) //operatöre göre yapılacak işlem.
                {
                case '+':
                    return(Hesapla(OperaNode.L) + Hesapla(OperaNode.R));

                case '-':
                    return(Hesapla(OperaNode.L) - Hesapla(OperaNode.R));

                case '*':
                    return(Hesapla(OperaNode.L) * Hesapla(OperaNode.R));

                case '/':
                    if (Hesapla(OperaNode.R) == 0)
                    {
                        Console.WriteLine("Sıfıra a bölünmez.");
                        return(0);
                    }
                    return(Hesapla(OperaNode.L) / Hesapla(OperaNode.R));

                case '%':
                    return(Hesapla(OperaNode.L) % Hesapla(OperaNode.R));
                }
            }
            return(0);
        }