在方法中显示

时间:2017-05-17 21:50:26

标签: c# arrays class methods

编辑:我能够添加并显示多项式但是如何在最后摆脱额外的加号?这是目前显示的内容:

多项式方程

7X ^ 5 + 3×^ 4 + 10×^ 3 + 2×^ 2 + 15X ^ 1 + 5×^ 0 + 1×^ 5 = 8X ^ 5 + 3×^ 4 + 10×^ 3 + 2×^ 2 + 15X ^ 1 + 5×^ 0 +

按Enter退出

最后的加号是我想摆脱^

这是我现在的代码。如果您需要澄清,请告诉我!

static void Main(string[] args)
    {
        int[] intCoefficients = new int[6] { 5, 15, 2, 10, 3, 7 };
        Polynomial firstpolynomial = new Polynomial(5);
        Polynomial secondpolynomial = new Polynomial(5, intCoefficients);
        Polynomial polynomialthree = new Polynomial();
        polynomialthree = firstpolynomial + secondpolynomial;

        Console.WriteLine("Polynomial Equation");
        Console.WriteLine("");

        //call the method
        secondpolynomial.Display();
        firstpolynomial.Display();
        polynomialthree.Display();

        Console.WriteLine("");
        Console.WriteLine("");
        Console.WriteLine("Press Enter to Exit");
        Console.ReadLine();

    }
}
class Polynomial: IComparable 
{
    //fields
    int intDegree;
    int[] intCoefficient = new int[6];

    //uses IComparable
    int IComparable.CompareTo(Object o)
    {
        int returnVal = -1;
        Polynomial temp = (Polynomial)o;
        if (this.Degree > temp.Degree)
        {
            returnVal = 1;
        }
        else
        {
            if (this.Degree < temp.Degree)
            {
                returnVal = -1;
            }
            else
            {
                returnVal = 0;
            }
        }
        return returnVal;
    }

    //properties
    public int Degree
    {
        get
        {
            return intDegree;
        }
        set
        {
            intDegree = value;
        }
    }
    public int[] Coefficient
    {
        get
        {
            return intCoefficient;
        }
        set
        {
            for (int x = 0; x < value.Length; x++)
            {
                intCoefficient[x] = value[x];
            }
        }
    }

