Esempio n. 1
0
        public void Test2()
        {
            InfixConvParser icp = new InfixConvParser();

            string[] input = new string[]
            {
                "ln(a)*(b+cos(90))",
                "a=12",
                "b=13"
            };
            List <ExpressionItem> expected = new List <ExpressionItem>
            {
                new ExpressionItem(ItemType.Operation, Operation.Logarithm),
                new ExpressionItem(ItemType.Bracket, Bracket.LeftBracket),
                new ExpressionItem(ItemType.Number, 12),
                new ExpressionItem(ItemType.Bracket, Bracket.RightBracket),
                new ExpressionItem(ItemType.Operation, Operation.Multiply),
                new ExpressionItem(ItemType.Bracket, Bracket.LeftBracket),
                new ExpressionItem(ItemType.Number, 13),
                new ExpressionItem(ItemType.Operation, Operation.Plus),
                new ExpressionItem(ItemType.Operation, Operation.Cosine),
                new ExpressionItem(ItemType.Bracket, Bracket.LeftBracket),
                new ExpressionItem(ItemType.Number, 90),
                new ExpressionItem(ItemType.Bracket, Bracket.RightBracket),
                new ExpressionItem(ItemType.Bracket, Bracket.RightBracket),
            };
            List <ExpressionItem> actual = icp.Parse(input);

            Assert.AreEqual(expected, actual);
        }
Esempio n. 2
0
        public void Test()
        {
            /// Example:
            /// Input: "a+b-100
            ///         a=12
            ///         b=13"
            /// Returns: { (Number, 12), (Operation, Plus), (Number, 13),
            /// (Operation, Minus), (Number, 100) }
            /// </remarks>
            InfixConvParser icp = new InfixConvParser();

            string[] input = new string[]
            {
                "c-100",
                "a=12",
                "c=13"
            };
            List <ExpressionItem> expected = new List <ExpressionItem>
            {
                new ExpressionItem(ItemType.Number, 12),
                new ExpressionItem(ItemType.Operation, Operation.Plus),
                new ExpressionItem(ItemType.Number, 13),
                new ExpressionItem(ItemType.Operation, Operation.Minus),
                new ExpressionItem(ItemType.Number, 100)
            };
            List <ExpressionItem> actual = icp.Parse(input);

            Assert.AreEqual(expected, actual);
        }
        public static void Show()
        {
            string pathToFolder = Directory.GetCurrentDirectory()
                                  .Replace(@"bin\Debug", "") + @"FilesSecond\";

            string[] text      = File.ReadAllLines(pathToFolder + "input" + ".txt");
            var      parser    = new InfixConvParser();
            var      parseText = parser.Parse(text);
            var      infixAlg  = new InfixСonversion();
            var      postfix   = infixAlg.InfixToPostfix(parseText);
            var      answer    = infixAlg.CalculateValue(postfix);
            var      sb        = new StringBuilder();

            foreach (var e in postfix)
            {
                switch (e.Type)
                {
                case ItemType.Bracket:
                    if (e.BraketType == Bracket.LeftBracket)
                    {
                        sb.Append("(");
                    }
                    else if (e.BraketType == Bracket.RightBracket)
                    {
                        sb.Append(")");
                    }
                    break;

                case ItemType.Number:
                    sb.Append(e.NumberValue);
                    break;

                case ItemType.Operation:
                    switch (e.OperationType)
                    {
                    case Operation.Cosine:
                        sb.Append("cos");
                        break;

                    case Operation.Divide:
                        sb.Append(":");
                        break;

                    case Operation.Logarithm:
                        sb.Append("lg");
                        break;

                    case Operation.Minus:
                        sb.Append("-");
                        break;

                    case Operation.Multiply:
                        sb.Append("*");
                        break;

                    case Operation.Plus:
                        sb.Append("+");
                        break;

                    case Operation.Power:
                        sb.Append("^");
                        break;

                    case Operation.Sine:
                        sb.Append("sin");
                        break;

                    case Operation.SquareRoot:
                        sb.Append("sqrt");
                        break;
                    }
                    break;
                }
                sb.Append(" ");
            }
            var prefixStr = sb.ToString();

            Console.WriteLine("Префиксная запись - " + text[0]);
            for (int i = 1; i < text.Count(); i++)
            {
                Console.WriteLine(text[i]);
            }
            Console.WriteLine("Результат в постфиксной записи - " + prefixStr);
            Console.WriteLine("Ответ: " + answer + ".");
        }