2015-11-29 149 views
-3

如何使用数组创建多项式函数?基本上我想显示多项式(例如x^2 + 3x + 5)。然后,我想显示最高多项式的程度。例如,x^2 + 3x + 5 = 2(最高度)。我需要一些多项式帮助

+0

你用什么代码来表达你的程序中的多项式,'Math.Pow()'我想因为它是C#?让我们看看你的代码... –

+0

是的,我想到了使用Math.Pow()和一个将填充指数部分的循环。 – CharleBarkely123

+0

我一直在尝试使用数组创建多项式。 – CharleBarkely123

回答

1
// 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) 
    } 
} 

我们还必须检查charx后是+-,或空白。我们也想检查那个指数。

1

我可能会推荐这样做。

 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; 

你将不得不首先把你的多项式变成一个字符串。