0

2 つのテキスト ファイルをマージし、3 番目の出力ファイルで "lastName" で並べ替える必要があります。私のコードは以下のとおりです。すべて1行で意味不明な出力をしています。私の過負荷がばかげているかもしれないことを理解しています。

//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;
}
4

2 に答える 2

0

ここにいくつかのコードがあります。私はできる限り説明しようとしました。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;
}
于 2013-04-13T20:31:29.903 に答える