コード例 #1
0
        public static List <Token> ConvertToPostfixToken(string expression)
        {
            int          prioriy;
            List <Token> tokens    = Token.StringToTokens(expression);
            List <Token> converted = new List <Token>(tokens.Count);

            Stack <Token> stack = new Stack <Token>();

            for (int i = 0; i < tokens.Count; i++)
            {
                Token current = tokens[i];
                if (current.TokenType == Token.Type.OPERATOR &&
                    (current.CValue == '+' ||
                     current.CValue == '-' ||
                     current.CValue == '*' ||
                     current.CValue == '/'))
                {
                    if (stack.Count <= 0)
                    {
                        stack.Push(current);
                    }
                    else
                    {
                        if (stack.Peek().CValue == '*' ||
                            stack.Peek().CValue == '/')
                        {
                            prioriy = 1;
                        }
                        else
                        {
                            prioriy = 0;
                        }
                        if (prioriy == 1)
                        {
                            if (current.CValue == '+' || current.CValue == '-')
                            {
                                converted.Add(stack.Pop());
                                i--;
                            }
                            else
                            {
                                converted.Add(stack.Pop());
                                i--;
                            }
                        }
                        else
                        {
                            if (current.CValue == '+' || current.CValue == '-')
                            {
                                converted.Add(stack.Pop());
                                stack.Push(current);
                            }
                            else
                            {
                                stack.Push(current);
                            }
                        }
                    }
                }
                else
                {
                    converted.Add(current);
                }
            }
            int len = stack.Count;

            for (int j = 0; j < len; j++)
            {
                converted.Add(stack.Pop());
            }
            return(converted);
        }
コード例 #2
0
        public static double EvaluateEquation(string eq)
        {
            List <Token> tokenized = Token.StringToTokens(eq);
            Token        equal     = tokenized.Find(t => t.TokenType == Token.Type.EQUAL);

            ReduceXDivision(tokenized);


            int eqIndex = tokenized.IndexOf(equal);

            Token[] left  = new Token[eqIndex];
            Token[] right = new Token[tokenized.Count - eqIndex - 1];
            Array.Copy(tokenized.ToArray(), left, eqIndex);
            Array.Copy(tokenized.ToArray(), eqIndex + 1, right, 0, tokenized.Count - eqIndex - 1);

            List <Token> leftList  = new List <Token>(left);
            List <Token> rightList = new List <Token>(right);

            Console.WriteLine("Left: ");
            PrintTokens(leftList);
            Console.WriteLine("Right: ");
            PrintTokens(rightList);

            while (leftList.FindAll(t => t.TokenType == Token.Type.X).Count > 1 || rightList.FindAll(t => t.TokenType == Token.Type.X).Count > 1)
            {
                ReduceX(leftList);
                ReduceX(rightList);
            }
            MinusToPlus(leftList);
            MinusToPlus(rightList);
            ReduceNumbers(rightList);
            ReduceNumbers(leftList);
            Console.WriteLine("Left: ");
            PrintTokens(leftList);
            Console.WriteLine("Right: ");
            PrintTokens(rightList);

            int    indexX = leftList.FindIndex(t => t.TokenType == Token.Type.X);
            double a      = indexX > 0 && leftList[indexX - 1].CValue == '*' ? leftList[indexX - 2].Value : indexX < 0 ? 0 : 1;

            indexX = rightList.FindIndex(t => t.TokenType == Token.Type.X);
            double c = indexX > 0 && rightList[indexX - 1].CValue == '*' ? rightList[indexX - 2].Value : indexX < 0 ? 0 : 1;

            indexX = leftList.FindIndex(t => leftList.IndexOf(t) < leftList.Count - 1 && t.TokenType == Token.Type.NUMBER && leftList[leftList.IndexOf(t) + 1].CValue == '+');
            if (indexX < 0)
            {
                if (leftList.Count == 1 && leftList[0].TokenType == Token.Type.NUMBER)
                {
                    indexX = 0;
                }
                else
                {
                    indexX = leftList.FindIndex(t => leftList.IndexOf(t) > 0 && t.TokenType == Token.Type.NUMBER && leftList[leftList.IndexOf(t) - 1].CValue == '+');
                }
            }

            double b = indexX < 0 ? 0 : leftList[indexX].Value;

            indexX = rightList.FindIndex(t => rightList.IndexOf(t) < rightList.Count - 1 && t.TokenType == Token.Type.NUMBER && rightList[rightList.IndexOf(t) + 1].CValue == '+');
            if (indexX < 0)
            {
                if (rightList.Count == 1 && leftList[0].TokenType == Token.Type.NUMBER)
                {
                    indexX = 0;
                }
                else
                {
                    indexX = rightList.FindIndex(t => rightList.IndexOf(t) > 0 && t.TokenType == Token.Type.NUMBER && rightList[rightList.IndexOf(t) - 1].CValue == '+');
                }
            }
            double d = indexX < 0 ? 0 : rightList[indexX].Value;


            Console.WriteLine(a);
            Console.WriteLine(c);
            Console.WriteLine(b);
            Console.WriteLine(d);

            if (a - c == 0)
            {
                return(Double.NaN);
            }
            else if (d - b == 0)
            {
                return(Double.PositiveInfinity);
            }
            else
            {
                return((d - b) / (a - c));
            }
        }