Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <exception>
- #include <string>
- #include <vector>
- #include <map>
- #include <set>
- #include <fstream>
- #include "Animals.h"
- #pragma warning (disable : 4996)
- using namespace std;
- int main()
- {
- vector<Animals> animals;
- ifstream fin("input.txt");
- if (!fin.is_open())
- {
- cerr << "Input file did not open!";
- return 0;
- }
- try
- {
- while (fin.peek() != EOF) {
- string str;
- getline(fin, str);
- int ag;
- string owner = "", name = "", type = "", age;
- int k = 0;
- while (str[k] != ',')
- {
- owner.push_back(str[k]);
- k++;
- }
- k += 2;
- while (str[k] != ',')
- {
- type.push_back(str[k]);
- k++;
- }
- k += 2;
- while (str[k] != ',')
- {
- name.push_back(str[k]);
- k++;
- }
- k += 2;
- while (k != str.size())
- {
- age.push_back(str[k]);
- k++;
- }
- ag = atoi(age.c_str());
- animals.push_back(Animals(owner, type, name, ag));
- }
- fin.close();
- }
- catch (exception & ex) {
- cout << "Something wrong: " << endl;
- cout << ex.what();
- }
- int choice = 1;
- while (choice != 0)
- {
- cout << "enter what do you want?" << endl;
- cout << "1. Calculate the number of different types of animals for each owner." << endl;
- cout << "2. For a specific type of animal (entered by the user) to bring all its owners and nicknames." << endl;
- cout << "3. Determine how many species of animals bears a certain nickname (the nickname is entered by the user)." << endl;
- cout << "4. To display information about the age of the oldest and the youngest animal of each species." << endl;
- cout << "0. Exit" << endl;
- cin >> choice;
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- switch (choice)
- {
- case 1: {
- map<string, set<string>> species1;
- for (int i = 0; i < animals.size(); i++)
- {
- species1[animals[i].GetOwnerName()].insert(animals[i].GetType());
- }
- for (auto Owner : species1)
- {
- cout << Owner.first << " - has " << Owner.second.size() << " different types of pets" << endl;
- }
- }
- break;
- case 2:
- {
- pair < set <string>, set <string>> answer;
- map <string, pair <set <string>, set <string>>> species2;
- string spec;
- for (Animals animal : animals) {
- auto& Info = species2[animal.GetType()];
- Info.first.insert(animal.GetOwnerName());
- Info.second.insert(animal.GetName());
- }
- cout << "enter type " << endl;
- getline(cin, spec);
- answer = species2[spec];
- cout << "Pets of the species - " << spec << " - have the following owners:" << endl;
- for (string owner : answer.first)
- {
- cout << owner << endl;
- }
- cout << "And that pets have the following names:" << endl;
- for (string name : answer.second)
- {
- cout << (name.empty() ? "<empty>" : name) << endl;
- }}
- break;
- case 3: {
- map <string, set <string>> species3;
- string petname;
- for (Animals animal : animals) {
- species3[animal.GetName()].insert(animal.GetType());
- }
- cout << "enter name" << endl;
- getline(cin, petname);
- cout << species3[petname].size() << " species of pets share this name." << endl; }
- break;
- case 4:
- { map <string, pair<Animals, Animals>> species4;
- string spec;
- for (Animals animal : animals) {
- auto& Info = species4[animal.GetType()];
- if (Info.first.GetOwnerName() == "\0" ||
- animal.GetAge() < Info.first.GetAge())
- {
- Info.first = animal;
- }
- if (Info.second.GetOwnerName() == "\0" ||
- animal.GetAge() > Info.second.GetAge())
- {
- Info.second = animal;
- }
- }
- cout << "The youngest pets:" << endl << endl;
- for (auto speciesPair : species4)
- {
- spec = speciesPair.first;
- pair <Animals, Animals> petsPair = speciesPair.second;
- cout << "Here is the youngest pet from the species: " << spec << endl;
- cout << petsPair.first.GetAge() << endl;
- }
- cout << "<<<The oldest pets>>>" << endl << endl;
- for (auto speciesPair : species4)
- {
- spec = speciesPair.first;
- pair <Animals, Animals> petsPair = speciesPair.second;
- cout << "Here is the oldest pet from the species: " << spec << endl;
- cout << petsPair.second.GetAge() << endl;
- }
- break;
- }
- default:
- break;
- }
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement