1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct student
{
string firstName, lastName;
float gpa;
int hours;
};
ostream& operator<<(ostream& os, student A)
{
os<<A.firstName<<"\t"<<A.lastName<<"\t"<<A.gpa<<"\t"<<A.hours;
return os;
}
istream& operator>>(istream& is, student& A)
{
is>>A.firstName>>A.lastName>>A.gpa>>A.hours;
return is;
}
void swap(student[], int i, int j);
void sort(student[], int size);
int main()
{
student A[100];
student B[100];
ifstream fin1, fin2;
ofstream fout;
fin1.open("merge1.txt");
int k=0;
while(fin1>>A[k])
{
k++;
}
fin1.close();
sort(A, k);
fin2.open("merge2.txt");
int j=0;
while(fin2>>B[j])
{
j++;
}
fin2.close();
sort(B, j);
fin1.open("merge1.txt");
fin2.open("merge2.txt");
fout.open("merged.txt");
int m=0;
int p=0;
while(m!=100 && p!=100)
{
if(A[m].lastName<B[p].lastName)
{
fout<<A[m]<<endl;
m++;
}
else
{
fout<<B[p]<<endl;
p++;
}
}
return 0;
}
void swap(student A[], int i, int j)
{
student temp;
temp=A[i];
A[i]=A[j];
A[j]=temp;
return;
}
void sort(student A[], int size)
{
for(int p=1; p<size; p++)
{
for(int c=0; c<size-p; c++)
{
if(A[c].lastName>A[c+1].lastName) swap(A, c, c+1);
}
}
return;
}
|