我需要一些多项式方面的帮助
本文关键字:方面 帮助 多项式 | 更新日期: 2023-09-27 18:24:58
如何使用数组创建多项式函数?基本上我想显示多项式(例如x^2+3x+5)。然后,我想展示最高多项式次数是多少。例如,x^2+3x+5=2(最高次数)。
我可能建议这样做。
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;
不过,你必须先把多项式变成一个字符串。
// initial highest exponent
int exponent = 0;
// We're assuming [in] is a stream such as istream in C++.
if (char == ^)
{
in >> char;
if (char > initial)
{
initial = char)
}
}
我们还必须检查x
之后的char
是+
、-
还是空白。我们还要检查这个指数。