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
|
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <fstream>
#include <stdlib.h>
using namespace std;
void readLine(vector<string> &playlist);
void timeAuthorLine(const vector<string> playlist, vector<string> &author, vector<string> &name, int &totaltime, vector<int> &min, vector<int> &sec1, vector<int> &sec2);
void displayData(const vector<string> playlist, const vector<string> author, const vector<string> name, int totaltime, const vector<int> min, const vector<int> sec1, const vector<int> sec2);
int main()
{
vector<string> playlist;
readLine(playlist);
system("pause");
return 0;
}
void readLine(vector<string> &playlist)
{
vector<string> author, name;
vector<int> min, sec1, sec2;
int totaltime = 0, i = 0;
ifstream inFile("Playlist.txt");
if (inFile.fail())
{
cout << "File not found.";
exit(1);
}
else
{
while (getline(inFile, playlist[i]))
{
getline(inFile, playlist[i]);
i = i + 1;
}
inFile.close();
timeAuthorLine(playlist, author, name, totaltime, min, sec1, sec2);
displayData(playlist, author, name, totaltime, min, sec1, sec2);
}
}
void timeAuthorLine(const vector<string> playlist, vector<string> &author, vector<string> &name, int &totaltime, vector<int> &min, vector<int> &sec1, vector<int> &sec2)
{
for (int i = 0; i < playlist.size(); i++)
{
string line = playlist[i];
int index = line.find(":");
min[i] = line[index - 1] - '0';
sec1[i] = line[index + 1] - '0';
sec2[i] = line[index + 2] - '0';
totaltime = totaltime + 60 * min[i] + 10 * sec1[i] + sec2[i];
for (int j = index + 4; j < line.size(); j++)
{
author[i] = line[j];
}
for (int k = 1; k < index - 4; k++)
{
name[i] = line[k];
}
}
}
void displayData(const vector<string> playlist, const vector<string> author, const vector<string> name, int totaltime, const vector<int> min, const vector<int> sec1, const vector<int> sec2)
{
cout << playlist[0] << endl;
cout << "-----------------------------------------------------" << endl;
for (int i = 0; i < playlist.size(); i++)
{
if ((i + 1) < 10)
cout << " " << i + 1 << ". ";
else
cout << i + 1 << ". ";
cout << min[i] << ":" << sec1[i] << sec2[i] << " " << name[i] << " by " << author[i] << endl;
}
cout << "-----------------------------------------------------" << endl;
cout << "Total Playing Time = " << totaltime / 3600 << ":";
if ((totaltime % 3600) / 60 < 10)
cout << "0" << (totaltime % 3600) / 60 << ":";
else
cout << (totaltime % 3600) / 60 << ":";
if ((totaltime % 3600) % 60 < 10)
cout << "0" << (totaltime % 3600) % 60;
else
cout << (totaltime % 3600) % 60;
}
| |