C#中的多项式问题 [英] Trouble with polynomials in C#

查看:72
本文介绍了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循环中的内容感到困惑,使第一个和第二个多项式显示然后添加它们。

解决方案

代码没有意义。多项式由它的度和系数(度+ 1值)描述。系数通常是浮点值而不是整数。



无需存储学位,因为可以通过获取系数数组长度来检索它。



未经测试的例子:

  class 多项式:IComparable 
{
double [] Coefficents;

public 多项式( double [] coeffs)
{
Init(coeffs);
}

public void Init( double [] coeffs)
{
Coefficents = coeffs;
}

public int GetDegree()
{
return Coefficients.Length - 1 ;
}

public double 计算( double x)
{
double y = 0 < /跨度>;
double px = 1 ;
for int i = 0 ; i < Coefficients.Length; i ++)
{
y + = px * Coefficients [i];
px * = x;
}
return y;
}

// 创建字符串表示
public 覆盖 string ToString()
{
string 结果;
if (Coefficients.Length)
result = Double .ToString(Coefficients [ 0 ]);
for int i = 1 ; i < Coefficients.Length; i ++)
{
if (系数[i] < 0
结果+ = - ;
else
result + = +;
result + = Double .ToString(Math.Abs​​(Coefficients [i]));
结果+ = * x ^;
result + = Int.ToString(i);
}
返回结果;
}

}


尝试

 < span class =code-keyword> static   void  Main( string  [] args) 
{
int [] ar = new int [] { 1 0 2 };
多项式p = new 多项式( 3 ,ar);
Console.WriteLine(p);
}





with



< pre lang =C#> // 显示多项式
public 覆盖 string ToString()
{
string s = string .Empty;

for int i = 0 ; i < intCoefficents.Length; i ++)
{
if (intCoefficents [i]!= 0
if (s == string .Empty)
s = string .Format( {0} x ^ {1},intCoefficents [i],i);
else
s = string .Format( {0} x ^ {1} +,intCoefficents [i],i)+ + s;
}
if (s == string .Empty)
s = 0;
return s;
}


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 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();
} 



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.

解决方案

The code makes no sense. A polynomial is described by it's degree and the coefficients (degree + 1 values). The coefficients are usually floating point values and not integers.

There is no need to store the degree because it can be retrieved by getting the coefficients array length.

Untested example:

class Polynomial: IComparable 
{
    double[] Coefficents;
    
    public Polynomial(double[] coeffs)
    {
        Init(coeffs);
    }

    public void Init(double[] coeffs)
    {
        Coefficents = coeffs;
    }

    public int GetDegree()
    {
        return Coefficients.Length - 1;
    }
    
    public double Calculate(double x)
    {
        double y = 0;
        double px = 1;
        for (int i = 0; i < Coefficients.Length; i++)
        {
            y += px * Coefficients[i];
            px *= x;
        }
        return y;
    }

    // Create a string representation
    public override string ToString()
    {
        string result;
        if (Coefficients.Length)
            result = Double.ToString(Coefficients[0]);
        for (int i = 1; i < Coefficients.Length; i++)
        {
            if (Coefficients[i] < 0)
                result += " - ";
            else
                result += " + ";
            result += Double.ToString(Math.Abs(Coefficients[i]));
            result += "*x^";
            result += Int.ToString(i);
        }
        return result;
    }

}


Try

static void Main(string[] args)
{
  int[] ar = new int[] { 1, 0, 2 };
  Polynomial p = new Polynomial(3, ar);
  Console.WriteLine(p);
}



with

//displays a polynomial
public override string ToString()
{
  string s = string.Empty;

  for (int i = 0; i < intCoefficents.Length; i++)
  {
    if (intCoefficents[i] != 0)
      if ( s == string.Empty)
        s = string.Format("{0}x^{1}", intCoefficents[i], i);
      else
        s = string.Format("{0}x^{1} + ", intCoefficents[i], i) + " " + s;
  }
  if (s == string.Empty)
    s = "0";
  return s;
}


这篇关于C#中的多项式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