Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.22 KB | None | 0 0
  1. // Lawrence Lai
  2.  
  3. // laitcl
  4.  
  5. // ENGR 101, Section #103, James Huang
  6.  
  7. // Project6
  8.  
  9. // 10/29/2010
  10.  
  11. // Project6.cpp
  12.  
  13. // The Mp3 Player Program
  14.  
  15. #include <iostream>
  16. #include <fstream>
  17. #include <string>
  18. #include <vector>
  19. #include <cassert>
  20.  
  21. #include "sort.h"
  22.  
  23. using namespace std;
  24.  
  25. //My own routines
  26.  
  27. void sortbytitle(vector <string> & songTitles, vector <string> & songArtists, vector <int> & songRatings, bool ascending)
  28. {
  29.  
  30.  
  31. int n = songTitles.size();
  32. int k;
  33. vector <string> songTitlesNew(0);
  34. vector <string> songArtistsNew(0);
  35. vector <int> songRatingsNew(0);
  36.  
  37. songTitlesNew.push_back(songTitles.at(0));
  38. songArtistsNew.push_back(songArtists.at(0));
  39. songRatingsNew.push_back(songRatings.at(0));
  40.  
  41.  
  42. for (int i = 1; i < n; i++)
  43. {
  44.  
  45. k = GetPos(songTitlesNew, songTitles.at(i), ascending);
  46. songTitlesNew.insert(songTitlesNew.begin() + k,songTitles.at(i));
  47. songArtistsNew.insert(songArtistsNew.begin() + k,songArtists.at(i));
  48. songRatingsNew.insert(songRatingsNew.begin() + k,songRatings.at(i));
  49.  
  50.  
  51. }
  52.  
  53.  
  54. songTitles = songTitlesNew;
  55. songArtists = songArtistsNew;
  56. songRatings = songRatingsNew;
  57.  
  58. return;
  59. }
  60.  
  61.  
  62. void sortbyartist(vector <string> & songTitles, vector <string> & songArtists, vector <int> & songRatings, bool ascending)
  63. {
  64.  
  65.  
  66. int n = songTitles.size();
  67. int k;
  68. vector <string> songTitlesNew(0);
  69. vector <string> songArtistsNew(0);
  70. vector <int> songRatingsNew(0);
  71.  
  72. songTitlesNew.push_back(songTitles.at(0));
  73. songArtistsNew.push_back(songArtists.at(0));
  74. songRatingsNew.push_back(songRatings.at(0));
  75.  
  76.  
  77. for (int i = 1; i < n; i++)
  78. {
  79.  
  80. k = GetPos(songArtistsNew, songArtists.at(i), ascending);
  81. songTitlesNew.insert(songTitlesNew.begin() + k,songTitles.at(i));
  82. songArtistsNew.insert(songArtistsNew.begin() + k,songArtists.at(i));
  83. songRatingsNew.insert(songRatingsNew.begin() + k,songRatings.at(i));
  84.  
  85.  
  86. }
  87.  
  88.  
  89. songTitles = songTitlesNew;
  90. songArtists = songArtistsNew;
  91. songRatings = songRatingsNew;
  92.  
  93. return;
  94. }
  95.  
  96.  
  97. void sortbyrating(vector <string> & songTitles, vector <string> & songArtists, vector <int> & songRatings, bool ascending)
  98. {
  99.  
  100.  
  101. int n = songTitles.size();
  102. int k;
  103. vector <string> songTitlesNew(0);
  104. vector <string> songArtistsNew(0);
  105. vector <int> songRatingsNew(0);
  106.  
  107. songTitlesNew.push_back(songTitles.at(0));
  108. songArtistsNew.push_back(songArtists.at(0));
  109. songRatingsNew.push_back(songRatings.at(0));
  110.  
  111.  
  112. for (int i = 1; i < n; i++)
  113. {
  114.  
  115. k = GetPos(songRatingsNew, songRatings.at(i), ascending);
  116. songTitlesNew.insert(songTitlesNew.begin() + k,songTitles.at(i));
  117. songArtistsNew.insert(songArtistsNew.begin() + k,songArtists.at(i));
  118. songRatingsNew.insert(songRatingsNew.begin() + k,songRatings.at(i));
  119.  
  120.  
  121. }
  122.  
  123.  
  124. songTitles = songTitlesNew;
  125. songArtists = songArtistsNew;
  126. songRatings = songRatingsNew;
  127.  
  128. return;
  129. }
  130.  
  131. void searchTitles(vector <string> & songTitles, vector <string> & songArtists, vector <int> & songRatings, string search)
  132. {
  133.  
  134.  
  135. vector <string> matchingSongs;
  136. vector <string> matchingArtists;
  137. vector <int> matchingRatings;
  138.  
  139. int n = songTitles.size();
  140.  
  141. for (int i = 0; i<n; i++)
  142. {
  143. if (songTitles.at(i) == search)
  144. {
  145. matchingSongs.push_back(songTitles.at(i));
  146. matchingArtists.push_back(songArtists.at(i));
  147. matchingRatings.push_back(songRatings.at(i));
  148. }
  149. }
  150.  
  151. songTitles = matchingSongs;
  152. songArtists = matchingArtists;
  153. songRatings = matchingRatings;
  154. return;
  155. }
  156.  
  157. void searchArtists(vector <string> & songTitles, vector <string> & songArtists, vector <int> & songRatings, string search)
  158. {
  159.  
  160.  
  161. vector <string> matchingSongs;
  162. vector <string> matchingArtists;
  163. vector <int> matchingRatings;
  164.  
  165. int n = songTitles.size();
  166.  
  167. for (int i = 0; i<n; i++)
  168. {
  169. if (songArtists.at(i) == search)
  170. {
  171. matchingSongs.push_back(songTitles.at(i));
  172. matchingArtists.push_back(songArtists.at(i));
  173. matchingRatings.push_back(songRatings.at(i));
  174. }
  175. }
  176. songTitles = matchingSongs;
  177. songArtists = matchingArtists;
  178. songRatings = matchingRatings;
  179. return;
  180. }
  181.  
  182. void searchRatings(vector <string> & songTitles, vector <string> & songArtists, vector <int> & songRatings, int search)
  183. {
  184. vector <string> matchingSongs;
  185. vector <string> matchingArtists;
  186. vector <int> matchingRatings;
  187.  
  188. int n = songTitles.size();
  189.  
  190. for (int i = 0; i<n; i++)
  191. {
  192. if (songRatings.at(i) == search)
  193. {
  194. matchingSongs.push_back(songTitles.at(i));
  195. matchingArtists.push_back(songArtists.at(i));
  196. matchingRatings.push_back(songRatings.at(i));
  197. }
  198. }
  199. songTitles = matchingSongs;
  200. songArtists = matchingArtists;
  201. songRatings = matchingRatings;
  202.  
  203. return;
  204. }
  205.  
  206.  
  207. /* REQUIRED FUNCTIONS */
  208.  
  209. /* This procedure will sort the values of the three input vectors depending on
  210. * the last two arguments using insertion sort.
  211. *
  212. * The first three arguments passed to the procedure should be vectors containing
  213. * song titles with corresponding artists and song ratings. The information stored
  214. * in the Xth location of each vector corresponds to the complete song information
  215. * for the Xth song in the library. Therefore, when sorting the vectors, you need
  216. * to ensure that the song information stays consistent. For example, if you
  217. * change the Xth song to the Yth song, you must make sure to update the indices in
  218. * all three vectors to reflect this change. The fourth argument specifies which of
  219. * the previous 3 vector values to sort by, and the final argument specifies
  220. * whether to sort by ascending or descending order.
  221. *
  222. * The sortBy argument translates to the following:
  223. * 0 -> sort by song title,
  224. * 1 -> sort by artist name,
  225. * 2 -> sort by rating
  226. * The ascending argument translates to the following:
  227. * true -> sort ascending,
  228. * false -> sort descending
  229. *
  230. * If the lengths of the three vectors are not equal, sort should exit immediately
  231. * with an error.
  232. *
  233. * This procedure modifies the ordering of the vectors accordingly, and the vectors
  234. * should be in sorted order upon the procedure's completion (according to the
  235. * description noted in the support functions section of the spec).
  236. */
  237. void sort(vector <string> & songTitles, vector <string> & songArtists, vector <int> & songRatings, int sortBy, bool ascending)
  238. {
  239.  
  240. assert(songTitles.size() == songArtists.size());
  241. assert(songRatings.size() == songArtists.size());
  242.  
  243. if (sortBy == 0)
  244. {
  245. sortbytitle(songTitles, songArtists, songRatings, ascending);
  246. }
  247. else if (sortBy == 1)
  248. {
  249. sortbyartist(songTitles, songArtists, songRatings, ascending);
  250. }
  251. else if (sortBy == 2)
  252. {
  253. sortbyrating(songTitles, songArtists, songRatings, ascending);
  254. }
  255. else
  256. {
  257. cout << "invalid sort option" << endl;
  258. }
  259.  
  260. return;
  261. }
  262.  
  263.  
  264.  
  265. /* This procedure merges two playlists together omitting any duplicate songs that
  266. * are encountered in the second list. The actual merge is implemented by appending
  267. * all the songs in the second list to the end of the first list in the order that
  268. * they appear in the second list omitting any encountered duplicates.
  269. *
  270. * The first 3 inputs are vectors representing the songs in the first playlist.
  271. * The next 3 inputs are vectors representing the songs in the second playlist.
  272. *
  273. * The procedure must save the final merged playlist into the first three input
  274. * vectors.
  275. */
  276. void merge(vector <string> & songs1, vector <string> & artists1, vector <int> & ratings1, vector <string> & songs2, vector <string> & artists2, vector <int> & ratings2)
  277. {
  278. int n;
  279. int k;
  280. n = songs2.size();
  281.  
  282. for (int i=0; i<n; i++)
  283. {
  284. bool duplicate=false;
  285. k = songs1.size();
  286.  
  287. for (int j=0; j<k; j++)
  288. {
  289. if (songs2.at(i) == songs1.at(j))
  290. {
  291. if (artists2.at(i) == artists1.at(j))
  292. {
  293. duplicate = true;
  294. }
  295. }
  296. }
  297. if (!duplicate)
  298. {
  299. songs1.push_back(songs2.at(i));
  300. artists1.push_back(artists2.at(i));
  301. ratings1.push_back(ratings2.at(i));
  302. }
  303.  
  304. }
  305. return;
  306. }
  307.  
  308.  
  309. /* This function constructs and returns an mp3 title string for the song
  310. * when passed a song name as a string, the artist name as a string, and the
  311. * rating as an int. The function returns artistname-songname-songrating.mp3 as a
  312. * string (e.g. jimi_hendrix-hey_joe-4.mp3)
  313. */
  314. string convertName(string name, string artist, int rating)
  315. {
  316. name = artist+"-"+name+"-"+ratingToChar(rating)+".mp3";
  317.  
  318. return name;
  319. }
  320.  
  321.  
  322. /* Given command and argument strings taken from a line in the playlist script,
  323. * this procedure will perform the necessary transformation on the input playlist.
  324. *
  325. * The first 3 inputs to the procedure are vectors representing all the songs in
  326. * the user's library.
  327. * The next 3 inputs are vectors representing the songs in the current playlist.
  328. * The next string input, called selector, corresponds to a command from the script
  329. * file and the last string input, called argument, is the argument that
  330. * accompanied the previous command in the script file.
  331. *
  332. * This procedure must modify the 3 current playlist vectors to perform a single
  333. * action specified by the selector/argument pair. Recall that you can convert a
  334. * C-string to an int using the function atoi() - you will need this for certain
  335. * actions taken in this procedure.
  336. */
  337. void performAction(vector <string> & allSongs, vector <string> & allArtists, vector <int> & allRatings, vector <string> & plSongs, vector <string> & plArtists, vector <int> & plRatings, string selector, string argument)
  338. {
  339. int i;
  340.  
  341. if (selector == "sortasc")
  342. {
  343. i = atoi(argument.c_str());
  344. sort(plSongs, plArtists, plRatings, i, true) ;
  345. }
  346. else if (selector == "sortdesc")
  347. {
  348. i = atoi(argument.c_str());
  349. sort(plSongs, plArtists, plRatings, i, false);
  350. }
  351. else if (selector == "select_songs_with_rating_of")
  352. {
  353. i = atoi(argument.c_str());
  354. vector <string> allSongsNew = allSongs;
  355. vector <string> allArtistsNew = allArtists;
  356. vector <int> allRatingsNew = allRatings;
  357.  
  358.  
  359. searchRatings(allSongsNew, allArtistsNew, allRatingsNew, i);
  360.  
  361. assert(allSongsNew.size() == allArtistsNew.size());
  362. assert(allRatingsNew.size() == allArtistsNew.size());
  363.  
  364. merge(plSongs, plArtists, plRatings, allSongsNew, allArtistsNew, allRatingsNew);
  365. }
  366.  
  367. else if (selector == "select_all_by")
  368. {
  369. vector <string> allSongsNew = allSongs;
  370. vector <string> allArtistsNew = allArtists;
  371. vector <int> allRatingsNew = allRatings;
  372.  
  373. searchArtists(allSongsNew, allArtistsNew, allRatingsNew, argument);
  374.  
  375. assert(allSongsNew.size() == allArtistsNew.size());
  376. assert(allRatingsNew.size() == allArtistsNew.size());
  377.  
  378. merge(plSongs, plArtists, plRatings, allSongsNew, allArtistsNew, allRatingsNew);
  379. }
  380. else if (selector == "select_songs_named")
  381. {
  382. vector <string> allSongsNew = allSongs;
  383. vector <string> allArtistsNew = allArtists;
  384. vector <int> allRatingsNew = allRatings;
  385.  
  386. searchTitles(allSongsNew, allArtistsNew, allRatingsNew, argument);
  387.  
  388. assert(allSongsNew.size() == allArtistsNew.size());
  389. assert(allRatingsNew.size() == allArtistsNew.size());
  390.  
  391. merge(plSongs, plArtists, plRatings, allSongsNew, allArtistsNew, allRatingsNew);
  392. }
  393. else if(selector == "output_to")
  394. {
  395. ofstream out(argument.c_str());
  396. PrintList(plSongs, plArtists, plRatings, true, out);
  397. plSongs.clear();
  398. plArtists.clear();
  399. plRatings.clear();
  400. }
  401. else if(selector == "convert_and_output_to")
  402. {
  403. ofstream out(argument.c_str());
  404. ConvertAndPrint(plSongs, plArtists, plRatings, true, out);
  405. plSongs.clear();
  406. plArtists.clear();
  407. plRatings.clear();
  408. }
  409. return;
  410. }
  411.  
  412.  
  413. /* This procedure reads through the entire user script file, and creates playlists
  414. * as specified.
  415. *
  416. * The first 3 inputs to the procedure are vectors representing all the songs in
  417. * the user's library.
  418. * The next 3 inputs are vectors representing the songs in the current playlist.
  419. * The last input is a reference to the ifstream connected to the scriptfile,
  420. *
  421. * This procedure must create all the playlists (and output them to the correct
  422. * files) that the script file specifies.
  423. */
  424. void generatePlaylists(vector <string> & allSongs, vector <string> & allArtists, vector <int> & allRatings, vector <string> & plSongs, vector <string> & plArtists, vector <int> & plRatings, ifstream & in)
  425. {
  426. string selector;
  427. string argument;
  428.  
  429. in >> selector >> argument;
  430.  
  431. while (!in.fail())
  432. {
  433. performAction(allSongs, allArtists, allRatings, plSongs, plArtists, plRatings, selector, argument);
  434.  
  435. in >> selector >> argument;
  436. }
  437.  
  438. return;
  439. }
  440.  
  441.  
  442.  
  443. // Write your definition of populateVectors() here
  444. void populateVectors(vector <string> & allSongs, vector <string> & allArtists, vector <int> & allRatings, ifstream & inFile)
  445. {
  446. string newsong;
  447. string newartist;
  448. int newrating;
  449. bool duplicate;
  450.  
  451. inFile >> newsong >> newartist >> newrating;
  452.  
  453. while (!inFile.fail())
  454. {
  455. int n = allSongs.size();
  456. for (int i = 0; i<n; i++)
  457. {
  458. if (allSongs.at(i) == newsong)
  459. {
  460. if (allArtists.at(i) == newartist)
  461. {
  462. duplicate = 1;
  463. }
  464. }
  465.  
  466. }
  467.  
  468. if(!duplicate)
  469. {
  470. allSongs.push_back(newsong);
  471. allArtists.push_back(newartist);
  472. allRatings.push_back(newrating);
  473. }
  474. inFile >> newsong >> newartist >> newrating;
  475. }
  476.  
  477.  
  478. assert(allSongs.size() == allArtists.size());
  479. assert(allRatings.size() == allArtists.size());
  480.  
  481. return;
  482. }
  483.  
  484.  
  485. // ------------------ Main Function ------------------------------
  486.  
  487. /* Main Function for custom playlist creator (THIS FUNCTION IS COMPLETE) */
  488. int main(){
  489. //set up variables
  490. string inputFile = "library.txt";
  491. string transformFile = "scriptfile.txt";
  492. string artist, songName;
  493. int rating;
  494.  
  495. vector<string> allSongs, allArtists; // songs and corresponding artists in library
  496. vector<int> allRatings; // corresponding ratings for library songs
  497.  
  498. vector<string> plSongs, plArtists; // songs and corresponding artists in playlist
  499. vector<int> plRatings; // corresponding ratings for playlist songs
  500.  
  501. ifstream inFile(inputFile.c_str());
  502. ifstream tFile(transformFile.c_str());
  503.  
  504. //read the playlist in from file and place them in vectors (for task 1)
  505. populateVectors(allSongs, allArtists, allRatings, inFile);
  506.  
  507. //run transformations from file (for task 3)
  508. generatePlaylists(allSongs, allArtists, allRatings, plSongs, plArtists, plRatings, tFile);
  509. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement