Beispiel #1
0
        public override eRuleReply Process(Shape aShape, ref List <Shape> aShapeList, ShuntingYard aExpressionParser)
        {
            float moveX = aExpressionParser.Parse(mTranslation[0]);
            float moveY = aExpressionParser.Parse(mTranslation[1]);
            float moveZ = aExpressionParser.Parse(mTranslation[2]);

            aShape.mScope.mPosition += new Vector3(moveX, moveY, moveZ);
            return(eRuleReply.Success);
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            // Just for fun - coloring the console text :-)
            WriteColorLine(ConsoleColor.Cyan, "Exercise_2_1_2\n");

            // Example from exercise
            WriteColor(ConsoleColor.Green, "5 1 2 + 4 * + 3 - ");
            Console.Write("= ");
            WriteColorLine(ConsoleColor.Red, ReversePolishCalculator.Compute("5 1 2 + 4 * + 3 -"));

            // Example from https://en.wikipedia.org/wiki/Shunting-yard_algorithm

            WriteColor(ConsoleColor.Green, "3 + 4 * 2 / (1 - 5) ^ 2 ^ 3 ");
            Console.Write("= ");
            WriteColorLine(ConsoleColor.Red, ReversePolishCalculator.Compute(ShuntingYard.Parse("3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3")));

            Console.WriteLine("\nThe correct answer if we had used real numbers (doubles instead of integers) would be:");
            WriteColor(ConsoleColor.Green, "3 + 4 * 2 / (1 - 5) ^ 2 ^ 3 ");
            Console.Write("= ");
            var result = 3.0 + ((4.0 * 2.0) / Math.Pow((1.0 - 5.0), Math.Pow(2.0, 3.0)));

            WriteColorLine(ConsoleColor.Red, result.ToString(CultureInfo.InvariantCulture));
            // the use of CultureInfo.InvariantCulture prevent formatting of numbers using the
            // systems format, ie. for me the Danish format with a comma instead of a dot: 3,0001220703125
        }
Beispiel #3
0
 public override eRuleReply Process(Shape aShape, ref List <Shape> aShapeList, ShuntingYard aExpressionParser)
 {
     float[] scale = new float[3] {
         0.0f, 0.0f, 0.0f
     };
     for (int i = 0; i < 3; ++i)
     {
         if (mScale[i][0] == '\'')
         {
             scale[i] = aShape.mScope.mScale[i] * aExpressionParser.Parse(mScale[i].Substring(1));
         }
         else
         {
             scale[i] = aExpressionParser.Parse(mScale[i]);
         }
     }
     aShape.mScope.mScale = new Vector3(scale[0], scale[1], scale[2]);
     return(eRuleReply.Success);
 }
Beispiel #4
0
        public override eRuleReply Process(Shape aShape, ref List <Shape> aShapeList, ShuntingYard aExpressionParser)
        {
            float lhs = aExpressionParser.Parse(mLHS);
            float rhs = aExpressionParser.Parse(mRHS);

            switch (mOperator)
            {
            case eCaseOperator.LESS:
                return(lhs < rhs ? eRuleReply.Success : eRuleReply.Failed);

            case eCaseOperator.LESS_EQUAL:
                return(lhs <= rhs ? eRuleReply.Success : eRuleReply.Failed);

            case eCaseOperator.EQUAL:
                return(lhs == rhs ? eRuleReply.Success : eRuleReply.Failed);

            case eCaseOperator.GREATER_EQUAL:
                return(lhs >= rhs ? eRuleReply.Success : eRuleReply.Failed);

            case eCaseOperator.GREATER:
                return(lhs > rhs ? eRuleReply.Success : eRuleReply.Failed);
            }
            return(eRuleReply.Success);
        }
