term.cs

来自「Data Structures and Algorithms with Obj」· CS 代码 · 共 86 行

CS
86
字号
namespace Opus6
{
    using System;

    [Version("$Id: Term.cs,v 1.4 2001/10/28 19:50:09 brpreiss Exp $"), Copyright("Copyright (c) 2001 by Bruno R. Preiss, P.Eng.")]
    public class Term : ComparableObject
    {
        public Term(Term term) : this(term.coefficient, term.exponent)
        {
        }

        public Term(double coefficient, int exponent)
        {
            this.coefficient = coefficient;
            this.exponent = exponent;
        }

        public override int CompareTo(object obj)
        {
            Term term1 = (Term) obj;
            if (this.exponent == term1.exponent)
            {
                if (this.coefficient < term1.coefficient)
                {
                    return -1;
                }
                if (this.coefficient > term1.coefficient)
                {
                    return 1;
                }
                return 0;
            }
            return (this.exponent - term1.exponent);
        }

        public void Differentiate()
        {
            if (this.exponent > 0)
            {
                this.coefficient *= this.exponent;
                this.exponent--;
            }
            else
            {
                this.coefficient = 0;
            }
        }

        public static Term operator +(Term t1, Term t2)
        {
            if (t1.exponent != t2.exponent)
            {
                throw new ArgumentException("unequal exponents");
            }
            return new Term(t1.coefficient + t2.coefficient, t1.exponent);
        }

        public override string ToString()
        {
            return (this.coefficient + "x^" + this.exponent);
        }


        public double Coefficient
        {
            get
            {
                return this.coefficient;
            }
        }

        public int Exponent
        {
            get
            {
                return this.exponent;
            }
        }


        protected double coefficient;
        protected int exponent;
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?