2013-12-18 114 views
3

我有2個txt文件,其中數字> 0,我必須對它們進行組合和排序。也不能有2個相同的值。合併2個文件並對它們進行排序

這裏是文件的值。 文件1:

1 
2 
3 
4 
5 
6 
7 

文件2:

1 
3 
6 
8 
10 

輸出應該是這樣的:

1 
2 
3 
4 
5 
6 
7 
8 
10 

這是我到目前爲止的代碼:

#include <stdio.h> 
#include <stdlib.h> 

int main() 
{ 
    FILE *fr1,*fr2; 
    int fst, snd, p[256], i=0, n=0; 
    bool f1=true,f2=true; 

    fr1 = fopen("txt/cisla.txt","r"); 
    fr2 = fopen("txt/cisla2.txt","r"); 

    while(feof(fr1) == 0 && feof(fr2) == 0) 
    { 

     if (f1) fscanf(fr1, "%d", &fst); 
     if (f2) fscanf(fr2, "%d", &snd); 

     printf("%d - %d\n", fst, snd); 

     if (fst == snd) 
     { 
      f1 = true; 
      f2 = true; 
      p[i] = fst; 
     } else if (fst > snd) 
     { 
      p[i] = snd; 
      f1 = false; 
      f2 = true; 
     } else 
     { 
      f2 = false; 
      f1 = true; 
      p[i] = fst; 
     } 

     i++; 

    } 

    fclose(fr1); 
    fclose(fr2); 

    printf("\n\n\n"); 

    for(int j = 0; j < i; j++) 
    { 
      printf("%d\n", p[j]); 
    } 

    return 0; 

} 

結果這是:

1 - 1 
2 - 3 
3 - 3 
4 - 6 
5 - 6 
6 - 6 
7 - 8 


1 
2 
3 
4 
5 
6 
7 

底部是數組。在頂部,我正在寫入所讀取的值。事情似乎停止在第一個文件的結尾,但我希望它繼續第二個,即使第一個文件在結尾處

+0

您的文件進行排序,對吧? – dasblinkenlight

+0

是的,他們排序 – DeiForm

+0

'系統(「貓文件1文件2>排序」);'? –

回答

2

的事情是它似乎停在第一個文件的末尾

那是因爲你告訴它這樣做 - 你有延續的條件是,無論feof小號回零:

while(feof(fr1) == 0 && feof(fr2) == 0) { 
    ... 
} 

我希望它繼續,即使第一個是在結束第二個

添加兩個循環後的第一個,寫出來的文件的「尾巴」與大要素:

while(feof(fr1) == 0 && feof(fr2) == 0) { 
    ... // Do the merge 
} 
while(feof(fr1) == 0) { 
    ... // Read from fr1, and write to the output 
} 
while(feof(fr2) == 0) { 
    ... // Read from fr2, and write to the output 
} 
+0

如果我改變它或它會崩潰,我怎麼能比較變量,然後如果我做2個不同的循環? – DeiForm

+0

@DeiForm這兩個「尾」循環出現在「合併循環」之後。我編輯了答案來說明。 – dasblinkenlight

+0

嗯,我仍然沒有得到它,第一個會像現在這樣做,然後我應該在第二個循環中做什麼? – DeiForm

0

while while循環說要繼續,而BOTH文件不在最後;我認爲只要EITHER沒有結束,就希望它繼續下去。當然,你最好千萬不要試圖從被在最後一讀...

+0

請問您是否更具體,從這我認爲我應該改變while循環從AND到OR,但我怎麼不能從已經在最後的文件讀取? – DeiForm

+0

在循環內部,您可以在閱讀文件之前檢查文件的eof。 –

0

這小小的改進可能會有所幫助。

while(feof(fr1) == 0 || feof(fr2) == 0) 
{} 

這是因爲要循環或讀取直到兩個文件都沒有完全讀取。

順便說一句,你爲什麼不能使用一些通用的容器..

+0

這會讓它崩潰,循環永遠不會結束 – DeiForm

+0

您爲什麼這麼認爲?它會繼續直到這兩個文件被完全讀取.. –

+0

,因爲我改變了它,並嘗試它 – DeiForm

0

你的問題被標記爲C++,但提供的代碼看起來並不像它。它看起來更像是C

如果您避免重新發明輪子並使用C++標準庫,那麼完成要做的事情要容易得多。

這裏是如何做到這一點使用std::vector和標準庫的一個簡單的例子:

// Open file streams for reading. 
std::ifstream fr1{"file1.txt"}; 
std::ifstream fr2{"file2.txt"}; 

// Read number tokens into a std::vector from both files. 
std::vector<int> v{std::istream_iterator<int>{fr1}, std::istream_iterator<int>{}}; 
v.insert(std::begin(v), std::istream_iterator<int>{fr2}, std::istream_iterator<int>{}); 

// Sort the vector. 
std::sort(std::begin(v), std::end(v)); 

// Remove consecutive duplicates (move them to back of vector). 
auto end = std::unique(std::begin(v), std::end(v)); 

// Remove duplicate elements. 
if (end != std::end(v)) { 
    v.erase(end, std::end(v)); 
} 

// Output vector. 
for (int i : v) { 
    std::cout << i << std::endl; 
} 

Look at this live example

相關問題