Advertisement
dllbridge

Untitled

Apr 7th, 2025
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.17 KB | None | 0 0
  1.  
  2.  
  3. #include  <iostream>
  4. #include    <string>
  5. #include    <vector>
  6. using namespace std;
  7.  
  8.  
  9.  
  10. ///////////////////////////////////////
  11. class Person
  12. {
  13.    public:
  14.     void setName(string newName)  { name = newName; }
  15.     void setAge (int newAge)      { age  =  newAge; }
  16.     void setGender(string newGender) { gender = newGender; }
  17.     void setPassport(int newPassport) { passport = newPassport; }
  18.  
  19.     string getName() { return name; }
  20.  
  21.     void showInfo()
  22.     {
  23.         cout << "Name: "     << name   << ", Age: "      << age
  24.              << ", Gender: " << gender << ", Passport: " << passport << endl;
  25.     }
  26.  
  27.   private:
  28.     string   name;
  29.     int       age;
  30.     string gender;
  31.     int  passport;
  32. };
  33.  
  34. // v.erase(v.begin() + 2);      // Удаление третьего элемента
  35. ////////////////////////////////////////////////////////
  36. class Archive
  37. {
  38.     public:
  39.  
  40.     int nCounter;
  41.    
  42.    
  43.     void delete_P()
  44.     {
  45.        
  46.         persons.pop_back();
  47.        
  48.       //  persons.erase(persons.begin() + 2);  
  49.     }
  50.    
  51.    
  52.     void delete_Persons(string name)
  53.     {
  54.         for (int i = 0; i < persons.size(); i++)
  55.         {
  56.             if (persons[i]->getName () == name)
  57.             {  // delete_P();                   //showInfo();
  58.                  persons.erase(persons.begin() + i);   
  59.             }
  60.             else   cout << "Person with name " << name << " not found." << endl;
  61.         }
  62.     }
  63.    
  64.     void addPerson(Person* newPerson)
  65.     {
  66.         persons.push_back(newPerson);
  67.     }
  68.  
  69.     void showPersons()
  70.     {
  71.         for (int i = 0; i < persons.size(); i++)
  72.         {
  73.             persons[i]->showInfo();
  74.             cout << "------------------------------" << endl;
  75.         }
  76.     }
  77.  
  78.     void findPersonByName(string name)
  79.     {
  80.         for (int i = 0; i < persons.size(); i++)
  81.         {
  82.             if (persons[i]->getName () == name)
  83.             {   persons[i]->showInfo();
  84.                
  85.             }
  86.             else   cout << "Person with name " << name << " not found." << endl;
  87.         }
  88.     }
  89.  
  90.   private:
  91.     vector<Person*> persons;
  92. };
  93.  
  94.  
  95. /////////////////////////////////////////////////////////////////////////////
  96. int main()
  97. {
  98.    
  99.    
  100.     Archive archive;
  101.  
  102.  
  103.     Person *person1 = new   Person;
  104.     person1->setName      ("Vlad");
  105.     person1->setAge           (28);
  106.     person1->setGender       ("M");
  107.     person1->setPassport    (5231);
  108.         archive.addPerson(person1);
  109.  
  110.             person1 =   new Person;
  111.     person1->setName      ("Olga");
  112.     person1->setAge           (32);
  113.     person1->setGender       ("W");
  114.     person1->setPassport    (5798);
  115.     archive.addPerson    (person1);
  116.    
  117.             person1 = new   Person;
  118.     person1->setName      ("Vlad");
  119.     person1->setAge           (33);
  120.     person1->setGender       ("M");
  121.     person1->setPassport    (1111);
  122.         archive.addPerson(person1);
  123.  
  124.             person1 =   new Person;
  125.     person1->setName    ("Natali");
  126.     person1->setAge           (56);
  127.     person1->setGender       ("W");
  128.     person1->setPassport    (2898);
  129.     archive.addPerson    (person1);    
  130.  
  131.     int    choice = 0;
  132.     int           age,
  133.              passport;
  134.              
  135.     string       name,
  136.                gender,
  137.            searchName;          
  138.    
  139.     Person* newPerson;
  140.    
  141.     string searchName_d;
  142.    
  143.     while (choice != 5)
  144.     {
  145.  
  146.         cout << "What do you want to do?" << endl;
  147.         cout << "1) Add a person"         << endl;
  148.         cout << "2) Find a person"        << endl;
  149.         cout << "3) Show all persons"     << endl;
  150.         cout << "4) Delete person"        << endl;
  151.         cout << "5) Exit the program"     << endl;
  152.         cout << "Your choice: ";
  153.         cin  >> choice;
  154.  
  155.         switch(choice)
  156.         {
  157.            
  158.  
  159.             case  1:    newPerson = new Person;
  160.  
  161.                         cout << "Enter name: ";
  162.                         cin  >> name;
  163.                         newPerson->setName(name);
  164.            
  165.                         cout << "Enter age: ";
  166.                         cin >> age;
  167.                         newPerson->setAge(age);
  168.            
  169.                         cout << "Enter gender (M/W): ";
  170.                         cin >> gender;
  171.                         newPerson->setGender(gender);
  172.            
  173.                         cout << "Enter passport number: ";
  174.                         cin >> passport;
  175.                         newPerson->setPassport(passport);
  176.            
  177.                         archive.addPerson(newPerson);
  178.                         cout << "Person added!" << endl;
  179.                         break;
  180.                        
  181.             case  2:    
  182.                         cout << "Enter name to search: ";
  183.                         cin >> searchName;
  184.                         archive.findPersonByName(searchName);        
  185.                         break;
  186.                        
  187.             case  3:    archive.showPersons();            
  188.                         break;
  189.                        
  190.             case  4:    cout << "Enter name to delete: ";
  191.                         cin >> searchName_d;
  192.                         archive.delete_Persons(searchName_d);    
  193.                         break;              
  194.                        
  195.             case  5:    cout << "Program terminated."        << endl;
  196.                         break;  
  197.                        
  198.             default:    cout << "Invalid choice. Try again." << endl;                    
  199.                          
  200.            
  201.         }  
  202.  
  203.        
  204.     }
  205.  
  206. return 0;
  207. }
  208.  
  209.  
  210.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement