Ejemplo n.º 1
0
        private string Calc(string expr, int idx)
        {
            List <Atom> infix = Parsing.getAtoms(expr);

            List <Atom> postfix = new List <Atom>();

            Stack <Atom> Symbs = new Stack <Atom>();

            for (int i = 0; i < infix.Count; ++i)
            {
                if (infix[i].getType() == "OPER")
                {
                    if (Symbs.Count != 0)
                    {
                        if (infix[i].getValue() == "(")
                        {
                            Symbs.Push(infix[i]);
                        }
                        else
                        if (infix[i].getValue() == ")")
                        {
                            while (Symbs.Peek().getValue() != "(")
                            {
                                postfix.Add(Symbs.Peek());
                                Symbs.Pop();
                            }
                            Symbs.Pop();
                        }
                        else
                        {
                            while (Symbs.Count != 0 && prior[Parsing.getHash(infix[i].getValue())] <= prior[Parsing.getHash(Symbs.Peek().getValue())])
                            {
                                postfix.Add(Symbs.Peek());
                                Symbs.Pop();
                            }

                            Symbs.Push(infix[i]);
                        }
                    }
                    else
                    {
                        Symbs.Push(infix[i]);
                    }
                }
                else
                if (infix[i].getType() == "REF")
                {
                    refs.addEdge(idx, getItemNumber(infix[i].getValue()));
                    if (refs.isInCycle(idx))
                    {
                        var invalidCells = refs.availableFrom(idx);
                        for (int j = 0; j < invalidCells.Count; ++j)
                        {
                            Grid.Rows[invalidCells[j] / sz].Cells[invalidCells[j] % sz].Value = "ERROR";
                        }
                    }
                    postfix.Add(new NumAtom(getItemValue(infix[i].getValue())));
                }
                else
                {
                    postfix.Add(infix[i]);
                }
            }

            while (Symbs.Count != 0)
            {
                postfix.Add(Symbs.Peek());
                Symbs.Pop();
            }

            Stack <Atom> Nums = new Stack <Atom>();

            for (int i = 0; i < postfix.Count; ++i)
            {
                if (postfix[i].getType() == "OPER")
                {
                    Atom atom1 = Nums.Pop();
                    Atom atom2 = Nums.Pop();
                    Atom nAtom = new NumAtom(0);
                    if (postfix[i].getValue() == "+")
                    {
                        nAtom = new NumAtom(Parsing.StringToInt(atom1.getValue()) + Parsing.StringToInt(atom2.getValue()));
                    }
                    if (postfix[i].getValue() == "-")
                    {
                        nAtom = new NumAtom(Parsing.StringToInt(atom2.getValue()) - Parsing.StringToInt(atom1.getValue()));
                    }
                    if (postfix[i].getValue() == "*")
                    {
                        nAtom = new NumAtom(Parsing.StringToInt(atom1.getValue()) * Parsing.StringToInt(atom2.getValue()));
                    }
                    if (postfix[i].getValue() == "/")
                    {
                        nAtom = new NumAtom(Parsing.StringToInt(atom2.getValue()) / Parsing.StringToInt(atom1.getValue()));
                    }
                    if (postfix[i].getValue() == "div")
                    {
                        nAtom = new NumAtom(Parsing.StringToInt(atom2.getValue()) / Parsing.StringToInt(atom1.getValue()));
                    }
                    if (postfix[i].getValue() == "mod")
                    {
                        nAtom = new NumAtom(Parsing.StringToInt(atom2.getValue()) % Parsing.StringToInt(atom1.getValue()));
                    }
                    if (postfix[i].getValue() == "max")
                    {
                        nAtom = new NumAtom(Math.Max(Parsing.StringToInt(atom2.getValue()), Parsing.StringToInt(atom1.getValue())));
                    }
                    if (postfix[i].getValue() == "min")
                    {
                        nAtom = new NumAtom(Math.Min(Parsing.StringToInt(atom2.getValue()), Parsing.StringToInt(atom1.getValue())));
                    }
                    if (postfix[i].getValue() == "=")
                    {
                        if ((atom2.getValue() == atom1.getValue()))
                        {
                            nAtom = new NumAtom(1);
                        }
                        else
                        {
                            nAtom = new NumAtom(0);
                        }
                    }
                    if (postfix[i].getValue() == ">=")
                    {
                        if ((Parsing.StringToInt(atom2.getValue()) >= Parsing.StringToInt(atom1.getValue())))
                        {
                            nAtom = new NumAtom(1);
                        }
                        else
                        {
                            nAtom = new NumAtom(0);
                        }
                    }
                    if (postfix[i].getValue() == "<=")
                    {
                        if ((Parsing.StringToInt(atom2.getValue()) <= Parsing.StringToInt(atom1.getValue())))
                        {
                            nAtom = new NumAtom(1);
                        }
                        else
                        {
                            nAtom = new NumAtom(0);
                        }
                    }
                    if (postfix[i].getValue() == ">")
                    {
                        if ((Parsing.StringToInt(atom2.getValue()) > Parsing.StringToInt(atom1.getValue())))
                        {
                            nAtom = new NumAtom(1);
                        }
                        else
                        {
                            nAtom = new NumAtom(0);
                        }
                    }
                    if (postfix[i].getValue() == "<")
                    {
                        if ((Parsing.StringToInt(atom2.getValue()) < Parsing.StringToInt(atom1.getValue())))
                        {
                            nAtom = new NumAtom(1);
                        }
                        else
                        {
                            nAtom = new NumAtom(0);
                        }
                    }
                    if (postfix[i].getValue() == "<>")
                    {
                        if ((atom2.getValue() != atom1.getValue()))
                        {
                            nAtom = new NumAtom(1);
                        }
                        else
                        {
                            nAtom = new NumAtom(0);
                        }
                    }
                    Nums.Push(nAtom);
                }
                else
                {
                    Nums.Push(postfix[i]);
                }
            }
            if (Nums.Count == 0)
            {
                return("");
            }
            else
            {
                return(Nums.Peek().getValue().ToString());
            }
        }
