2016-04-07 9 views
-1

を削除するには、乱数が含まれている、と私は(FILE2からのすべての番号を持つファイル1からの各番号)ファイルの両方を比較C++ 2つのファイルを比較し、tは、C++プログラムを書いてみる重複共通番号

//open and read the two file "rno1.txt" and "rno2.txt". 
    ifstream inputfile1; 
    inputfile1.open("rno1.txt"); 
    ifstream inputfile2; 
    inputfile2.open("rno2.txt"); 

    bool found = false; 
    bool F = false; 

    int CList1[20]; //save the containt of file1. 
    int CList2[20]; //save the containt of file2. 
    int i=0,j=0; 
    int n1,n2; 


    //move the containt of file1 to the array. 
    while (inputfile1 >> n1 && i < 20) 
    { 
     CList1[i]= n1; 
     i++; 
     } 

    //move the containt of file2 to the array. 
    while (inputfile2 >> n2 && j < 20) 
    { 
     CList2[j]= n2; 
     j++; 
     } 
    inputfile2.close(); 

    //comparing the common number between both files. 
    for (int x=0 ; x<20 ; x++) 
    { 
       for (int y=0 ; y<20 ; y++) 
       { 
        if (CList1[x] == CList2[y]) 
        { cout << CList1[x] << "\n" ; 
         found = true; } 
       } 
     } 


    //checking if there is common numbers between both files or not! 
    if (found == false) 
    { cout << "There are NO common number!" << "\n"; } 

    inputfile1.close(); 
    inputfile2.close(); 

問題:例えば、私は、共通の番号の印刷前に、両方のファイル間の重複の共通番号を削除したい

..

file1  file2 
    2   4 
    3   2 
    2   4 
    4   0 
    4   3 
    1   7 

共通番号は次のようになります。

2 
3 
2 
4 
4 
4 
4 

私が望む結果は次のとおりです。

2 
3 
4 
+0

ソートとマージ。 – user3528438

+1

私は宿題を臭い... –

+0

@ user3528438どうすれば使えますか? –

答えて

1
  • は、両方のリストからすべての数字を取り、配列でそれらをダンプします。
  • アレイを並べ替えます。
  • アレイをステップし、その番号が複数ある場合は、その番号のみを出力します。
+0

どのソートを使うべきですか? –

+0

デフォルトの配列並べ替えを使うことができます。 std :: sort(myvector.begin()、myvector.end()) – Auriga

+0

両方のファイルからすべての番号を追加すると、どの番号がそれらの間で共通であるかはわかりますか? –

関連する問題