合并两个文本文件排序

Merging Two Text Files Sorted

本文关键字:文本 文件 排序 两个 合并      更新时间:2023-10-16

我需要合并两个文本文件,并在第三个输出文件上按"lastName"排序。我的代码如下,它输出胡言乱语,都在一行。我知道我的超负荷可能是愚蠢的,任何帮助都是感激的。

//header
#include <fstream>
#include <string>
#include <iostream> 
using namespace std;
struct mergedList {
  string firstName;
  string lastName;
  float gpa;
  int hours;
};
ostream& operator << (ostream& os, mergedList A) {
  os << A.firstName << "t" << A.lastName << "t" << A.gpa << "t" << A.hours;
  return os;
}
istream& operator >> (istream& is, mergedList& A) {
  is >> A.firstName >> A.lastName >> A.gpa >> A.hours;
  return is;
}
void swap(mergedList D1[], int i, int j);
void sort(mergedList D1[], int size);
int main() {
  ifstream indata;
  ifstream indata2;
  ofstream outdata;
  indata.open("merge1.txt");
  indata2.open("merge2.txt");
  outdata.open("merged.txt");
  //begin sentinel controlled loop for both lists
  mergedList D1[100];
  int index = 0;
  indata >> D1[index];
  while (!indata.eof()) {
    index++;
    indata >> D1[index];
  }
  sort(D1, index);
  mergedList D2[100];
  int index2 = 0;
  indata2 >> D2[index2];
  while (!indata2.eof()) {
    index2++;
    indata2 >> D2[index2];
  }
  sort(D2, index); {
    int i = 0, j = 0;
    while ((i < index) && (j < index2)) if (D1[i].lastName < D2[j].lastName) {
        outdata << D1[i];
        i++;
        indata >> D1[i];
      } else {
        outdata << D2[j];
        j++;
        indata2 >> D2[j];
      }
  }
  indata.close();
  indata2.close();
  outdata.close();
  return 0;
}
void swap(mergedList D1[], int i, int j) {
  mergedList temp;
  temp = D1[i];
  D1[i] = D1[j];
  D1[j] = temp;
  return;
}
void sort(mergedList D1[], int size) {
  for (int p = 1; p < size; p++) {
    for (int c = 0; c < size - p; c++) {
      if (D1[c].lastName > D1[c + 1].lastName) swap(D1, c, c + 1);
    }
  }
  return;
}

下面是一些代码。我已经尽可能多地解释了。如果你正在使用c++,你应该尝试利用你已经可用的容器和算法

struct mergedList
{
    string firstName;
    string lastName;
    float gpa;
    int hours;
};

ostream& operator <<(ostream& os, mergedList A)
{
    os << A.firstName << "t" << A.lastName << "t" << A.gpa << "t" << A.hours;
    return os;
}

istream& operator >>(istream& is, mergedList& A)
{
    is >> A.firstName >> A.lastName >> A.gpa >> A.hours;
    return is;
}
// We use this to compare two MergedList structs. i.e. by first name
// http://www.cplusplus.com/reference/algorithm/sort/ for an example
struct my_sorter {
  bool operator() (mergedList one, mergedList two) { return one.firstName < two.firstName ; }
};

int main()
{
    ifstream indata;
    ifstream indata2;
    ofstream outdata;
    indata.open("merged.txt");
    indata2.open("merged2.txt");
    outdata.open("merged.txt");
    // This can be a vector. No need for array here.
    vector<mergedList> D1;
    int index=0, index2 = 0;
    mergedList tmp;
    // You can read from streams like this if the data is formatted.
    while (indata >> tmp)
    {
        D1.push_back(tmp);
        index++;            // Maybe you need this??
    }
    // Read the second file in to the same vector.
    // You don't need another one.
    while (indata2 >> tmp)
    {
        D1.push_back(tmp);
        index2++;
    }
    cout << "Before sorting" << endl;
    copy(D1.begin(), D1.end(), ostream_iterator<mergedList>(cout, "n"));
    // Sort the vector using the std::sort algorithm.
    // http://www.cplusplus.com/reference/algorithm/sort/ for an example
    sort(D1.begin(), D1.end(), my_sorter());
    cout << "After sorting" << endl;
    copy(D1.begin(), D1.end(), ostream_iterator<mergedList>(cout, "n"));
    // Write the sorted list to the output file
    copy(D1.begin(), D1.end(), ostream_iterator<mergedList>(outdata, "n"));
    indata.close();
    indata2.close();
    outdata.close();
    return 0;
}

如果优化对您来说是一个问题,我建议您使用STL容器作为实现适当的排序方法。如果代码应该处理每个给定的输入文件,就不要使用静态数组。我还可以补充一点,您可以先合并两个文件,然后对第三个文件进行排序。