【发布时间】:2025-11-23 15:45:01
【问题描述】:
编辑:我能够添加和显示多项式,但我如何摆脱最后的额外加号?这是当前显示的内容:
多项式方程
7x^5+3x^4+10x^3+2x^2+15x^1+5x^0+1x^5= 8x^5+3x^4+10x^3+2x^2+15x^1+5x^0+
回车键退出
最后的加号是我想去掉的^
这是我现在拥有的代码。如果您需要澄清,请告诉我!
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() + "=");
}
}
}
}
【问题讨论】:
-
你试过
polynomialthree.Display()吗? -
您需要在
Polynomial类中覆盖ToString以使其按照您现在的方式工作,或者摆脱Console.WriteLines 并调用firstpolynomial.Display()(等)而是。 -
好的,感谢您的帮助!我只是想通了,但现在因为我决定在显示方法中显示它弄乱了顺序,所以它不是从最高到最低的指数显示。相反,它的显示从最低到最高。我知道我必须使用 Array.Sort() 来做到这一点,但我不知道如何对三个多项式进行排序
-
对于所有更新以及如有任何混淆,我深表歉意。在过去的几分钟里,我一直在研究这个问题,并且能够使它工作,但有一个例外。当它显示多项式时,它会在等式末尾显示一个加号。我该如何摆脱这个?多项式 7x^5+3x^4+10x^3+2x^2+15x^1+5x^0+1x^5= 8x^5+3x^4+10x^3+2x^2+15x^1+ 5x^0+ 按 Enter 退出
-
通过删除添加加号的部分代码。