Beispiel #5
0
        public override eRuleReply Process(Shape aShape, ref List <Shape> aShapeList, ShuntingYard aExpressionParser)
        {
            bool    isRelative          = mNumberOfSplits[0] == '\'';
            float   inputNumberOfSplits = aExpressionParser.Parse(mNumberOfSplits);
            float   newSize             = 0.0f;
            int     numberOfSplits      = 0;
            Vector3 axis = Vector3.zero;

            axis[(int)mAxis] = 1;
            if (isRelative)
            {
                mNumberOfSplits = mNumberOfSplits.Substring(1);
                newSize         = aShape.mScope.mScale[(int)mAxis] / inputNumberOfSplits;
                numberOfSplits  = (int)(inputNumberOfSplits);
            }
            else
            {
                newSize        = inputNumberOfSplits;
                numberOfSplits = Mathf.FloorToInt(aShape.mScope.mScale[(int)mAxis] / inputNumberOfSplits);
            }

            for (int i = 0; i < numberOfSplits; ++i)
            {
                Shape splitShape = new Shape();
                splitShape.mName                     = mNameOfChildren;
                splitShape.mScope.mPosition          = aShape.mScope.mPosition + axis * newSize * i;
                splitShape.mScope.mScale             = aShape.mScope.mScale;
                splitShape.mScope.mScale[(int)mAxis] = newSize;
                splitShape.mScope.mColor             = aShape.mScope.mColor;
                splitShape.mSplitData                = new SplitAttributes {
                    mSplitIndex = i, mSplitTotal = numberOfSplits
                };
                aShapeList.Add(splitShape);
                aShape.mScope.mChildren.Add(splitShape.mScope);
            }
            return(eRuleReply.Success);
        }
Beispiel #6
0
 public void Parse_ExpressionWithNestedFunctions_ReturnRpn()
 {
     ShuntingYard.Parse("sin ( max ( 2 , 3 ) / 3 * pi )").Should().Be("2 3 max 3 / pi * sin");
 }
Beispiel #7
0
 public void Parse_Pow_ReturnRpn()
 {
     ShuntingYard.Parse("pow ( 2 , 8 )").Should().Be("2 8 pow");
     Calculate("pow ( 2 , 8 )").Should().Be(256);
 }
Beispiel #8
0
 public void Parse_Sqrt_ReturnRpn()
 {
     ShuntingYard.Parse("sqrt ( 16 )").Should().Be("16 sqrt");
     Calculate("sqrt ( 16 )").Should().Be(4);
 }
Beispiel #9
0
 public void Parse_MixedExpressionWithParenthesesAndPower_ReturnRpn()
 {
     ShuntingYard.Parse("3 + 4 * 2 / ( 1 - 5 ) ^ 2 ^ 3").Should().Be("3 4 2 * 1 5 - 2 3 ^ ^ / +");
 }
Beispiel #10
0
 public void Parse_MixedExpressionWithParentheses_ReturnRpn()
 {
     ShuntingYard.Parse("3 + 4 * 2 / ( 1 - 5 )").Should().Be("3 4 2 * 1 5 - / +");
     Calculate("3 + 4 * 2 / ( 1 - 5 )").Should().Be(1);
 }
Beispiel #11
0
        public void Parse_MissingLeftParenthesesInExpression_ThrowsException()
        {
            Action act = () => ShuntingYard.Parse("1 - 5 )");

            act.Should().Throw <ArgumentException>();
        }
Beispiel #12
0
 public void Parse_ExpressionInParentheses_ReturnRpn()
 {
     ShuntingYard.Parse("( 1 - 5 )").Should().Be("1 5 -");
     Calculate("1 5 -").Should().Be(-4);
 }
Beispiel #13
0
 public void Parse_AdditionAndMultiplication_ReturnRpn()
 {
     ShuntingYard.Parse("3 + 4 * 2").Should().Be("3 4 2 * +");
     Calculate("3 + 4 * 2").Should().Be(11);
 }
Beispiel #14
0
 public void Parse_NumberAddition_ReturnRpn()
 {
     ShuntingYard.Parse("3 + 4").Should().Be("3 4 +");
     Calculate("3 + 4").Should().Be(7);
 }
Beispiel #15
0
 public void Parse_Number_ReturnNumber()
 {
     ShuntingYard.Parse("3").Should().Be("3");
 }
Beispiel #16
0
 // Helper methods
 // to make the tests more readable
 private int Calculate(string expression)
 {
     return(ReversePolishCalculator.Compute(ShuntingYard.Parse(expression)));
 }