Advertisement
Guest User

Untitled

a guest
Dec 3rd, 2013
329
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <iomanip>
  4. #include <fstream>
  5. using namespace std;
  6.  
  7. //Song Class Declaration
  8.  
  9. class Song
  10. {
  11.  
  12. private:
  13.  
  14. int time;
  15. int year;
  16. string title;
  17. string genre;
  18. string artist;
  19.  
  20. public:
  21. Song();
  22. void set(string sname, string aname, string sgenre, int syear, int stime);
  23. void get(string &sname, string &aname, string &sgenre, int &syear, int &stime);
  24. void print();
  25. int gettime();
  26. void sort();
  27. string gettitle();
  28.  
  29. };
  30.  
  31.  
  32. //***************************************************
  33. //* Class Constructor *
  34. //***************************************************
  35.  
  36. Song::Song()
  37. {
  38. int time = 0;
  39. int year = 0;
  40. string title = "none";
  41. string genre = "none";
  42. string artist = "none";
  43.  
  44. }
  45.  
  46.  
  47. //***************************************************
  48. //* Set Function *
  49. //***************************************************
  50.  
  51. void Song::set(string sname, string aname, string sgenre, int syear, int stime)
  52. {
  53. time = stime;
  54. year = syear;
  55. title = sname;
  56. genre = sgenre;
  57. artist = aname;
  58.  
  59. }
  60.  
  61. //***************************************************
  62. //* Get Function *
  63. //***************************************************
  64.  
  65. void Song::get(string &sname, string &aname, string &sgenre, int &syear, int &stime)
  66. {
  67.  
  68.  
  69.  
  70. }
  71.  
  72.  
  73. //***************************************************
  74. //* Get Time Function *
  75. //***************************************************
  76. int Song::gettime()
  77. {
  78. return time;
  79. }
  80.  
  81. //***************************************************
  82. //* Get Title Function *
  83. //***************************************************
  84.  
  85. string Song::gettitle()
  86. {
  87.  
  88. return title;
  89.  
  90. }
  91.  
  92. //***************************************************
  93. //* Sort Function *
  94. //***************************************************
  95. void Song::sort()
  96. {
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103. }
  104.  
  105. //***************************************************
  106. //* Print Function *
  107. //***************************************************
  108.  
  109. void Song::print()
  110. {
  111.  
  112. using namespace std;
  113. cout << setw(15) << title << setw(15) << artist << setw(10) << genre << setw(6) << year << setw(5) << time << endl;
  114.  
  115. }
  116.  
  117.  
  118. //***************************************************
  119. //* main function *
  120. //***************************************************
  121. int main()
  122. {
  123.  
  124. fstream din;
  125.  
  126. //Create an array
  127. int size = 0;
  128.  
  129. cout << "Welcome to Heather's Song Data Program." << endl;
  130. cout << "When prompted, please enter up to 20 song data." << endl;
  131. cout << "When finished, hit Ctrl-D or it will auto stop at 20." << endl;
  132.  
  133.  
  134.  
  135. Song array[size];
  136. //Initialize the array
  137. for (int i = 0; i <= 20; i++)
  138. {
  139.  
  140. break;
  141. int time = 0;
  142. int year = 0;
  143. string genre, title, artist, junk;
  144.  
  145. getline(cin, junk);
  146. cout << "Enter the name of the song." << endl;
  147. getline(cin, title);
  148. cout << "Enter the artist's name." << endl;
  149. getline(cin, artist);
  150. cout << "Enter the genre." << endl;
  151. getline(cin, genre);
  152. cout << "Enter the year it was released." << endl;
  153. cin >> year;
  154. cout << "Enter the time of the song in seconds." << endl;
  155. cin >> time;
  156.  
  157. array[i].set(title, artist, genre, year, time);
  158.  
  159. size++;
  160.  
  161. }
  162.  
  163. // Print array of Songs
  164. using namespace std;
  165. cout << setw(15)<< "Title:" << setw(15) << "Artist:" << setw(10) << "Genre:" << setw(6) << "Year:" << setw(5) << "Time(s)" << endl;
  166. cout << "_________________________________________________"<< endl;
  167.  
  168. for (int i = 0; i<size; i++)
  169. array[i].print();
  170.  
  171. int minimum1;
  172. Song temp1;
  173. Song sortedtitle[size];
  174.  
  175. //Initialize a second array for title sorting
  176. for (int i = 0; i <size; i++)
  177. {
  178. sortedtitle[i] = array[i];
  179. }
  180.  
  181. //Access title to sort
  182.  
  183.  
  184. for (int i = 0; i <size-1; i++)
  185. {
  186. minimum1 = i;
  187. for (int j = 1+i; j <size; j++)
  188. {if (sortedtitle[j].gettitle() < sortedtitle[minimum1].gettitle())
  189. minimum1 = j;
  190. }
  191. if (minimum1 != i)
  192. {
  193. temp1 = sortedtitle[i];
  194. sortedtitle[i] = sortedtitle[minimum1];
  195. sortedtitle[minimum1] = temp1;
  196. }
  197.  
  198. }
  199.  
  200. // Print out title sorted array
  201. cout << endl;
  202. cout << setw(15)<< "Title:" << setw(15) << "Artist:" << setw(10) << "Genre:" << setw(6) << "Year:" << setw(5) << "Time(s)" << endl;
  203.  
  204. cout << endl;
  205. cout << "_________________________________________________"<< endl;
  206.  
  207. for (int i = 0; i<size; i++)
  208. sortedtitle[i].print();
  209.  
  210.  
  211. int minimum;
  212. Song temp;
  213.  
  214. //Initialize a third array for time sorting
  215. Song sortedtime[size];
  216. for (int i = 0; i < size; i++)
  217. {
  218. sortedtime[i] = array[i];
  219. }
  220.  
  221. // Access time to sort
  222. // Sort the array using time in seconds
  223.  
  224. for (int i = 0; i<size-1; i++)
  225. {
  226. minimum = i;
  227. for (int j = 1+i; j<size; j++)
  228. { if (sortedtime[j].gettime() < sortedtime[minimum].gettime())
  229. minimum = j;
  230. }
  231.  
  232. if (minimum != i)
  233. { temp = sortedtime[i];
  234. sortedtime[i] = sortedtime[minimum];
  235. sortedtime[minimum] = temp;
  236. }
  237. }
  238.  
  239.  
  240. //Print sorted time array
  241. cout << endl;
  242. cout << setw(15)<< "Title:" << setw(15) << "Artist:" << setw(10) << "Genre:" << setw(6) << "Year:" << setw(5) << "Time(s)" << endl;
  243.  
  244. cout << endl;
  245. cout << "_________________________________________________"<< endl;
  246.  
  247. for (int i = 0; i<size; i++)
  248. sortedtime[i].print();
  249.  
  250.  
  251. return 0;
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement