c++比较两个文件并删除重复的公共编号

c++ comparing two file and remove the duplicate common number

本文关键字:删除 编号 文件 比较 两个 c++      更新时间:2023-10-16

t尝试编写一个c++程序,该程序获取两个包含随机数的文件,并比较两个文件(文件1中的每个数字与文件2中的所有数字)

//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
  • 取两个列表中的所有数字,将它们转储到一个数组中
  • 对数组进行排序
  • 对数组进行步进处理,只有当数组不止一个时才打印出数字