示例#1
0
 public static OperatorMap.Map GetMap(string Operator)
 {
     if (OperatorMap.CheckOperator(Operator))
     {
         OperatorMap.Map[] array = OperatorMap.map();
         for (int i = 0; i < array.Length; i++)
         {
             OperatorMap.Map result = array[i];
             if (result.Operator == Operator)
             {
                 return(result);
             }
         }
     }
     return(new OperatorMap.Map(99, Operator));
 }
示例#2
0
 /// <summary>
 /// 计算后缀表达式
 /// </summary>
 /// <param name="alexpression"></param>
 /// <returns></returns>
 public static object ComputePostfix(ArrayList alexpression)
 {
     try
     {
         //·建立一个栈S
         Stack s     = new Stack();
         int   count = alexpression.Count;
         int   i     = 0;
         while (i < count)
         {
             //·从左到右读后缀表达式,读到数字就将它转换为数值压入栈S中,
             string word = alexpression[i++].ToString();
             if (OperatorMap.IsVar(word))
             {
                 s.Push(word);
                 //      System.Console.WriteLine("Push:{0}",word);
             }
             else//读到运算符则从栈中依次弹出两个数分别到Y和X,
             if (OperatorMap.CheckOperator(word))
             {
                 string y, x, sResult;
                 if (!CheckOneOperator(word))
                 {
                     y = s.Pop().ToString();
                     x = s.Pop().ToString();
                     //然后以“X 运算符 Y”的形式计算机出结果,再压加栈S中
                     sResult = ComputeTwo(x, y, word).ToString();
                     s.Push(sResult);
                 }
                 else
                 {
                     x       = s.Pop().ToString();
                     sResult = ComputeOne(x, word).ToString();
                     s.Push(sResult);
                 }
             }
         }
         string spop = s.Pop().ToString();
         //    System.Console.WriteLine("Result:{0}",spop);
         return(spop);
     }
     catch
     {
         System.Console.WriteLine("Result:表达式不符合运算规则!Sorry!");
         return("Sorry!Error!");
     }
 }
示例#3
0
        public static object ComputePostfix(ArrayList alexpression)
        {
            object result;

            try
            {
                Stack stack = new Stack();
                int   count = alexpression.Count;
                int   i     = 0;
                while (i < count)
                {
                    string text = alexpression[i++].ToString();
                    if (OperatorMap.IsVar(text))
                    {
                        stack.Push(text);
                    }
                    else
                    {
                        if (OperatorMap.CheckOperator(text))
                        {
                            if (!CheckOneOperator(text))
                            {
                                string sR    = stack.Pop().ToString();
                                string text2 = stack.Pop().ToString();
                                string obj   = ComputeTwo(text2, sR, text).ToString();
                                stack.Push(obj);
                            }
                            else
                            {
                                string text2 = stack.Pop().ToString();
                                string obj   = ComputeOne(text2, text).ToString();
                                stack.Push(obj);
                            }
                        }
                    }
                }
                string text3 = stack.Pop().ToString();
                result = text3;
            }
            catch
            {
                Console.WriteLine("Result:表达式不符合运算规则!Sorry!");
                result = "Sorry!Error!";
            }
            return(result);
        }
示例#4
0
        /// <summary>
        /// 中缀表达式转换为后缀表达式
        /// </summary>
        /// <param name="alexpression"></param>
        /// <returns></returns>
        public static ArrayList ConvertToPostfix(ArrayList alexpression)
        {
            ArrayList alOutput  = new ArrayList();
            Stack     sOperator = new Stack();
            string    word      = null;
            int       count     = alexpression.Count;
            int       i         = 0;

            while (i < count)
            {
                word = alexpression[i++].ToString();

                //·读到左括号时总是将它压入栈中
                if (OperatorMap.CheckLeftBracket(word))
                {
                    sOperator.Push(word);
                }
                else

                //·读到右括号时,将*近栈顶的第一个左括号上面的运算符全部依次弹出,送至输出队列后,再丢弃左括号。
                if (OperatorMap.CheckRightBracket(word))
                {
                    while (true)
                    {
                        if (sOperator.Count == 0)
                        {
                            break;
                        }
                        string sTop = sOperator.Peek().ToString();
                        if (sTop == "(")
                        {
                            sOperator.Pop(); break;
                        }
                        else
                        {
                            alOutput.Add(sOperator.Pop());
                        }
                    }
                }
                else

                //·当读到数字直接送至输出队列中
                if (OperatorMap.IsVar(word))
                {
                    alOutput.Add(word);
                }
                else

                //·当读到运算符t时,
                //     a.将栈中所有优先级高于或等于t的运算符弹出,送到输出队列中;
                //     b.t进栈
                if (OperatorMap.CheckOperator(word))
                {
                    while (sOperator.Count > 0)
                    {
                        string sPop = sOperator.Peek().ToString();

                        if (sPop == "(")
                        {
                            break;
                        }

                        if (OperatorMap.GetMaxprior(word, sPop) >= 0)
                        {
                            //       sPop = sOperator.Pop().ToString();
                            alOutput.Add(sOperator.Pop().ToString());
                        }
                        else
                        {
                            break;
                        }
                        //      System.Console.WriteLine("XH{0}",sPop);
                    }
                    sOperator.Push(word);
                }

                //    System.Console.WriteLine("{0}",word.ToString());
            }

            //中缀表达式全部读完后,若栈中仍有运算符,将其送到输出队列中
            while (sOperator.Count > 0)
            {
                string s = sOperator.Pop().ToString();
                alOutput.Add(s);
                //    System.Console.WriteLine("{0}:{1}",sOperator.Count,s.ToString());
            }

            return(alOutput);
        }
示例#5
0
        public static ArrayList ConvertToPostfix(ArrayList alexpression)
        {
            ArrayList arrayList = new ArrayList();
            Stack     stack     = new Stack();
            int       count     = alexpression.Count;
            int       i         = 0;

            while (i < count)
            {
                string text = alexpression[i++].ToString();
                if (OperatorMap.CheckLeftBracket(text))
                {
                    stack.Push(text);
                }
                else
                {
                    if (OperatorMap.CheckRightBracket(text))
                    {
                        while (stack.Count != 0)
                        {
                            string a = stack.Peek().ToString();
                            if (a == "(")
                            {
                                stack.Pop();
                                break;
                            }
                            arrayList.Add(stack.Pop());
                        }
                    }
                    else
                    {
                        if (OperatorMap.IsVar(text))
                        {
                            arrayList.Add(text);
                        }
                        else
                        {
                            if (OperatorMap.CheckOperator(text))
                            {
                                while (stack.Count > 0)
                                {
                                    string text2 = stack.Peek().ToString();
                                    if (text2 == "(" || OperatorMap.GetMaxprior(text, text2) < 0)
                                    {
                                        break;
                                    }
                                    arrayList.Add(stack.Pop().ToString());
                                }
                                stack.Push(text);
                            }
                        }
                    }
                }
            }
            while (stack.Count > 0)
            {
                string value = stack.Pop().ToString();
                arrayList.Add(value);
            }
            return(arrayList);
        }