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 116
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
struct album
{
string albumN,year,songL[30],song[30];
int c;
};
void sSort(string ar[], string arL[], int elems)
{
int swaps = 1,j;
while(swaps)
{
swaps = 0;
for(j = 0; j < elems-1; j++)
{
if(arL[j].compare(arL[j+1]) == 1)
{
swap(arL[j],arL[j+1]);
swap(ar[j],ar[j+1]);
swaps = 1;
}
}
}
}
void sort_albums( struct album beatles[], int c)
{
int swaps = 1,j;
while(swaps)
{
swaps = 0;
for(j = 0; j < c-1; j++)
{
if(beatles[j].albumN.compare(beatles[j+1].albumN) == 1)
{
swap(beatles[j],beatles[j+1]);
swaps = 1;
}
}
}
}
int main ()
{
const int size = 5;
string line,ar[200];
struct album beatles[size];
int len,i = 0,x = 0;
ifstream beatlesData ("beatles.c"); //opening the file.
if (beatlesData.is_open()) // if the file is open
{
while (!beatlesData.eof()) // while the end of file is NOT reached
{
for(x = 0; x < size; x++)
{
// get album name
getline(beatlesData,line);
beatles[x].albumN = line;
// get album year
getline(beatlesData,line);
beatles[x].year = line;
beatles[x].c = 0;
// get the first song
getline(beatlesData,line);
// get the rest of the songs
// get album songs
do
{
len = line.length();
beatles[x].song[i] = line;
beatles[x].songL[i] = beatles[x].song[i].substr(4,len);
beatles[x].c++;
getline(beatlesData,line);
i++;
} while(line[0] != '=' && !beatlesData.eof());
//sort songs
sSort(beatles[x].song, beatles[x].songL, i);
// reset the number of songs to 0
i = 0;
}
}
sort_albums(beatles, 5);
for(int y = 0; y < size; y++)
{
cout << beatles[y].albumN << endl;
cout << beatles[y].year << endl;
for(int x = 0; x < beatles[y].c; x++)
{
cout << beatles[y].song[x] << endl;
}
cout << "==============================" << endl;
}
beatlesData.close(); //closing the file
}
else cout << "Unable to open file"; //if the file is not open output
return 0;
}
|