Ejemplo n.º 1
0
        /// <summary>
        /// 计算中缀表达式中的所有函数
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        private LinkList calcFuns(LinkList list)
        {
            //遍历整个链表


            ArrayList paramsList = new ArrayList(5);
            LinkNode  funNode = null, insertNode = null, nextTempNode = null;

            for (LinkNode node = list.First; node != null; node = nextTempNode)
            {
                nextTempNode = node.Next;              //node用于循环,而每次计算里都要将)删除,所有提前保存node的下一个结点的值



                if (node.getWord().wordType == WordType.Leftp)
                {
                    stack.Push(node);
                }
                else if (node.getWord().wordType == WordType.Rightp)
                {
                    if (stack.Count < 1)
                    {
                        throw new Exception("括号不匹配,缺左括号");
                    }
                    else if (((LinkNode)stack.Peek()).Previous == null || ((LinkNode)stack.Peek()).Previous.getWord().wordType != WordType.FunName)
                    {
                        //括号只是一个表达式,而不属于一个函数
                        //条件是括号前面不是函数名,或括号前面没有元素
                        stack.Pop();
                        continue;
                    }
                    else
                    {
                        paramsList.Clear();


                        LinkNode start = (LinkNode)stack.Pop();
                        funNode    = start.Previous;
                        insertNode = node.Next;
                        list.removeBetween(start, node);
                        LinkList innerList = new LinkList(start, node);                     //还包含括号

                        //第一种情况,括号里面没有任何参数
                        if (innerList.Count == 2)                     //判断括号里是否为空

                        {
                            //计算函数的值

                            double result = Function(funNode.getWord().valueString, paramsList);


                            list.remove(funNode);
                            list.insert(insertNode, new LinkNode(new Word(WordType.Number, result.ToString())));

                            continue;
                        }

                        else
                        {
                            innerList.remove(start);                            //删除括号
                            innerList.remove(node);

                            LinkNode comma = null;
                            comma = innerList.searchComma();

                            //第二种情况,括号里只有一个参数(参数表达式)
                            if (comma == null)                         //没有逗号
                            {
                                paramsList.Add(calculator.compute(innerList));

                                //根据参数计算函数的值
                                double result = Function(funNode.getWord().valueString, paramsList);

                                list.remove(funNode);
                                list.insert(insertNode, new LinkNode(new Word(WordType.Number, result.ToString())));


                                continue;
                            }
                            else                             /////第三种情况,括号里有多个参数表达式
                            {
                                while (comma != null)
                                {
                                    LinkNode newStart = innerList.First;
                                    innerList.removeBetween(newStart, comma);
                                    LinkList tempList = new LinkList(newStart, comma);

                                    if (tempList.Count < 2)
                                    {
                                        throw new Exception("逗号前面有问题");
                                    }
                                    tempList.remove(tempList.End);                                    //删除逗号


                                    paramsList.Add(calculator.compute(tempList));

                                    comma = innerList.searchComma();
                                }

                                if (innerList.Count == 0)
                                {
                                    throw new Exception("函数的逗号后面有问题");
                                }
                                else
                                {
                                    paramsList.Add(calculator.compute(innerList));
                                    //用多个参数计算函数

                                    double result = Function(funNode.getWord().valueString, paramsList);

                                    list.remove(funNode);
                                    list.insert(insertNode, new LinkNode(new Word(WordType.Number, result.ToString())));
                                }
                            }
                        }
                    }
                }
            }


            return(list);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 计算中缀表达式的值
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>


        public double compute(LinkList list)
        {
            this.list = list;
            createSuffixExpression();
            return(reckon());
        }