Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Lawrence Lai
- // laitcl
- // ENGR 101, Section #103, James Huang
- // Project6
- // 10/29/2010
- // Project6.cpp
- // The Mp3 Player Program
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <cassert>
- #include "sort.h"
- using namespace std;
- //My own routines
- void sortbytitle(vector <string> & songTitles, vector <string> & songArtists, vector <int> & songRatings, bool ascending)
- {
- int n = songTitles.size();
- int k;
- vector <string> songTitlesNew(0);
- vector <string> songArtistsNew(0);
- vector <int> songRatingsNew(0);
- songTitlesNew.push_back(songTitles.at(0));
- songArtistsNew.push_back(songArtists.at(0));
- songRatingsNew.push_back(songRatings.at(0));
- for (int i = 1; i < n; i++)
- {
- k = GetPos(songTitlesNew, songTitles.at(i), ascending);
- songTitlesNew.insert(songTitlesNew.begin() + k,songTitles.at(i));
- songArtistsNew.insert(songArtistsNew.begin() + k,songArtists.at(i));
- songRatingsNew.insert(songRatingsNew.begin() + k,songRatings.at(i));
- }
- songTitles = songTitlesNew;
- songArtists = songArtistsNew;
- songRatings = songRatingsNew;
- return;
- }
- void sortbyartist(vector <string> & songTitles, vector <string> & songArtists, vector <int> & songRatings, bool ascending)
- {
- int n = songTitles.size();
- int k;
- vector <string> songTitlesNew(0);
- vector <string> songArtistsNew(0);
- vector <int> songRatingsNew(0);
- songTitlesNew.push_back(songTitles.at(0));
- songArtistsNew.push_back(songArtists.at(0));
- songRatingsNew.push_back(songRatings.at(0));
- for (int i = 1; i < n; i++)
- {
- k = GetPos(songArtistsNew, songArtists.at(i), ascending);
- songTitlesNew.insert(songTitlesNew.begin() + k,songTitles.at(i));
- songArtistsNew.insert(songArtistsNew.begin() + k,songArtists.at(i));
- songRatingsNew.insert(songRatingsNew.begin() + k,songRatings.at(i));
- }
- songTitles = songTitlesNew;
- songArtists = songArtistsNew;
- songRatings = songRatingsNew;
- return;
- }
- void sortbyrating(vector <string> & songTitles, vector <string> & songArtists, vector <int> & songRatings, bool ascending)
- {
- int n = songTitles.size();
- int k;
- vector <string> songTitlesNew(0);
- vector <string> songArtistsNew(0);
- vector <int> songRatingsNew(0);
- songTitlesNew.push_back(songTitles.at(0));
- songArtistsNew.push_back(songArtists.at(0));
- songRatingsNew.push_back(songRatings.at(0));
- for (int i = 1; i < n; i++)
- {
- k = GetPos(songRatingsNew, songRatings.at(i), ascending);
- songTitlesNew.insert(songTitlesNew.begin() + k,songTitles.at(i));
- songArtistsNew.insert(songArtistsNew.begin() + k,songArtists.at(i));
- songRatingsNew.insert(songRatingsNew.begin() + k,songRatings.at(i));
- }
- songTitles = songTitlesNew;
- songArtists = songArtistsNew;
- songRatings = songRatingsNew;
- return;
- }
- void searchTitles(vector <string> & songTitles, vector <string> & songArtists, vector <int> & songRatings, string search)
- {
- vector <string> matchingSongs;
- vector <string> matchingArtists;
- vector <int> matchingRatings;
- int n = songTitles.size();
- for (int i = 0; i<n; i++)
- {
- if (songTitles.at(i) == search)
- {
- matchingSongs.push_back(songTitles.at(i));
- matchingArtists.push_back(songArtists.at(i));
- matchingRatings.push_back(songRatings.at(i));
- }
- }
- songTitles = matchingSongs;
- songArtists = matchingArtists;
- songRatings = matchingRatings;
- return;
- }
- void searchArtists(vector <string> & songTitles, vector <string> & songArtists, vector <int> & songRatings, string search)
- {
- vector <string> matchingSongs;
- vector <string> matchingArtists;
- vector <int> matchingRatings;
- int n = songTitles.size();
- for (int i = 0; i<n; i++)
- {
- if (songArtists.at(i) == search)
- {
- matchingSongs.push_back(songTitles.at(i));
- matchingArtists.push_back(songArtists.at(i));
- matchingRatings.push_back(songRatings.at(i));
- }
- }
- songTitles = matchingSongs;
- songArtists = matchingArtists;
- songRatings = matchingRatings;
- return;
- }
- void searchRatings(vector <string> & songTitles, vector <string> & songArtists, vector <int> & songRatings, int search)
- {
- vector <string> matchingSongs;
- vector <string> matchingArtists;
- vector <int> matchingRatings;
- int n = songTitles.size();
- for (int i = 0; i<n; i++)
- {
- if (songRatings.at(i) == search)
- {
- matchingSongs.push_back(songTitles.at(i));
- matchingArtists.push_back(songArtists.at(i));
- matchingRatings.push_back(songRatings.at(i));
- }
- }
- songTitles = matchingSongs;
- songArtists = matchingArtists;
- songRatings = matchingRatings;
- return;
- }
- /* REQUIRED FUNCTIONS */
- /* This procedure will sort the values of the three input vectors depending on
- * the last two arguments using insertion sort.
- *
- * The first three arguments passed to the procedure should be vectors containing
- * song titles with corresponding artists and song ratings. The information stored
- * in the Xth location of each vector corresponds to the complete song information
- * for the Xth song in the library. Therefore, when sorting the vectors, you need
- * to ensure that the song information stays consistent. For example, if you
- * change the Xth song to the Yth song, you must make sure to update the indices in
- * all three vectors to reflect this change. The fourth argument specifies which of
- * the previous 3 vector values to sort by, and the final argument specifies
- * whether to sort by ascending or descending order.
- *
- * The sortBy argument translates to the following:
- * 0 -> sort by song title,
- * 1 -> sort by artist name,
- * 2 -> sort by rating
- * The ascending argument translates to the following:
- * true -> sort ascending,
- * false -> sort descending
- *
- * If the lengths of the three vectors are not equal, sort should exit immediately
- * with an error.
- *
- * This procedure modifies the ordering of the vectors accordingly, and the vectors
- * should be in sorted order upon the procedure's completion (according to the
- * description noted in the support functions section of the spec).
- */
- void sort(vector <string> & songTitles, vector <string> & songArtists, vector <int> & songRatings, int sortBy, bool ascending)
- {
- assert(songTitles.size() == songArtists.size());
- assert(songRatings.size() == songArtists.size());
- if (sortBy == 0)
- {
- sortbytitle(songTitles, songArtists, songRatings, ascending);
- }
- else if (sortBy == 1)
- {
- sortbyartist(songTitles, songArtists, songRatings, ascending);
- }
- else if (sortBy == 2)
- {
- sortbyrating(songTitles, songArtists, songRatings, ascending);
- }
- else
- {
- cout << "invalid sort option" << endl;
- }
- return;
- }
- /* This procedure merges two playlists together omitting any duplicate songs that
- * are encountered in the second list. The actual merge is implemented by appending
- * all the songs in the second list to the end of the first list in the order that
- * they appear in the second list omitting any encountered duplicates.
- *
- * The first 3 inputs are vectors representing the songs in the first playlist.
- * The next 3 inputs are vectors representing the songs in the second playlist.
- *
- * The procedure must save the final merged playlist into the first three input
- * vectors.
- */
- void merge(vector <string> & songs1, vector <string> & artists1, vector <int> & ratings1, vector <string> & songs2, vector <string> & artists2, vector <int> & ratings2)
- {
- int n;
- int k;
- n = songs2.size();
- for (int i=0; i<n; i++)
- {
- bool duplicate=false;
- k = songs1.size();
- for (int j=0; j<k; j++)
- {
- if (songs2.at(i) == songs1.at(j))
- {
- if (artists2.at(i) == artists1.at(j))
- {
- duplicate = true;
- }
- }
- }
- if (!duplicate)
- {
- songs1.push_back(songs2.at(i));
- artists1.push_back(artists2.at(i));
- ratings1.push_back(ratings2.at(i));
- }
- }
- return;
- }
- /* This function constructs and returns an mp3 title string for the song
- * when passed a song name as a string, the artist name as a string, and the
- * rating as an int. The function returns artistname-songname-songrating.mp3 as a
- * string (e.g. jimi_hendrix-hey_joe-4.mp3)
- */
- string convertName(string name, string artist, int rating)
- {
- name = artist+"-"+name+"-"+ratingToChar(rating)+".mp3";
- return name;
- }
- /* Given command and argument strings taken from a line in the playlist script,
- * this procedure will perform the necessary transformation on the input playlist.
- *
- * The first 3 inputs to the procedure are vectors representing all the songs in
- * the user's library.
- * The next 3 inputs are vectors representing the songs in the current playlist.
- * The next string input, called selector, corresponds to a command from the script
- * file and the last string input, called argument, is the argument that
- * accompanied the previous command in the script file.
- *
- * This procedure must modify the 3 current playlist vectors to perform a single
- * action specified by the selector/argument pair. Recall that you can convert a
- * C-string to an int using the function atoi() - you will need this for certain
- * actions taken in this procedure.
- */
- void performAction(vector <string> & allSongs, vector <string> & allArtists, vector <int> & allRatings, vector <string> & plSongs, vector <string> & plArtists, vector <int> & plRatings, string selector, string argument)
- {
- int i;
- if (selector == "sortasc")
- {
- i = atoi(argument.c_str());
- sort(plSongs, plArtists, plRatings, i, true) ;
- }
- else if (selector == "sortdesc")
- {
- i = atoi(argument.c_str());
- sort(plSongs, plArtists, plRatings, i, false);
- }
- else if (selector == "select_songs_with_rating_of")
- {
- i = atoi(argument.c_str());
- vector <string> allSongsNew = allSongs;
- vector <string> allArtistsNew = allArtists;
- vector <int> allRatingsNew = allRatings;
- searchRatings(allSongsNew, allArtistsNew, allRatingsNew, i);
- assert(allSongsNew.size() == allArtistsNew.size());
- assert(allRatingsNew.size() == allArtistsNew.size());
- merge(plSongs, plArtists, plRatings, allSongsNew, allArtistsNew, allRatingsNew);
- }
- else if (selector == "select_all_by")
- {
- vector <string> allSongsNew = allSongs;
- vector <string> allArtistsNew = allArtists;
- vector <int> allRatingsNew = allRatings;
- searchArtists(allSongsNew, allArtistsNew, allRatingsNew, argument);
- assert(allSongsNew.size() == allArtistsNew.size());
- assert(allRatingsNew.size() == allArtistsNew.size());
- merge(plSongs, plArtists, plRatings, allSongsNew, allArtistsNew, allRatingsNew);
- }
- else if (selector == "select_songs_named")
- {
- vector <string> allSongsNew = allSongs;
- vector <string> allArtistsNew = allArtists;
- vector <int> allRatingsNew = allRatings;
- searchTitles(allSongsNew, allArtistsNew, allRatingsNew, argument);
- assert(allSongsNew.size() == allArtistsNew.size());
- assert(allRatingsNew.size() == allArtistsNew.size());
- merge(plSongs, plArtists, plRatings, allSongsNew, allArtistsNew, allRatingsNew);
- }
- else if(selector == "output_to")
- {
- ofstream out(argument.c_str());
- PrintList(plSongs, plArtists, plRatings, true, out);
- plSongs.clear();
- plArtists.clear();
- plRatings.clear();
- }
- else if(selector == "convert_and_output_to")
- {
- ofstream out(argument.c_str());
- ConvertAndPrint(plSongs, plArtists, plRatings, true, out);
- plSongs.clear();
- plArtists.clear();
- plRatings.clear();
- }
- return;
- }
- /* This procedure reads through the entire user script file, and creates playlists
- * as specified.
- *
- * The first 3 inputs to the procedure are vectors representing all the songs in
- * the user's library.
- * The next 3 inputs are vectors representing the songs in the current playlist.
- * The last input is a reference to the ifstream connected to the scriptfile,
- *
- * This procedure must create all the playlists (and output them to the correct
- * files) that the script file specifies.
- */
- void generatePlaylists(vector <string> & allSongs, vector <string> & allArtists, vector <int> & allRatings, vector <string> & plSongs, vector <string> & plArtists, vector <int> & plRatings, ifstream & in)
- {
- string selector;
- string argument;
- in >> selector >> argument;
- while (!in.fail())
- {
- performAction(allSongs, allArtists, allRatings, plSongs, plArtists, plRatings, selector, argument);
- in >> selector >> argument;
- }
- return;
- }
- // Write your definition of populateVectors() here
- void populateVectors(vector <string> & allSongs, vector <string> & allArtists, vector <int> & allRatings, ifstream & inFile)
- {
- string newsong;
- string newartist;
- int newrating;
- bool duplicate;
- inFile >> newsong >> newartist >> newrating;
- while (!inFile.fail())
- {
- int n = allSongs.size();
- for (int i = 0; i<n; i++)
- {
- if (allSongs.at(i) == newsong)
- {
- if (allArtists.at(i) == newartist)
- {
- duplicate = 1;
- }
- }
- }
- if(!duplicate)
- {
- allSongs.push_back(newsong);
- allArtists.push_back(newartist);
- allRatings.push_back(newrating);
- }
- inFile >> newsong >> newartist >> newrating;
- }
- assert(allSongs.size() == allArtists.size());
- assert(allRatings.size() == allArtists.size());
- return;
- }
- // ------------------ Main Function ------------------------------
- /* Main Function for custom playlist creator (THIS FUNCTION IS COMPLETE) */
- int main(){
- //set up variables
- string inputFile = "library.txt";
- string transformFile = "scriptfile.txt";
- string artist, songName;
- int rating;
- vector<string> allSongs, allArtists; // songs and corresponding artists in library
- vector<int> allRatings; // corresponding ratings for library songs
- vector<string> plSongs, plArtists; // songs and corresponding artists in playlist
- vector<int> plRatings; // corresponding ratings for playlist songs
- ifstream inFile(inputFile.c_str());
- ifstream tFile(transformFile.c_str());
- //read the playlist in from file and place them in vectors (for task 1)
- populateVectors(allSongs, allArtists, allRatings, inFile);
- //run transformations from file (for task 3)
- generatePlaylists(allSongs, allArtists, allRatings, plSongs, plArtists, plRatings, tFile);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement