2015-11-29 138 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; 

你將不得不首先把你的多項式變成一個字符串。