    //constructors
    public Polynomial() //this constructor creates the polynomial 0
    {
        for (int x = 0; x < intCoefficient.Length; x++)
        {
            intCoefficient[x] = 0;
            intDegree = 0;
        }
    }
    public Polynomial(int intDegree) //creates x^5 polynomial
    {
        this.intDegree = intDegree;
        for (int x = 0; x < intCoefficient.Length; x++)
        {
            intCoefficient[x] = 0;
        }
        intCoefficient[intDegree] = 1;
    }
    public Polynomial(int intDegree, int[] intArray) //creates other polynomial
    {
        this.intDegree = intDegree;
        for (int x = 0; x < intCoefficient.Length; x++)
        {
            intCoefficient[x] = 0;
        }
        for (int x = 0; x < intArray.Length; x++)
        {
            intCoefficient[x] = intArray[x];
        }
    }
    public override string ToString() //creates polynomial equation and returns to Main()
    {

        string strPolynomial = "";

        for (int x = intDegree; x >= 0; x--)
        {
            if (intCoefficient[x] > 0 && intCoefficient[x] != 1)
            {
                strPolynomial += intCoefficient[x].ToString() + "x^" + x.ToString() + "+";
            }
            else
            {
                if (intCoefficient[x] == 1)
                {
                    strPolynomial += intCoefficient[x].ToString() + "x^" + x.ToString() + "="; 
                }
            }
        }

        return strPolynomial;
    }
    //adds first two polynomials together and returns result to the third polynomial
    public static Polynomial operator +(Polynomial firstpolynomial, Polynomial secondpolynomial)
    {
        int intAddedDegree;
        int[] intAddedCoefficient = new int[6];

        if (firstpolynomial.Degree > secondpolynomial.Degree)
        {
            intAddedDegree = firstpolynomial.Degree;
        }
        else
        {
            intAddedDegree = secondpolynomial.Degree;
        }
        for (int x = 0; x < intAddedCoefficient.Length; x++)
        {
            intAddedCoefficient[x] = firstpolynomial.Coefficient[x] + secondpolynomial.Coefficient[x];
        }

        Polynomial result = new Polynomial(intAddedDegree, intAddedCoefficient);

        return result;
    }
    //display method
    public void Display() //couldn't figure out how to make the plus go away
    {
        for (int x = intDegree; x >= 0; x--)
        {
            if (intCoefficient[x] > 0 && intCoefficient[x] != 1)
            {
                Console.Write(intCoefficient[x].ToString() + "x^" + x.ToString() + "+");
            }
            else
            {
                if (intCoefficient[x] == 1)
                {
                    Console.WriteLine(intCoefficient[x].ToString() + "x^" + x.ToString() + "=");
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

不是一个小问题,而且您有许多代码质量问题。尝试理解这个代码,恕我直言更加灵活,简化和健壮。

所有有趣的逻辑都在ToString()中,它创建了一个多项式的字符串。 diplay函数只将ToString()的输出写入控制台窗口。

static class Program
{
    internal static void Main(string[] args)
    {
        int[] intCoefficients = new int[] { 5, 15, 2, 10, 3, 7 };
        Polynomial firstpolynomial = new Polynomial(5);
        Polynomial secondpolynomial = new Polynomial( intCoefficients );
        Polynomial polynomialthree;
        polynomialthree=firstpolynomial+secondpolynomial;

        Console.WriteLine("Polynomial Equation");
        Console.WriteLine();

        //call the method
        secondpolynomial.Display();
        firstpolynomial.Display();
        polynomialthree.Display();

        Console.WriteLine();
        Console.WriteLine();
        Console.WriteLine("Press Enter to Exit");
        Console.ReadLine();
    }
}

public class Polynomial : IComparable, IComparable<Polynomial>
{
    readonly int[] intCoefficient;
    //constructors
    public Polynomial() //this constructor creates the polynomial 0
        : this(0) { }
    public Polynomial(int intDegree) //creates x^5 polynomial
    {
        this.intCoefficient=new int[intDegree+1];
        this.intCoefficient[intDegree]=1;
    }
    public Polynomial(params int[] intArray) //creates other polynomial
    {
        this.intCoefficient=intArray;
    }

    //properties
    public int Degree
    {
        get
        {
            return intCoefficient.Length-1;
        }
    }
    public int[] Coefficient
    {
        get
        {
            return intCoefficient;
        }
    }

    /// <summary>
    /// Converts the polynomial to a string representation
    /// </summary>
    /// <returns>A string object</returns>
    public override string ToString() 
    {
        StringBuilder result = new StringBuilder();
        for (int i=0; i<=Degree; i++)
        {
            int coef = intCoefficient[i];
            int sign = Math.Sign(coef);
            coef=Math.Abs(coef);
            if (coef>0)
            {
                if (sign<0)
                {
                    // always add a leading negative when needed
                    result.Append("-");
                }
                else if (result.Length>0)
                {
                    // only add a leading plus if not first term
                    result.Append("+");
                }
                // append Cx^i terms
                if (coef>1&&i>1)
                {
                    result.AppendFormat("{0}x^{1}", coef, i);
                }
                else if (coef>1&&i==1)
                {
                    result.AppendFormat("{0}x", coef);
                }
                else if (coef>1&&i==0)
                {
                    result.AppendFormat("{0}", coef);
                }
                else if (coef==1&&i==1)
                {
                    result.Append("x");
                }
                else if (i==0)
                {
                    result.Append(coef.ToString());
                }
                else
                {
                    result.AppendFormat("x^{0}", i);
                }
            } // ignore term of coef==0
        }            
        return result.ToString();
    }

    /// <summary>
    /// Adds the coefficients of two polynomials
    /// </summary>
    /// <param name="firstpolynomial">A polynomial</param>
    /// <param name="secondpolynomial">Another polynomial</param>
    /// <returns>The resulting polynomial</returns>
    public static Polynomial operator +(Polynomial firstpolynomial, Polynomial secondpolynomial)
    {
        // Always make sure the first polynomial has equal or more terms
        if (secondpolynomial.Degree>firstpolynomial.Degree)
        {
            // Reverse the operation
            return secondpolynomial+firstpolynomial;
        }
        // Result must have same degree as first poly
        int result_degree = firstpolynomial.Degree;
        int[] result_coef = new int[result_degree+1];
        for (int i = 0; i<secondpolynomial.intCoefficient.Length; i++)
        {
            // Add coefficients from two polynomials
            result_coef[i]=firstpolynomial.intCoefficient[i]+secondpolynomial.intCoefficient[i];
        }
        for (int i = secondpolynomial.intCoefficient.Length; i<result_coef.Length; i++)
        {
            // Assign remaining coefficients from fist polynomial
            result_coef[i]=firstpolynomial.intCoefficient[i];
        }
        return new Polynomial(result_coef);
    }
    //display method
    public void Display() 
    {
        Console.WriteLine(ToString());
    }

    public int CompareTo(Polynomial other)
    {
        // Just compare the order of the polynmomials
        return Degree.CompareTo(other.Degree);
    }
    int IComparable.CompareTo(Object o)
    {
        if (o is Polynomial)
        {
            return CompareTo(o as Polynomial);
        }
        return 0;
    }
}

输出

Polynomial Equation

5+15x+2x^2+10x^3+3x^4+7x^5
x^5
5+15x+2x^2+10x^3+3x^4+8x^5


Press Enter to Exit