2016-06-12 69 views
0

嘿傢伙,所以我有一個問題。我想讀取整數對(第一個是係數,第二個是指數),每對都是鏈表中的一個節點。我將繼續使用這些對填充鏈接列表,直到它看到換行符或在輸入文本文件中輸入密鑰。讀取整數直到文本輸入文件中的換行符

下一行,它會重新開始,所以輸入文件看起來像

-1 0 6 2 3 2 5 6 1 6 

2 5 3 2 4 2 5 7 2 7 

在那裏讀這將是兩個不同的多項式,即

  • 多項式1 = -1 + 6x^2 + 3x^2 + 5x^6 + x^6

  • 多項式2 = 2x^5 + 3x^2 + 4x^2 + 5x^7 + 2x^7

或2個不同的鏈表爲每個多項式。因爲當前的方式我有,如果我只是使用像

while (infile >> coefficient >> exponent) 
{ 
    polynomialA.listInsert(coefficient, exponent); 
} 

它會讀取兩條線,使一個很長的一個多項式。

編輯:對不起,我想我不清楚。問題是 - 如何讓ifstream繼續讀取intgers對,直到它到達文本文件中的換行符。

+1

好。你有什麼問題? –

+0

對不起,我猜我不清楚。我只是想知道如何讓ifstream繼續讀取intgers對,直到它到達文本文件中的換行符。 – user2444400

回答

1

可以分解成線,然後標記:提示:使用std::stringstreamstd::getline

std::string line; 
std::getline(infile, line) //Read the whole line 
{ 
    std::stringstream ss(line); 
    while(ss >> coefficient >> exponent) //read the pairs 
     polynomialA.listInsert(coefficient, exponent); 
} 

std::getline(infile, line) //read another whole line 
{ 
    std::stringstream ss(line); 
    while(ss >> coefficient >> exponent) //read the pairs 
     polynomialB.listInsert(coefficient, exponent); 
} 

當然,上面可以寫在一個更好的方式一起,但是,我會留給你。

相關問題