コード例 #1
0
ファイル: Factor.cs プロジェクト: eawd/equation-parser
        /// <summary>
        /// Adds a factor.
        /// eg. {2, x^2}, {4} + {-3, y}, {2}
        /// </summary>
        /// <param name="factor"></param>
        public void Add(Factor factor)
        {
            Factor rFactor = this;
            Factor lFactor = factor;
            Factor result  = new Factor();

            if (lFactor.Terms.Count > 0)
            {
                for (int i = lFactor.Terms.Count - 1; i >= 0; i--)
                {
                    bool done = false;

                    if (rFactor.Terms.Count > 0)
                    {
                        for (int j = rFactor.Terms.Count - 1; j >= 0; j--)
                        {
                            if (lFactor.Terms [i].VariablesToString().Equals(rFactor.Terms [j].VariablesToString()))
                            {
                                lFactor.Terms [i].Coefficient += rFactor.Terms [j].Coefficient;
                                if (lFactor.Terms [i].Coefficient != 0)
                                {
                                    result.Add(lFactor.Terms [i]);
                                }
                                lFactor.Terms.RemoveAt(i);
                                rFactor.Terms.RemoveAt(j);
                                done = true;
                                break;
                            }
                        }
                    }

                    if (!done)
                    {
                        result.Add(lFactor.Terms [i]);
                        lFactor.Terms.RemoveAt(i);
                    }
                }
            }

            if (rFactor.Terms.Count > 0)
            {
                foreach (var rTerm in rFactor.Terms)
                {
                    result.Add(rTerm);
                }
            }

            this.Terms = result.Terms;
        }
コード例 #2
0
ファイル: Factor.cs プロジェクト: eawd/equation-parser
        /// <summary>
        /// Divide by a factor.
        /// eg. (2x + 1) / (3x + 2)
        /// </summary>
        /// <param name="t">The factor to be devided by.</param>
        public void Divide(Factor t)
        {
            Factor result = new Factor();
            Factor rTerm;

            foreach (var factor in Terms)
            {
                rTerm = t.Clone();
                rTerm.Divide(factor);
                result.Add(rTerm);
            }

            Terms = result.Terms;
        }
コード例 #3
0
ファイル: Factor.cs プロジェクト: eawd/equation-parser
        /// <summary>
        /// Multiply a factor by a factor.
        /// (2x + 1) * (x + 3)
        /// </summary>
        /// <param name="factor"></param>
        public void Multiply(Factor factor)
        {
            Factor result = new Factor();
            Factor rTerm;

            foreach (var f in Terms)
            {
                rTerm = factor.Clone();
                rTerm.Multiply(f);
                result.Add(rTerm);
            }

            Terms = result.Terms;
        }
コード例 #4
0
        //SumOp -> + Term SumOp
        //SumOp -> - Term SumOp
        //SumOp -> EPSILON
        Factor SumOp(Factor t)
        {
            Factor t2;

            switch (lookahead.type)
            {
            case TokenType.ADD:
                GetNextToken();
                t2 = Term();
                t.Add(t2);
                return(SumOp(t));

            case TokenType.SUB:
                GetNextToken();
                t2 = Term();
                t.Subtract(t2);
                return(SumOp(t));

            default:
                return(t);
            }
        }
コード例 #5
0
        /// <summary>
        /// Adds a factor.
        /// eg. {2, x^2}, {4} + {-3, y}, {2}
        /// </summary>
        /// <param name="factor"></param>
        public void Add(Factor factor)
        {
            Factor rFactor = this;
            Factor lFactor = factor;
            Factor result = new Factor ();

            if (lFactor.Terms.Count > 0)
                for (int i = lFactor.Terms.Count - 1; i >= 0; i--) {
                    bool done = false;

                    if (rFactor.Terms.Count > 0)
                        for (int j = rFactor.Terms.Count - 1; j >= 0; j--) {
                            if (lFactor.Terms [i].VariablesToString ().Equals (rFactor.Terms [j].VariablesToString ())) {
                                lFactor.Terms [i].Coefficient += rFactor.Terms [j].Coefficient;
                                if (lFactor.Terms [i].Coefficient != 0)
                                    result.Add (lFactor.Terms [i]);
                                lFactor.Terms.RemoveAt (i);
                                rFactor.Terms.RemoveAt (j);
                                done = true;
                                break;
                            }
                        }

                    if (!done) {
                        result.Add (lFactor.Terms [i]);
                        lFactor.Terms.RemoveAt (i);
                    }
                }

            if (rFactor.Terms.Count > 0)
                foreach (var rTerm in rFactor.Terms) {
                    result.Add (rTerm);
                }

            this.Terms = result.Terms;
        }
コード例 #6
0
 //SumOp -> + Term SumOp
 //SumOp -> - Term SumOp
 //SumOp -> EPSILON
 Factor SumOp(Factor t)
 {
     Factor t2;
     switch (lookahead.type) {
     case TokenType.ADD:
         GetNextToken ();
         t2 = Term ();
         t.Add (t2);
         return SumOp (t);
     case TokenType.SUB:
         GetNextToken ();
         t2 = Term ();
         t.Subtract (t2);
         return SumOp (t);
     default:
         return t;
     }
 }
コード例 #7
0
        /// <summary>
        /// Multiply a factor by a factor.
        /// (2x + 1) * (x + 3)
        /// </summary>
        /// <param name="factor"></param>
        public void Multiply(Factor factor)
        {
            Factor result = new Factor ();
            Factor rTerm;

            foreach (var f in Terms) {
                rTerm = factor.Clone ();
                rTerm.Multiply (f);
                result.Add (rTerm);
            }

            Terms = result.Terms;
        }
コード例 #8
0
        /// <summary>
        /// Divide by a factor.
        /// eg. (2x + 1) / (3x + 2)
        /// </summary>
        /// <param name="t">The factor to be devided by.</param>
        public void Divide(Factor t)
        {
            Factor result = new Factor ();
            Factor rTerm;

            foreach (var factor in Terms) {
                rTerm = t.Clone ();
                rTerm.Divide (factor);
                result.Add (rTerm);
            }

            Terms = result.Terms;
        }