상속: IExpression
예제 #1
0
파일: Program.cs 프로젝트: pelehok/patterns
        public static IExpression GetMarriedWomanExpression()
        {
            IExpression julie   = new TerminalExpression("Julie");
            IExpression married = new TerminalExpression("Married");

            return(new AndExpression(julie, married));
        }
예제 #2
0
        public static IExpression Build(this string expression)
        {
            Stack <IExpression> expressionStack = new Stack <IExpression>();

            foreach (string value in expression.Split(" "))
            {
                IExpression resultExpression;

                if (int.TryParse(value, out int number))
                {
                    resultExpression = new TerminalExpression(number);
                }
                else
                {
                    IExpression secondExpression = expressionStack.Pop();
                    IExpression firstExpression  = expressionStack.Pop();

                    resultExpression = value switch
                    {
                        "+" => new AddExpression(firstExpression, secondExpression),
                        "-" => new SubtractExpression(firstExpression, secondExpression),
                        "*" => new MultiplyExpression(firstExpression, secondExpression),
                        "/" => new DivideExpression(firstExpression, secondExpression),
                        _ => throw new Exception("Invalid operation")
                    };
                }

                expressionStack.Push(resultExpression);
            }

            return(expressionStack.Pop());
        }
    }
예제 #3
0
파일: Program.cs 프로젝트: pelehok/patterns
        public static IExpression GetMaleExpression()
        {
            IExpression robert = new TerminalExpression("Robert");
            IExpression john   = new TerminalExpression("John");

            return(new OrExpression(robert, john));
        }
예제 #4
0
        public static Expression getMarriedWomanExpression()
        {
            Expression Mary    = new TerminalExpression("Mary");
            Expression Married = new TerminalExpression("Married");

            return(new AndExpression(Married, Mary));
        }
예제 #5
0
        public static Expression GetMaleExpression()
        {
            Expression Zac  = new TerminalExpression("Zac");
            Expression Jean = new TerminalExpression("Jean");

            return(new OrExpression(Zac, Jean));
        }
예제 #6
0
        //builds the interpreter tree and defines the rule "Site1 and (Bonus1 or (User1 or User2))"
        static Expression BuildInterpreterTree()
        {
            // Literal
            Expression terminal1 = new TerminalExpression("Bonus1");
            Expression terminal2 = new TerminalExpression("User1");
            Expression terminal3 = new TerminalExpression("User2");
            Expression terminal4 = new TerminalExpression("Site1");

            Expression alternation1 = new OrExpression(terminal2, terminal3);
            Expression alternation2 = new OrExpression(terminal1, alternation1);

            return(new AndExpression(terminal4, alternation2));
        }
예제 #7
0
        public static IExpression GetMarriedWomanExpression()
        {
            IExpression e1 = new TerminalExpression()
            {
                Data = "C"
            };
            IExpression e2 = new TerminalExpression()
            {
                Data = "1"
            };

            return(new AndExpression()
            {
                Exp1 = e1, Exp2 = e2
            });
        }
예제 #8
0
        public static IExpression GetMaleExpression()
        {
            IExpression e1 = new TerminalExpression()
            {
                Data = "A"
            };
            IExpression e2 = new TerminalExpression()
            {
                Data = "B"
            };

            return(new OrExpression()
            {
                Exp1 = e1, Exp2 = e2
            });
        }
예제 #9
0
        private Expression buildTree(string expr)
        {
            Stack s = new Stack();

            for (int i = 0; i < expr.Length; i++)
            {
                string currChar = expr.Substring(i, 1);
                if (isOperator(currChar) == false)
                {
                    Expression e = new TerminalExpression(currChar);
                    s.Push(e);
                }
                else
                {
                    Expression r = (Expression)s.Pop();
                    Expression l = (Expression)s.Pop();
                    Expression n = getNonTerminalExpression(currChar, l, r);
                    s.Push(n);
                }
            }//for
            return((Expression)s.Pop());
        }