Ejemplo n.º 2
0
        public static List <Atom> getAtoms(string expr)
        {
            List <Atom> atoms = new List <Atom>();

            if (expr == "")
            {
                return(atoms);
            }
            string oldexpr = expr;

            expr = "";
            for (int i = 0; i < oldexpr.Length; ++i)
            {
                if (oldexpr[i] == '-' && (i == 0 || oldexpr[i - 1] > '9' || oldexpr[i - 1] < '0'))
                {
                    expr += '0';
                }
                expr += oldexpr[i];
            }

            expr = expr.Replace(" ", "");
            Stack <string> oper = new Stack <string>();

            for (int i = 0; i < expr.Length - 2; ++i)
            {
                if (expr.Substring(i, 3) == "min" || expr.Substring(i, 3) == "max")
                {
                    oper.Push(expr.Substring(i, 3));
                    expr = expr.Remove(i, 3);
                }

                if (expr[i] == ',')
                {
                    expr = expr.Remove(i, 1).Insert(i, oper.Pop());
                }
            }

            int    st  = 0;
            string cur = expr[0].ToString();

            // 0 - shortoper, 1 - longoper, 2 - num, 3 - ref
            if (expr[0] >= '0' && expr[0] <= '9')
            {
                st = 2;
            }
            if (expr[0] >= 'a' && expr[0] <= 'z' || expr[0] == '<' || expr[0] == '>' || expr[0] == '=')
            {
                st = 1;
            }
            if (expr[0] >= 'A' && expr[0] <= 'Z')
            {
                st = 3;
            }

            for (int i = 1; i < expr.Length; ++i)
            {
                if (expr[i] >= '0' && expr[i] <= '9')
                {
                    if (st == 0 || st == 1)
                    {
                        Atom nAtom = new OperAtom(cur);
                        atoms.Add(nAtom);
                        cur = "";
                        st  = 2;
                    }
                    if (st == 3)
                    {
                        st = 3;
                    }
                }
                else
                if (expr[i] >= 'A' && expr[i] <= 'Z')
                {
                    if (st == 0 || st == 1)
                    {
                        Atom nAtom = new OperAtom(cur);
                        atoms.Add(nAtom);
                        cur = "";
                    }
                    if (st == 2)
                    {
                        Atom nAtom = new NumAtom(StringToInt(cur));
                        atoms.Add(nAtom);
                        cur = "";
                    }
                    st = 3;
                }
                else
                if (expr[i] >= 'a' && expr[i] <= 'z' || expr[i] == '<' || expr[i] == '>' || expr[i] == '=')
                {
                    if (st == 0)
                    {
                        Atom nAtom = new OperAtom(cur);
                        atoms.Add(nAtom);
                        cur = "";
                    }
                    if (st == 2)
                    {
                        Atom nAtom = new NumAtom(StringToInt(cur));
                        atoms.Add(nAtom);
                        cur = "";
                    }
                    if (st == 3)
                    {
                        Atom nAtom = new RefAtom(cur);
                        atoms.Add(nAtom);
                        cur = "";
                    }
                    st = 1;
                }
                else
                {
                    if (st == 0 || st == 1)
                    {
                        Atom nAtom = new OperAtom(cur);
                        atoms.Add(nAtom);
                    }
                    if (st == 2)
                    {
                        Atom nAtom = new NumAtom(StringToInt(cur));
                        atoms.Add(nAtom);
                    }
                    if (st == 3)
                    {
                        Atom nAtom = new RefAtom(cur);
                        atoms.Add(nAtom);
                    }
                    st  = 0;
                    cur = "";
                }
                cur += expr[i];
            }

            if (st == 0 || st == 1)
            {
                Atom nAtom = new OperAtom(cur);
                atoms.Add(nAtom);
            }
            if (st == 2)
            {
                Atom nAtom = new NumAtom(StringToInt(cur));
                atoms.Add(nAtom);
            }
            if (st == 3)
            {
                Atom nAtom = new RefAtom(cur);
                atoms.Add(nAtom);
            }

            /*string operParse = "";
             * int numParse = 0;
             *
             * for (int i = 0; i < expr.Length; ++i)
             * {
             *  if (expr[i] >= '0' && expr[i] <= '9')
             *  {
             *      if (i != 0 && !(expr[i - 1] >= '0' && expr[i - 1] <= '9'))
             *      {
             *          Atom nAtom = new OperAtom(operParse);
             *          atoms.Add(nAtom);
             *          operParse = "";
             *      }
             *      numParse = numParse * 10 + expr[i] - '0';
             *  }
             *  else
             *  {
             *      if (i != 0 && expr[i - 1] >= '0' && expr[i - 1] <= '9')
             *      {
             *          Atom nAtom = new NumAtom(numParse);
             *          atoms.Add(nAtom);
             *          numParse = 0;
             *      }
             *      else
             *      if (i != 0 && !(expr[i - 1] >= 'a' && expr[i - 1] <= 'z'))
             *      {
             *          Atom nAtom = new OperAtom(operParse);
             *          atoms.Add(nAtom);
             *          operParse = "";
             *      }
             *      operParse += expr[i];
             *  }
             * }
             *
             * if (expr.Length != 0)
             *  if (expr[expr.Length - 1] >= '0' && expr[expr.Length - 1] <= '9')
             *  {
             *      Atom nAtom = new NumAtom(numParse);
             *      atoms.Add(nAtom);
             *      numParse = 0;
             *  }
             *  else
             *  {
             *      Atom nAtom = new OperAtom(operParse);
             *      atoms.Add(nAtom);
             *      operParse = "";
             *  }*/

            return(atoms);
        }
Ejemplo n.º 3
0
        public string Calc(string expr)
        {
            try
            {
                List <Atom> infix = Parsing.getAtoms(expr);

                List <Atom> postfix = new List <Atom>();

                Stack <Atom> Symbs = new Stack <Atom>();

                for (int i = 0; i < infix.Count; ++i)
                {
                    if (infix[i].getType() == "OPER")
                    {
                        if (Symbs.Count != 0)
                        {
                            if (infix[i].getValue() == "(")
                            {
                                Symbs.Push(infix[i]);
                            }
                            else
                            if (infix[i].getValue() == ")")
                            {
                                while (Symbs.Peek().getValue() != "(")
                                {
                                    postfix.Add(Symbs.Peek());
                                    Symbs.Pop();
                                }
                                Symbs.Pop();
                            }
                            else
                            {
                                while (Symbs.Count != 0 && prior[Parsing.getHash(infix[i].getValue())] <= prior[Parsing.getHash(Symbs.Peek().getValue())])
                                {
                                    postfix.Add(Symbs.Peek());
                                    Symbs.Pop();
                                }

                                Symbs.Push(infix[i]);
                            }
                        }
                        else
                        {
                            Symbs.Push(infix[i]);
                        }
                    }
                    else
                    {
                        postfix.Add(infix[i]);
                    }
                }

                while (Symbs.Count != 0)
                {
                    postfix.Add(Symbs.Peek());
                    Symbs.Pop();
                }

                Stack <Atom> Nums = new Stack <Atom>();
                for (int i = 0; i < postfix.Count; ++i)
                {
                    if (postfix[i].getType() == "OPER")
                    {
                        Atom atom1 = Nums.Pop();
                        Atom atom2 = Nums.Pop();
                        Atom nAtom = new NumAtom(0);
                        if (postfix[i].getValue() == "+")
                        {
                            nAtom = new NumAtom(Parsing.StringToInt(atom1.getValue()) + Parsing.StringToInt(atom2.getValue()));
                        }
                        if (postfix[i].getValue() == "-")
                        {
                            nAtom = new NumAtom(Parsing.StringToInt(atom2.getValue()) - Parsing.StringToInt(atom1.getValue()));
                        }
                        if (postfix[i].getValue() == "*")
                        {
                            nAtom = new NumAtom(Parsing.StringToInt(atom1.getValue()) * Parsing.StringToInt(atom2.getValue()));
                        }
                        if (postfix[i].getValue() == "/")
                        {
                            nAtom = new NumAtom(Parsing.StringToInt(atom2.getValue()) / Parsing.StringToInt(atom1.getValue()));
                        }
                        if (postfix[i].getValue() == "div")
                        {
                            nAtom = new NumAtom(Parsing.StringToInt(atom2.getValue()) / Parsing.StringToInt(atom1.getValue()));
                        }
                        if (postfix[i].getValue() == "mod")
                        {
                            nAtom = new NumAtom(Parsing.StringToInt(atom2.getValue()) % Parsing.StringToInt(atom1.getValue()));
                        }
                        if (postfix[i].getValue() == "max")
                        {
                            nAtom = new NumAtom(Math.Max(Parsing.StringToInt(atom2.getValue()), Parsing.StringToInt(atom1.getValue())));
                        }
                        if (postfix[i].getValue() == "min")
                        {
                            nAtom = new NumAtom(Math.Min(Parsing.StringToInt(atom2.getValue()), Parsing.StringToInt(atom1.getValue())));
                        }
                        if (postfix[i].getValue() == "=")
                        {
                            if ((atom2.getValue() == atom1.getValue()))
                            {
                                nAtom = new NumAtom(1);
                            }
                            else
                            {
                                nAtom = new NumAtom(0);
                            }
                        }
                        if (postfix[i].getValue() == ">=")
                        {
                            if ((Parsing.StringToInt(atom2.getValue()) >= Parsing.StringToInt(atom1.getValue())))
                            {
                                nAtom = new NumAtom(1);
                            }
                            else
                            {
                                nAtom = new NumAtom(0);
                            }
                        }
                        if (postfix[i].getValue() == "<=")
                        {
                            if ((Parsing.StringToInt(atom2.getValue()) <= Parsing.StringToInt(atom1.getValue())))
                            {
                                nAtom = new NumAtom(1);
                            }
                            else
                            {
                                nAtom = new NumAtom(0);
                            }
                        }
                        if (postfix[i].getValue() == ">")
                        {
                            if ((Parsing.StringToInt(atom2.getValue()) > Parsing.StringToInt(atom1.getValue())))
                            {
                                nAtom = new NumAtom(1);
                            }
                            else
                            {
                                nAtom = new NumAtom(0);
                            }
                        }
                        if (postfix[i].getValue() == "<")
                        {
                            if ((Parsing.StringToInt(atom2.getValue()) < Parsing.StringToInt(atom1.getValue())))
                            {
                                nAtom = new NumAtom(1);
                            }
                            else
                            {
                                nAtom = new NumAtom(0);
                            }
                        }
                        if (postfix[i].getValue() == "<>")
                        {
                            if ((atom2.getValue() != atom1.getValue()))
                            {
                                nAtom = new NumAtom(1);
                            }
                            else
                            {
                                nAtom = new NumAtom(0);
                            }
                        }
                        Nums.Push(nAtom);
                    }
                    else
                    {
                        Nums.Push(postfix[i]);
                    }
                }
                if (Nums.Count == 0)
                {
                    return("");
                }
                else
                {
                    return(Nums.Peek().getValue().ToString());
                }
            }
            catch
            {
                return("");
            }
        }