2014-10-19 35 views
-2

這段代碼應該輸出2個不同多項式的最大系數,但是如果第一個大於第二個,它將無法正確編譯。因此,如果第一個多項式是1 - 2x + 4x^3並且第二個多項式是-x + 5x^2 - 3x^6,那麼第一個多項式是1 - 2 + 4x^3。由於第二個多項式大於第一個,它將起作用。比較兩個多項式與矢量的係數C++

它張貼「向量標超出範圍」錯誤每當第一多項式比第二

class Polynomial { 
public: 
Polynomial(); 
Polynomial(const vector<int>& coeffs); 

int Degree() const; 
int Coefficient(int k) const; 
void print() const; 
private: 
vector<int>coefficient; 

int main(){ 
//Variable and vector for inputs 
vector<int> coefficient; 
int input = 0; 
//Welcome message 
cout << "Welcome! Please input the coefficients of the first polynomial p(x).\nWhen you are finished, enter -12345.\n"; 

//While loop - if input isn't -12345, put the input into coefficient. 
while (input != -12345){ 
    cin >> input; 
    coefficient.push_back(input); 
} 

//Deletes -12345 
coefficient.pop_back(); 

//Puts coefficient values into constructor 
Polynomial first(coefficient); 

//Prints first polynomial 
cout << "\nYour first polynomial is p(x) = "; 
first.print(); 

//Prints degrees of first polynomial 
cout << ".\np(x) has degree " << first.Degree(); 
int degree1 = first.Degree(); 

//Prints transformation of first polynomial 
cout << ".\nThe transform of p(x) is "; 
first.Transform(); 

//clears the values in coefficient for second polynomial inputs. 
coefficient.clear(); 

//Inputs the second polynomial's coefficients. 
cout << ".\n\nPlease input the coefficients of the second polynomial q(x).\n"; 
//Had to use do-while because while loop wouldn't work. 
do { 
    cin >> input; 
    coefficient.push_back(input); 

} while (input != -12345); 

//Deletes -12345 
coefficient.pop_back(); 

//Puts coefficients into second polynomial 
Polynomial second(coefficient); 

//Prints second polynomial 
cout << "\nYour second polynomial is q(x) = "; 
second.print(); 

cout << ".\nq(x) has degree " << second.Degree(); 
int degree2 = second.Degree(); 

if (degree1 > degree2){ 
    cout << ".\n\nThe coefficient of x^" << degree1 << " in p(x) is " << first.Coefficient(degree1); 
    cout << ".\nThe coefficient of x^" << degree1 << " in q(x) is " << second.Coefficient(degree1); 
} 
else{ 
    cout << ".\n\nThe coefficient of x^" << degree2 << " in p(x) is " << first.Coefficient(degree2); 
    cout << ".\nThe coefficient of x^" << degree2 << " in q(x) is " << second.Coefficient(degree2); 
} 

int Polynomial::Degree() const{ 
int number = 0; 
for (size_t i = 0, size = coefficient.size(); i < size; i++){ 
    if (coefficient[i] != 0) 
     number = i; 
} 
return number; 
} 

int Polynomial::Coefficient(int k) const{ 
if (coefficient[k] != 0) 
    return coefficient[k]; 
else 
    return 0; 
} 
+0

無不是這個代碼替換功能 – P0W 2014-10-19 03:56:59

+0

請創建[最低工作示例](https://stackoverflow.com/help/mcve)和張貼con克里特島錯誤你得到。 「不編譯」太模糊。而且,在這種情況下它沒有任何意義,因爲程序編譯是否不能依賴運行時值。您發佈的代碼總是返回「係數[k]」,並不以任何方式選擇「更大系數」(它甚至不會比較任何內容)。 'if'語句完全沒用,除非比較運算符'!='對於任何類型的'係數[k]'都是奇怪的超載。 – Oguk 2014-10-19 04:04:38

+0

對不起,這裏還有一些新的字眼問題。更多信息:如果第一個多項式是1-2x + 4x^3,第二個多項式是-x + 5x^2-3x^6。最高度是6,所以第一個多項式的係數是0,而第二個多項式的係數是-3。 每當我運行第一個多項式的代碼時,我會得到一個「向量下標超出範圍」的錯誤消息。 – stevenctran 2014-10-19 04:27:39

回答

0

假設你coefficient矢量較大僅運行至多項式的最高次冪的指數(即它有4個元素,如果x^3是最高功率),試圖通過

int Polynomial::Coefficient(int k) const{ 
    if (k < coefficient.size()) 
     return coefficient[k]; 
    else 
     return 0; 
}