Created
October 26, 2015 16:02
-
-
Save dralletje/53fd5397386511ae889d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <sstream> | |
#include <string> | |
#include <cstdlib> | |
#include <cassert> | |
using namespace std; | |
struct Length | |
{ | |
int minutes; // #minuten (0..) | |
int seconds; // #seconden (0..59) | |
}; | |
struct Track | |
{ | |
string artist; // naam van uitvoerende artiest | |
string cd; // titel van cd | |
int year; // jaar van uitgave | |
int track; // nummer van track | |
string title; // titel van track | |
string tags; // tags van track | |
Length time; // lengte van track | |
string country; // land van artiest | |
}; | |
struct TrackDisplay | |
{ | |
bool showArtist; // toont artist als true | |
bool showAlbum; // toont cd titel als true | |
bool showYear; // toont year als true | |
bool showTrack; // toont track nr als true | |
bool showTitle; // toont track titel als true | |
bool showTags; // toont tags als true | |
bool showLength; // toont track lengte als true | |
bool showCountry; // toont country als true | |
}; | |
bool match (string sub, string source) | |
{// Preconditie: | |
assert (true) ; | |
/* Postconditie: | |
Resultaat is true alleen als sub een letterlijke (case-sensitive) deelstring is van source. | |
*/ | |
return source.find(sub) != string::npos ; | |
} | |
ostream& operator<< (ostream& out, const Length lengte) { | |
out << lengte.minutes << ':' << lengte.seconds; | |
return out; | |
} | |
istream& operator>> (istream& in, Length& lengte) { | |
string alles; | |
in >> alles; | |
string minuten; | |
string seconden; | |
bool minuut = true; | |
for(int i = 0; i < alles.size(); i = i + 1) { | |
if (alles[i] == ':') { | |
minuut = false; | |
continue; | |
} | |
if (minuut) { | |
minuten = minuten + alles[i]; | |
} else { | |
seconden = seconden + alles[i]; | |
} | |
} | |
lengte.seconds = stoi(seconden); | |
lengte.minutes = stoi(minuten); | |
return in; | |
} | |
Length operator+ (const Length& a, const Length& b) { | |
assert (0 <= a.minutes && 0 <= a.seconds && a.seconds < 60 && 0 <= b.minutes && 0 <= b.seconds && b.seconds < 60) ; | |
Length newLength; | |
int tempSeconds = a.seconds + b.seconds; | |
int tempMinutes = a.minutes + b.minutes; | |
if (tempSeconds > 60) { | |
newLength.seconds = tempSeconds % 60; | |
newLength.minutes = tempMinutes + 1; | |
} else { | |
newLength.seconds = tempSeconds; | |
newLength.minutes = tempMinutes; | |
} | |
return newLength; | |
} | |
void show_track (Track lied, TrackDisplay lt) { | |
if (lt.showArtist) { | |
cout << "artist: " << lied.artist << '\n'; | |
} | |
if (lt.showAlbum) { | |
cout << "cd: " << lied.cd << '\n'; | |
} | |
if (lt.showYear) { | |
cout << "year: " << lied.year << '\n'; | |
} | |
if (lt.showTrack) { | |
cout << "track: "; | |
if (lied.track < 10) { | |
cout << "0" << lied.track; | |
} else { | |
cout << lied.track; | |
} | |
cout << '\n'; | |
} | |
if (lt.showTitle) { | |
cout << "title: " << lied.title << '\n'; | |
} | |
if (lt.showTags) { | |
cout << "tags: " << lied.tags << '\n'; | |
} | |
if (lt.showLength) { | |
cout << "time: " << lied.time << '\n'; | |
} | |
if (lt.showCountry) { | |
cout << "country: " << lied.country << '\n'; | |
} | |
} | |
int getLineInt(istream& in) { | |
string tmp; | |
getline(in, tmp); | |
return stoi(tmp); | |
} | |
void obliviate(istream& in) { | |
string oblivion; | |
getline(in, oblivion); | |
} | |
istream& operator>> (istream& in, Track& track) { | |
getline(in, track.artist); | |
if (track.artist == "") { | |
return in; | |
} | |
getline(in, track.cd); | |
track.year = getLineInt(in); | |
track.track = getLineInt(in); | |
getline(in, track.title); | |
getline(in, track.tags); | |
in >> track.time; | |
obliviate(in); | |
getline(in, track.country); | |
obliviate(in); | |
return in; | |
} | |
const int MAX_NR_OF_TRACKS = 5000; | |
typedef Track MusicDB [MAX_NR_OF_TRACKS]; | |
int main() { | |
//TrackDisplay display = {true, true, true, true, true, true, true, true}; | |
ifstream infile; | |
infile.open("Nummers.txt"); | |
if(!infile.is_open()) { | |
cout << "Opening file failed, have another try ;D" << '\n'; | |
return 1; | |
} | |
int count = 0; | |
for(int i = 0; i < MAX_NR_OF_TRACKS; i = i + 1) { | |
Track track; | |
infile >> track; | |
if (track.artist == "") { | |
break; | |
} | |
count = count + 1; | |
} | |
cout << "Read " << count << " entries" << '\n'; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment