2013-04-13 169 views
0

我需要合并两个文本文件,并按第三个输出文件中的“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; 
} 
+2

有一个问题,你为什么要分别对两个文件的输入进行排序?你可以将它们读入一个数组(最好是矢量)并对整个事物进行排序。 – 2013-04-13 20:12:51

+0

不要试图超载标准方法!人们可能会阅读你的代码并感到困惑! –

回答

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; 
} 
+0

我以前从未使用过载体,在此期间我一直在阅读它们。我很困惑与ostream_iterator下来,你有“排序后”。他们都在说它的未定义。我在头文件中添加了矢量和算法。我很抱歉,我只是不知道如何去解决这个问题 – JAY3

+0

@ JAY3哦,你需要'#包括'。顺便说一句,这只是一个奇特的打印方式。您可以使用for循环,并根据需要逐一打印。 – 2013-04-13 22:44:25

+0

@ JAY3我写了一些这样的代码,因为我看到了你的ostream重载和使用情况,并且认为你已经可以知道这些了,或者可以轻松搞定。 – 2013-04-13 22:46:16

0

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