コード例 #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
ファイル: Interpreter.cs プロジェクト: mkader/Design-Patterns
        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
ファイル: Interpreter.cs プロジェクト: mkader/Design-Patterns
        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
ファイル: Program.cs プロジェクト: juanmorenomotta/JavaC--PD
        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());
        }