Advertisement
Guest User

Untitled

a guest
Feb 24th, 2020
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.22 KB | None | 0 0
  1. #include <iostream>
  2. #include <exception>
  3. #include <string>
  4. #include <vector>
  5. #include <map>
  6. #include <set>
  7. #include <fstream>
  8. #include "Animals.h"
  9. #pragma warning (disable : 4996)
  10. using namespace std;
  11. int main()
  12. {
  13. vector<Animals> animals;
  14. ifstream fin("input.txt");
  15. if (!fin.is_open())
  16. {
  17. cerr << "Input file did not open!";
  18. return 0;
  19. }
  20. try
  21. {
  22. while (fin.peek() != EOF) {
  23. string str;
  24. getline(fin, str);
  25.  
  26. int ag;
  27. string owner = "", name = "", type = "", age;
  28.  
  29. int k = 0;
  30.  
  31. while (str[k] != ',')
  32. {
  33. owner.push_back(str[k]);
  34. k++;
  35. }
  36. k += 2;
  37. while (str[k] != ',')
  38. {
  39. type.push_back(str[k]);
  40. k++;
  41. }
  42. k += 2;
  43. while (str[k] != ',')
  44. {
  45. name.push_back(str[k]);
  46. k++;
  47. }
  48. k += 2;
  49. while (k != str.size())
  50. {
  51. age.push_back(str[k]);
  52. k++;
  53. }
  54.  
  55. ag = atoi(age.c_str());
  56.  
  57. animals.push_back(Animals(owner, type, name, ag));
  58. }
  59. fin.close();
  60. }
  61. catch (exception & ex) {
  62. cout << "Something wrong: " << endl;
  63. cout << ex.what();
  64. }
  65.  
  66. int choice = 1;
  67. while (choice != 0)
  68. {
  69. cout << "enter what do you want?" << endl;
  70. cout << "1. Calculate the number of different types of animals for each owner." << endl;
  71. cout << "2. For a specific type of animal (entered by the user) to bring all its owners and nicknames." << endl;
  72. cout << "3. Determine how many species of animals bears a certain nickname (the nickname is entered by the user)." << endl;
  73. cout << "4. To display information about the age of the oldest and the youngest animal of each species." << endl;
  74. cout << "0. Exit" << endl;
  75. cin >> choice;
  76. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  77.  
  78. switch (choice)
  79. {
  80. case 1: {
  81. map<string, set<string>> species1;
  82. for (int i = 0; i < animals.size(); i++)
  83. {
  84. species1[animals[i].GetOwnerName()].insert(animals[i].GetType());
  85. }
  86. for (auto Owner : species1)
  87. {
  88. cout << Owner.first << " - has " << Owner.second.size() << " different types of pets" << endl;
  89. }
  90. }
  91. break;
  92. case 2:
  93. {
  94. pair < set <string>, set <string>> answer;
  95. map <string, pair <set <string>, set <string>>> species2;
  96. string spec;
  97. for (Animals animal : animals) {
  98. auto& Info = species2[animal.GetType()];
  99. Info.first.insert(animal.GetOwnerName());
  100. Info.second.insert(animal.GetName());
  101. }
  102. cout << "enter type " << endl;
  103. getline(cin, spec);
  104.  
  105. answer = species2[spec];
  106. cout << "Pets of the species - " << spec << " - have the following owners:" << endl;
  107. for (string owner : answer.first)
  108. {
  109. cout << owner << endl;
  110. }
  111. cout << "And that pets have the following names:" << endl;
  112. for (string name : answer.second)
  113. {
  114. cout << (name.empty() ? "<empty>" : name) << endl;
  115. }}
  116. break;
  117. case 3: {
  118.  
  119. map <string, set <string>> species3;
  120. string petname;
  121.  
  122. for (Animals animal : animals) {
  123. species3[animal.GetName()].insert(animal.GetType());
  124. }
  125. cout << "enter name" << endl;
  126. getline(cin, petname);
  127. cout << species3[petname].size() << " species of pets share this name." << endl; }
  128. break;
  129. case 4:
  130. { map <string, pair<Animals, Animals>> species4;
  131. string spec;
  132. for (Animals animal : animals) {
  133. auto& Info = species4[animal.GetType()];
  134. if (Info.first.GetOwnerName() == "\0" ||
  135. animal.GetAge() < Info.first.GetAge())
  136. {
  137. Info.first = animal;
  138. }
  139. if (Info.second.GetOwnerName() == "\0" ||
  140. animal.GetAge() > Info.second.GetAge())
  141. {
  142. Info.second = animal;
  143. }
  144. }
  145.  
  146. cout << "The youngest pets:" << endl << endl;
  147. for (auto speciesPair : species4)
  148. {
  149. spec = speciesPair.first;
  150. pair <Animals, Animals> petsPair = speciesPair.second;
  151. cout << "Here is the youngest pet from the species: " << spec << endl;
  152. cout << petsPair.first.GetAge() << endl;
  153. }
  154. cout << "<<<The oldest pets>>>" << endl << endl;
  155. for (auto speciesPair : species4)
  156. {
  157. spec = speciesPair.first;
  158. pair <Animals, Animals> petsPair = speciesPair.second;
  159. cout << "Here is the oldest pet from the species: " << spec << endl;
  160. cout << petsPair.second.GetAge() << endl;
  161. }
  162. break;
  163. }
  164. default:
  165.  
  166. break;
  167. }
  168. }
  169. system("pause");
  170. return 0;
  171.  
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement