Beispiel #1
0
 public Boolean getResult() {
     ParserD p=new ParserD(m_formula[0], stock);
     decimal o1;
     try
     {
         o1 = p.getResult();
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
     p=new ParserD(m_formula[2], stock);
     decimal o2;
     try
     {
         o2 = p.getResult();
     }
     catch (Exception e)
     {
         throw new Exception(e.Message);
     }
     if (m_formula[1].Equals("=")) {
         if (o1 == o2)
             return true;
         else 
             return false;
     }
     else if (m_formula[1].Equals(">")) {
         if (o1 > o2)
             return true;
         else
             return false;
     }
     else if (m_formula[1].Equals("<")) {
         if (o1 < o2)
             return true;
         else
             return false;
     }
     else { return false; }
 }
Beispiel #2
0
        private String simplyFormula(String [] formula)
        {
            Queue <String> qFormula = new Queue <String>();
            Queue <String> temp     = new Queue <String>();
            Queue <String> result   = new Queue <String>();

            foreach (string s in formula)
            {
                qFormula.Enqueue(s);
            }
            while (qFormula.Count != 0)
            {
                if (qFormula.Peek().Equals("{"))
                {
                    qFormula.Dequeue();
                    temp.Clear();
                    try {
                        while (!qFormula.Peek().Equals("}"))
                        {
                            temp.Enqueue(qFormula.Dequeue());
                        }
                    }
                    catch (Exception e)
                    {
                        return("表达式错误!请检查括号是否匹配!");
                    }
                    qFormula.Dequeue();
                    String [] f = temp.ToArray();
                    result.Enqueue(simplyFormula(f));
                }
                else if (!(qFormula.Peek().Equals("AND") || qFormula.Peek().Equals("and")) &&
                         !(qFormula.Peek().Equals("OR") || qFormula.Peek().Equals("or")))
                {
                    if (qFormula.Peek().Contains(">") || qFormula.Peek().Contains("<") || qFormula.Peek().Contains("="))
                    {
                        ParserB p = new ParserB(qFormula.Dequeue(), stock);
                        try
                        {
                            result.Enqueue(p.getResult().ToString());
                        }
                        catch (Exception e)
                        {
                            return(e.Message);
                        }
                    }
                    else
                    {
                        ParserD p = new ParserD(qFormula.Dequeue(), stock);
                        try
                        {
                            result.Enqueue(p.getResult().ToString());
                        }
                        catch (Exception e)
                        {
                            return(e.Message);
                        }
                    }
                }

                else
                {
                    result.Enqueue(qFormula.Dequeue());
                }
            }
            Stack <String> sResult = new Stack <string>();
            Stack <String> sTemp   = new Stack <string>();

            while (result.Count() != 0)
            {
                sTemp.Push(result.Dequeue());
            }
            while (sTemp.Count() != 0)
            {
                sResult.Push(sTemp.Pop());
            }
            while (sResult.Count() > 1)
            {
                Boolean o1;
                try{
                    o1 = Convert.ToBoolean(sResult.Pop());
                }
                catch (FormatException e)
                {
                    return("表达式错误!请检查AND(OR)运算符操作数据是否为布尔值!");
                }
                catch
                {
                    return("表达式错误!请检查AND(OR)运算符前是否缺失数据!");
                }

                String  opreator = sResult.Pop();
                Boolean o2;
                try
                {
                    o2 = Convert.ToBoolean(sResult.Pop());
                }
                catch (FormatException e)
                {
                    return("表达式错误!请检查AND(OR)运算符操作数据是否为布尔值!");
                }
                catch
                {
                    return("表达式错误!请检查AND(OR)运算符后是否缺失数据!");
                }

                if (opreator.Equals("AND") || opreator.Equals("and"))
                {
                    String tempResult = (o1 && o2).ToString();
                    sResult.Push(tempResult);
                }
                if (opreator.Equals("OR") || opreator.Equals("or"))
                {
                    String tempResult = (o1 || o2).ToString();
                    sResult.Push(tempResult);
                }
            }

            return(sResult.Pop());
        }
Beispiel #3
0
        public Boolean getResult()
        {
            ParserD p = new ParserD(m_formula[0], stock);
            decimal o1;

            try
            {
                o1 = p.getResult();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            p = new ParserD(m_formula[2], stock);
            decimal o2;

            try
            {
                o2 = p.getResult();
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
            if (m_formula[1].Equals("="))
            {
                if (o1 == o2)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else if (m_formula[1].Equals(">"))
            {
                if (o1 > o2)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else if (m_formula[1].Equals("<"))
            {
                if (o1 < o2)
                {
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }