Проблема с многочленами в C#
Моя цель-создать программу, которая создает два полинома, складывает их вместе и отображает результат. У меня возникли проблемы с конструкторами для создания двух полиномов, которые будут отображаться в методе ToString (), который я должен переопределить. У меня возникли проблемы с переопределением метода ToString () для возврата полинома и отображения его в методе Display.
static void Main(string[] args) { //create two polynomials that can be added together Polynomial[] polCoefficents = new Polynomial[5]; //creates 5 spaces in array polCoefficents.ToString(); Polynomial polynomialone = new Polynomial(); //creates first polynomial Polynomial polynomialtwo = new Polynomial(); //creates second polynomial Polynomial polDegrees = new Polynomial(); //set degress to length of coefficents for (int x = 0; x < polCoefficents.Length; x++) //creates the coefficents { polCoefficents[x] = new Polynomial(x); // numbers in brackets represent degrees polCoefficents[0] = new Polynomial(0); polCoefficents[0].Coefficents = 20; //the coefficent for x ^ 0 is 20 polCoefficents[1] = new Polynomial(1); polCoefficents[1].Coefficents = -5; //the coefficent for x is -5 polCoefficents[2] = new Polynomial(2); polCoefficents[2].Coefficents = 4; //the coefficent for x ^ 2 is 4 polCoefficents[3] = new Polynomial(3); polCoefficents[3].Coefficents = 5; //the coefficent for x ^ 3 is 5 polCoefficents[4] = new Polynomial(4); polCoefficents[4].Coefficents = 9; //the coefficent for x ^ 4 is 9 polCoefficents[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}; //sorts int 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; } } //constructors public 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 polynomial public 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 polynomial Console.WriteLine(); }
Что я уже пробовал:
Я смог заставить это работать, но теперь я запутался в том, что должно идти в цикле for, чтобы отобразить первый и второй полиномы, а затем добавить их.
Ralf Meier
Что именно вы хотите видеть в результате "ToString"?
Какие элементы должны работать с циклом ? (Я тоже немного сбит с толку)