Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7. class Games {
  8. public:
  9. Games(); // Default constructor
  10. Games(string, string, string, int, double);
  11. void SetName(string);
  12. void SetScore(double);
  13. void Print();
  14. void SetType(string);
  15. void SetYear(int);
  16. void SetGenre(string);
  17. int GetScore() { //Gets the score form the private: section
  18. return score;
  19. }
  20. int GetYear() { //Gets the year form the private: section
  21. return year;
  22. }
  23. string GetName() { //Gets the name form the private: section
  24. return name;
  25. }
  26. string GetGenre() { //Gets the genre form the private: section
  27. return genre;
  28. }
  29. string GetType() { //Gets the type form the private: section
  30. return type;
  31. }
  32. private:
  33. string name, type, genre;
  34. double score;
  35. int year;
  36. };
  37.  
  38. Games::Games() { // Default constructor definition
  39. name = "UnNamed";
  40. type = "Undefined";
  41. genre = "NoGenre";
  42. score = -1.0;
  43. year = -1;
  44. }
  45.  
  46. Games::Games(string n, string t, string g, int y, double s) {
  47. name = n;
  48. type = t;
  49. genre = g;
  50. year = y;
  51. score = s;
  52. }
  53. void Games::SetName(string gameName) {
  54. name = gameName;
  55. }
  56.  
  57. void Games::SetScore(double gameRank) {
  58. score = gameRank;
  59. }
  60.  
  61. void Games::SetType(string gameType) {
  62. type = gameType;
  63. }
  64.  
  65. void Games::SetYear(int gameYear) {
  66. year = gameYear;
  67. }
  68.  
  69. void Games::SetGenre(string gameGenre) {
  70. genre = gameGenre;
  71. }
  72.  
  73. void Games::Print() {
  74. cout << setw(20) << name << " -- ";
  75. cout << setw(5) << score << " -- ";
  76. cout << setw(5) << year << " -- ";
  77. cout << setw(5) << genre << " -- ";
  78. cout << setw(5) << type << endl;
  79. }
  80. void SortByScore(Games* target) //function to sort the array by score
  81. {
  82. sort(target, target + 5, [](Games& a, Games& b) ->
  83. bool {
  84. return a.GetScore() < b.GetScore();
  85. });}
  86. void SortByYear(Games* target) //function to sort the array by year
  87. {
  88. sort(target, target + 5, [](Games& a, Games& b) ->
  89. bool {
  90. return a.GetYear() < b.GetYear();
  91. });}
  92. void SortByName(Games* target) //function to sort the array by name
  93. {
  94. sort(target, target + 5, [](Games& a, Games& b) ->
  95. bool {
  96. return a.GetName() < b.GetName();
  97. });}
  98. void SortByGenre(Games* target) //function to sort the array by genre
  99. {
  100. sort(target, target + 5, [](Games& a, Games& b) ->
  101. bool {
  102. return a.GetGenre() < b.GetGenre();
  103. });}
  104. void SortByType(Games* target) //function to sort the array by type
  105. {
  106. sort(target, target + 5, [](Games& a, Games& b) ->
  107. bool {
  108. return a.GetType() < b.GetType();
  109. });}
  110. int main()
  111. {
  112. int userInput;
  113. Games myGames[5];
  114. myGames[0].SetName("Gran Turismo");
  115. myGames[0].SetYear(1997);
  116. myGames[0].SetGenre("Racing");
  117. myGames[0].SetType("PlayStation");
  118. myGames[0].SetScore(8.6);
  119. myGames[1] = Games("Super Mario Galaxy", "Wii Console", "Platform", 2007, 9.0);
  120. myGames[0].Print();
  121. myGames[1].Print();
  122. myGames[2].Print();
  123.  
  124. cout << "Sorted by score they are: " << endl;
  125. SortByScore(&myGames[0]); //call to function to print the sorted version
  126. for(auto game : myGames)
  127. {
  128. if(game.GetName() != "UnNamed"){
  129. game.Print();
  130. }
  131. }
  132. cout << "Sorted by year they are: " << endl;
  133. SortByYear(&myGames[0]); //call to function to print the sorted version
  134. for(auto game : myGames)
  135. {
  136. if(game.GetName() != "UnNamed"){
  137. game.Print();
  138. }
  139. }
  140. cout << "Sorted by name they are: " << endl;
  141. SortByName(&myGames[0]); //call to function to print the sorted version
  142. for(auto game : myGames)
  143. {
  144. if(game.GetName() != "UnNamed"){
  145. game.Print();
  146. }
  147. }
  148. cout << "Sorted by genre they are: " << endl;
  149. SortByGenre(&myGames[0]); //call to function to print the sorted version
  150. for(auto game : myGames)
  151. {
  152. if(game.GetName() != "UnNamed"){
  153. game.Print();
  154. }
  155. }
  156. cout << "Sorted by type they are: " << endl;
  157. SortByType(&myGames[0]); //call to function to print the sorted version
  158. for(auto game : myGames)
  159. {
  160. if(game.GetName() != "UnNamed"){
  161. game.Print();
  162. }
  163. }
  164.  
  165. cout << "input 1 to run again with the UnNamed titles, 0 to quit: ";
  166. cin >> userInput;
  167.  
  168. if (userInput = 1){
  169. cout << "Sorted by score they are: " << endl;
  170. SortByScore(&myGames[0]); //call to function to print the sorted version
  171. for(auto game : myGames)
  172. {
  173. game.Print();
  174. }
  175. cout << "Sorted by year they are: " << endl;
  176. SortByYear(&myGames[0]); //call to function to print the sorted version
  177. for(auto game : myGames)
  178. {
  179. game.Print();
  180. }
  181. cout << "Sorted by name they are: " << endl;
  182. SortByName(&myGames[0]); //call to function to print the sorted version
  183. for(auto game : myGames)
  184. {
  185. game.Print();
  186. }
  187. cout << "Sorted by genre they are: " << endl;
  188. SortByGenre(&myGames[0]); //call to function to print the sorted version
  189. for(auto game : myGames)
  190. {
  191. game.Print();
  192. }
  193. cout << "Sorted by type they are: " << endl;
  194. SortByType(&myGames[0]); //call to function to print the sorted version
  195. for(auto game : myGames)
  196. {
  197. game.Print();
  198. }
  199. } else if(userInput = 0){
  200. return 0;
  201. }
  202.  
  203.  
  204. return 0;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement