本文介绍了C#中的多项式问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我的目标是创建一个程序,创建两个多项式并将它们相加并显示结果。我在构造函数中遇到了创建两个多项式的问题,这两个多项式将显示在我必须覆盖的ToString()方法中。我有问题覆盖ToString()方法以返回多项式并在显示方法中显示它。 static void Main(string [ ] args) { //创建两个可以加在一起的多项式 多项式[] polCoefficents = new Polynomial [5]; //在数组中创建5个空格 polCoefficents.ToString(); 多项式polynomialone = new Polynomial(); //创建第一个多项式多项式polynomialtwo = new Polynomial(); //创建第二个多项式多项式polDegrees = new Polynomial(); //将degress设置为coefficents的长度 for(int x = 0; x< polCoefficents.Length; x ++)//创建系数 { polCoefficents [x] =新的多项式(x); //括号中的数字表示度 polCoefficents [0] = new Polynomial(0); polCoefficents [0] .Coefficents = 20; // x ^ 0的系数是20 polCoefficents [1] = new Polynomial(1); polCoefficents [1] .Coefficents = -5; // x的系数是-5 polCoefficents [2] = new Polynomial(2); polCoefficents [2] .Coefficents = 4; // x ^ 2的系数是4 polCoefficents [3] = new Polynomial(3); polCoefficents [3] .Coefficents = 5; // x ^ 3的系数是5 polCoefficents [4] = new Polynomial(4); polCoefficents [4] .Coefficents = 9; // x ^ 4的系数是9 polCoefficents [x] .ToString(); } Console.WriteLine(); Console.WriteLine(按Enter键退出); Console.ReadLine(); } } class多项式:IComparable { int intDegree; int [] intCoefficents = new int [] {1,5,10,4,8}; //对 int进行排序IComparable.CompareTo(对象B) { int returnVal; 多项式nextvalue =(多项式)B; if(this.intDegree> nextvalue.intDegree) { returnVal = 1; } else if(this.intDegree< nextvalue.intDegree) { returnVal = -1; } 其他 { returnVal = 0; } 返回returnVal; } public int Degree { get { return intDegree; } 设置 { intDegree = value; } } public int Coefficents { get { return intCoefficents [4]; } 设定 { intCoefficents [4] = value; } } public Polynomial() { //创建多项式0 // intDegree = intCoefficents.Length; for(int i = 0; i< intCoefficents.Length; i ++) { intCoefficents [i] = 0; } } //构造函数 public多项式(int intDegree) { //此代码将创建第二个多项式( x ^ 4) this.intDegree = intDegree; for(int x = 0; x< intCoefficents.Length; x ++) { this.intCoefficents [x] = 0; } this.intCoefficents [this.Degree] = 1; ToString(); } public Polynomial(int Degree,int [] intArrayIntegers) { intCoefficents = intArrayIntegers; intDegree = intArrayIntegers.Length; } //显示多项式公共覆盖字符串ToString() { for(int i = 0; i< intCoefficents.Length ; i ++)//必须是for循环,但对内部的内容感到困惑 { //混淆了这里的内容} 返回string.Format({ 0} x ^ {1} +,intCoefficents,intDegree); //返回多项式} public void Display() { //显示多项式 Console.WriteLine(); } 我的尝试: 我能够完成这项工作,但现在我对for循环中的内容感到困惑,使第一个和第二个多项式显示然后添加它们。解决方案 My goal is to create a program that creates two polynomials and adds them together and displays the result. I am having trouble with the constructors to create the two polynomials that will be displayed in the ToString() Method that I have to override. I am having issues overriding the ToString() Method to return the polynomial and display it in the Display Method.static void Main(string[] args){//create two polynomials that can be added togetherPolynomial[] polCoefficents = new Polynomial[5]; //creates 5 spaces in arraypolCoefficents.ToString();Polynomial polynomialone = new Polynomial(); //creates first polynomialPolynomial polynomialtwo = new Polynomial(); //creates second polynomialPolynomial polDegrees = new Polynomial(); //set degress to length of coefficentsfor (int x = 0; x < polCoefficents.Length; x++) //creates the coefficents{polCoefficents[x] = new Polynomial(x);// numbers in brackets represent degreespolCoefficents[0] = new Polynomial(0);polCoefficents[0].Coefficents = 20; //the coefficent for x ^ 0 is 20polCoefficents[1] = new Polynomial(1);polCoefficents[1].Coefficents = -5; //the coefficent for x is -5polCoefficents[2] = new Polynomial(2);polCoefficents[2].Coefficents = 4; //the coefficent for x ^ 2 is 4polCoefficents[3] = new Polynomial(3);polCoefficents[3].Coefficents = 5; //the coefficent for x ^ 3 is 5polCoefficents[4] = new Polynomial(4);polCoefficents[4].Coefficents = 9; //the coefficent for x ^ 4 is 9polCoefficents[x].ToString();}Console.WriteLine("");Console.WriteLine("Press Enter to Exit");Console.ReadLine();}}class Polynomial: IComparable {int intDegree;int[] intCoefficents = new int[] { 1, 5, 10, 4, 8};//sortsint IComparable.CompareTo(Object B){int returnVal;Polynomial nextvalue = (Polynomial)B;if (this.intDegree > nextvalue.intDegree){returnVal = 1;}else if (this.intDegree < nextvalue.intDegree){returnVal = -1;}else{returnVal = 0;}return returnVal;}public int Degree{get{return intDegree;}set{intDegree = value;}}public int Coefficents{get{return intCoefficents[4];}set{intCoefficents[4] = value;}}public Polynomial(){//creates polynomial 0// intDegree = intCoefficents.Length;for (int i = 0; i < intCoefficents.Length; i++){intCoefficents[i] = 0;}}//constructorspublic Polynomial(int intDegree){//this code will create the second polynomial (x^4)this.intDegree = intDegree;for (int x = 0; x < intCoefficents.Length; x++){this.intCoefficents[x]=0;}this.intCoefficents[this.Degree] = 1;ToString();}public Polynomial(int Degree, int[] intArrayIntegers){intCoefficents = intArrayIntegers;intDegree = intArrayIntegers.Length;}//displays a polynomialpublic override string ToString(){for (int i = 0; i < intCoefficents.Length; i++) //has to be for loop, but confused on what goes inside{//confused on what goes in here}return string.Format("{0}x^{1} + ", intCoefficents, intDegree); //returns polynomial}public void Display(){//displays polynomialConsole.WriteLine();} What I have tried:I was able to make that work but now I am confused on what should go in the for loop to make the first and second polynomials display and then add them. 解决方案 这篇关于C#中的多项式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-26 19:57