Example #1
0
        /// <summary>
        /// 得到计算表达式的所有变量名称
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        public ArrayList getVaribles(string s)
        {
            if (s.Trim().Length == 0)
            {
                throw new Exception("表达式为空");
            }
            ArrayList array = new ArrayList(8);


            LinkList list = scan.parse(s);

            LinkNode node = list.First;

            while (node != null)
            {
                if (node.getWord().wordType == WordType.Identifier || node.getWord().wordType == WordType.IdentifierWithDot)
                {
                    array.Add(node.getWord().valueString);
                }

                node = node.Next;
            }

            return(array);
        }
Example #2
0
        /// <summary>
        /// 检查+ - * / 操作符的格式是否正确
        /// </summary>

        private void observeList()
        {
            LinkNode node = list.First;
            LinkNode pre, next;

            while (node != null)
            {
                //word是+ - * / 这四种类型
                if (node.getWord().wordType <= WordType.Div)
                {
                    pre  = node.Previous;
                    next = node.Next;

                    if (next == null)
                    {
                        throw new Exception(node.getWord().valueString + "后面有误1");
                    }

                    if (pre.getWord().wordType != WordType.Rightp && pre.getWord().wordType != WordType.Number)
                    {
                        throw new Exception(node.getWord().valueString + "前面有误");
                    }

                    if (next.getWord().wordType != WordType.Leftp && next.getWord().wordType != WordType.Number)
                    {
                        throw new Exception(node.getWord().valueString + "后面有误2");
                    }
                }


                node = node.Next;
            }
        }
Example #3
0
        /// <summary>
        /// 计算表达式的值
        /// </summary>
        /// <returns></returns>


        private double reckon()
        {
            //此时stack里存放的是double对象
            stack.Clear();
            double r1, r2;

            LinkNode node = suffixList.First;

            while (node != null)
            {
                if (node.getWord().wordType == WordType.Number)
                {
                    stack.Push(double.Parse(node.getWord().valueString));
                }
                else if (node.getWord().wordType < WordType.Leftp)
                {
                    if (stack.Count >= 2)
                    {
                        r2 = (double)stack.Pop();
                        r1 = (double)stack.Pop();

                        switch (node.getWord().wordType)
                        {
                        case WordType.Plus: stack.Push(r1 + r2); break;

                        case WordType.Minus: stack.Push(r1 - r2); break;

                        case WordType.Mul: stack.Push(r1 * r2); break;

                        case WordType.Div: stack.Push(r1 / r2); break;

                        case WordType.Power: stack.Push(Math.Pow(r1, r2)); break;
                        }
                    }
                    else
                    {
                        throw new Exception("表达式操作数数目不正确");
                    }
                }
                else
                {
                    throw new Exception("表达式错误 0");
                }

                node = node.Next;
            }

            if (stack.Count != 1)
            {
                throw new Exception("表达式操作数数目不正确");
            }
            else
            {
                return((double)stack.Pop());
            }
        }
Example #4
0
        /// <summary>
        /// 在+或-前添加数字0
        /// </summary>
        /// <returns></returns>
        public LinkList modify()
        {
            //修正头结点
            LinkNode node = this.First;

            if (node.getWord().wordType == WordType.Plus || node.getWord().wordType == WordType.Minus)
            {
                insert(node, new LinkNode(new Word(WordType.Number, "0")));
            }


            LinkNode preNode = node;          //前一个结点

            node = node.Next;

            while (node != null)
            {
                //在+ -号前而是(就在+ -号前添加一个0
                if (node.getWord().wordType == WordType.Plus || node.getWord().wordType == WordType.Minus)
                {
                    if (preNode.getWord().wordType == WordType.Leftp || preNode.getWord().wordType == WordType.Comma)
                    {
                        insert(node, new LinkNode(new Word(WordType.Number, "0")));
                    }
                }

                //确定函数名
                if (preNode.getWord().wordType == WordType.Identifier && node.getWord().wordType == WordType.Leftp)
                {
                    preNode.getWord().wordType = WordType.FunName;
                }

                preNode = node;

                node = node.Next;
            }



            return(this);
        }
Example #5
0
        /// <summary>
        /// 操作符入栈
        /// </summary>
        /// <param name="node"></param>

        private void dealOperator(LinkNode node)
        {
            list.remove(node);

            while (stack.Count != 0 && priorities[(int)((Word)stack.Peek()).wordType, (int)node.getWord().wordType] == 1)
            {
                Word word = (Word)stack.Pop();
                suffixList.add(new LinkNode(word));
            }


            stack.Push(node.getWord());
        }
Example #6
0
        /// <summary>
        /// 将表达式中的变量替换为具体的数字
        /// </summary>
        /// <param name="slist"></param>

        public void replaceVariable(MyList slist)
        {
            LinkNode node = first;

            while (node != null)
            {
                Word word = node.getWord();
                if (word.wordType == WordType.Identifier || word.wordType == WordType.IdentifierWithDot)

                {
                    string name = node.getWord().valueString;

                    double v = slist[name];



                    word.wordType    = WordType.Number;
                    word.valueString = v.ToString();
                }

                node = node.Next;
            }
        }
Example #7
0
        /// <summary>
        /// 扫描list产生后缀表达式
        /// </summary>

        internal LinkList createSuffixExpression()
        {
            if (list.Count == 0)
            {
                throw new Exception("表达式元素个数为零");
            }
            suffixList = new LinkList();
            stack.Clear();
            LinkNode node = list.First;

            while (node != null)
            {
                LinkNode next = node.Next;;
                switch (node.getWord().wordType)
                {
                case WordType.Number: list.remove(node); suffixList.add(node); break;

                case WordType.Plus: dealOperator(node); break;

                case WordType.Minus: dealOperator(node); break;

                case WordType.Mul: dealOperator(node); break;

                case WordType.Div: dealOperator(node); break;

                case WordType.Power: dealOperator(node); break;

                case WordType.Leftp: dealOperator(node); break;

                case WordType.Rightp: dealRight(node); break;

                default: break;
                }
                node = next;
            }

            //弹出在栈中剩余的元素
            while (stack.Count != 0)
            {
                Word word = (Word)stack.Pop();
                if (word.wordType == WordType.Leftp)
                {
                    throw new Exception("在括号(匹配错误");
                }
                suffixList.add(new LinkNode(word));
            }

            return(suffixList);
        }
Example #8
0
        /// <summary>
        /// 在链表中寻找逗号,计这两个结点
        /// </summary>
        /// <returns></returns>


        public LinkNode searchComma()
        {
            LinkNode node = first;

            while (node != null)
            {
                if (node.getWord().wordType == WordType.Comma)
                {
                    return(node);
                }

                node = node.Next;
            }
            return(null);           //没有逗号这个结点
        }
Example #9
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);
        }