C# 我需要一些多项式方面的帮助
如何使用数组创建多项式函数?基本上我想显示多项式(例如x^2+3x+5)。然后,我想展示最高多项式次数是多少。例如,x^2+3x+5=2(最高阶)C# 我需要一些多项式方面的帮助,c#,arrays,console,polynomial-math,C#,Arrays,Console,Polynomial Math,如何使用数组创建多项式函数?基本上我想显示多项式(例如x^2+3x+5)。然后,我想展示最高多项式次数是多少。例如,x^2+3x+5=2(最高阶) 我们还必须检查x之后的字符是+、-还是空白。我们还要检查该指数。我建议这样做 string polynomial = "x^2 + 3x + 5"; int index = 0,highestdegree = 0; foreach (char character in polynomial) {
我们还必须检查
x
之后的字符是+
、-
还是空白。我们还要检查该指数。我建议这样做
string polynomial = "x^2 + 3x + 5";
int index = 0,highestdegree = 0;
foreach (char character in polynomial) {
if(character == '^')
{
index++;
try{
int test;
int.TryParse(polynomial[index],out test);
if(test >highestdegree)
highestdegree = test;
index--;
}
catch{
index--;
}
}
index += 1;
}
if(highest degree == 0)
{
highestdegree == 1;
}
return highest degree;
不过,您必须先将多项式转换为字符串。您在程序中使用什么代码来表示多项式,Math.Pow()
我想,因为它是C#?让我们看看你的代码……是的,我想使用Math.Pow()和一个循环来填充指数部分。我一直在尝试使用数组创建多项式。
string polynomial = "x^2 + 3x + 5";
int index = 0,highestdegree = 0;
foreach (char character in polynomial) {
if(character == '^')
{
index++;
try{
int test;
int.TryParse(polynomial[index],out test);
if(test >highestdegree)
highestdegree = test;
index--;
}
catch{
index--;
}
}
index += 1;
}
if(highest degree == 0)
{
highestdegree == 1;
}
return highest degree;