Advertisement
tomur

Done Homework

Mar 23rd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.27 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. //head no longer global
  7. //write the main
  8. //make constructor and destructor and write inside them how tehy are created
  9. //use at least 2 different object of classes (dynamic object by new and automated by )
  10. class Animal {
  11. private:
  12.     int id;
  13.     char *name;
  14.     char *spieces;
  15.     int owner_id;
  16.  
  17.     Animal *next;
  18.     Animal(int, char *, char *, int);
  19.  
  20.     ~Animal();
  21.  
  22.     void print();
  23.  
  24.     friend class AnimalList;
  25. };
  26.  
  27. Animal::Animal(int idSet, char *nameSet, char *spiecesSet, int owner_idSet) {
  28.     cout << "Animal constructor called" << endl;
  29.     id = idSet;
  30.     name = new char[strlen(nameSet) + 1];
  31.     strcpy(name, nameSet);
  32.     spieces = new char[strlen(spiecesSet) + 1];
  33.     strcpy(spieces, spiecesSet);
  34.     owner_id = owner_idSet;
  35.     next = NULL;
  36. }
  37.  
  38. Animal::~Animal() {
  39.     cout << "Animal destructor called" << endl;
  40.     delete[] name;
  41.     delete[] spieces;
  42. }
  43.  
  44. void Animal::print() {
  45.     cout << "ID: " << id << endl;
  46.     cout << "Name: " << name << endl;
  47.     cout << "Spieces: " << spieces << endl;
  48.     cout << "Owner ID: " << owner_id << endl;
  49.     printf("\n");
  50. }
  51.  
  52. //-------------------------------------------------------------------
  53.  
  54. class AnimalList {
  55. public:
  56.     Animal* first;
  57.  
  58.     AnimalList();//create empty list
  59.     ~AnimalList();//delete all list
  60.     void add_Animal(int, char *, char *, int);
  61.  
  62.     void remove_Owner(int);
  63.  
  64.     void print();
  65.  
  66.     int &modify(char* name,char* spieces, int id);
  67. };
  68.  
  69. AnimalList::AnimalList() {
  70.     cout << "AnimalList constructor called" << endl;
  71.     first=NULL;
  72. }
  73.  
  74. AnimalList::~AnimalList() {
  75.     cout << "AnimalList destructor called" << endl;
  76.     if (first == NULL) {
  77.         cout << "first is NULL" << endl;
  78.         return;
  79.     } else {
  80.         Animal *tmp = first;
  81.         while (tmp != NULL) {
  82.             Animal *deleteMe = tmp;
  83.             tmp = tmp->next;
  84.             delete deleteMe;
  85.         }
  86.         first = NULL;
  87.     }
  88. }
  89.  
  90. void AnimalList::add_Animal(int id, char *name, char *spieces, int owner_id) {
  91.  
  92.     if(first==NULL)
  93.     {
  94.  
  95.     }else
  96.     {
  97.         Animal *tmp2 = first;
  98.         while (tmp2 != NULL) {
  99.             if (tmp2->id == id) {
  100.                 cout << "Cannot add animal ID Repeat" << endl;
  101.                 return;
  102.             }
  103.             tmp2 = tmp2->next;
  104.         }
  105.     }
  106.     Animal *newCharacter = new Animal(id, name, spieces, owner_id);
  107.  
  108.  
  109.  
  110.  
  111.     if (first == NULL) {
  112.         first = newCharacter;
  113.         newCharacter->next = NULL;
  114.         cout << "added first element" << endl;
  115.         return;
  116.     } else {
  117.         Animal *tmp = first;
  118.         if(tmp->id > newCharacter->id)
  119.         {
  120.             newCharacter->next = tmp;
  121.             first = newCharacter;
  122.             cout<<"added as first in not empty list"<<endl;
  123.             return;
  124.         }
  125.         while (tmp->next != NULL) {
  126.             if (newCharacter->id < tmp->next->id) {
  127.                 newCharacter->next = tmp->next;
  128.                 tmp->next = newCharacter;
  129.                 cout << "added element in between" << endl;
  130.                 return;
  131.             }
  132.             tmp = tmp->next;
  133.         }
  134.         tmp->next = newCharacter;
  135.         newCharacter->next = NULL;
  136.         cout << "added element as last" << endl;
  137.     }
  138. }
  139.  
  140. void AnimalList::print() {
  141.     if(first==NULL)
  142.     {
  143.         cout<<"List is empty"<<endl;
  144.         return;
  145.     }
  146.     Animal *tmp = first;
  147.     while (tmp != NULL) {
  148.         cout << "ID: " << tmp->id << endl;
  149.         cout << "Name: " << tmp->name << endl;
  150.         cout << "Spieces: " << tmp->spieces << endl;
  151.         cout << "Owner ID: " << tmp->owner_id << endl;
  152.         printf("\n");
  153.         tmp = tmp->next;
  154.     }
  155. }
  156.  
  157. void AnimalList::remove_Owner(int ownerIdToRemove) {
  158.     if(first==NULL)
  159.     {
  160.         cout<<"List is empty"<<endl;
  161.         return;
  162.     }
  163.     Animal *tmp = first;
  164.     bool deleted;
  165.     while (tmp->next != NULL) {
  166.         deleted = false;
  167.         if (tmp->next->owner_id == ownerIdToRemove) //DESTROY HIM
  168.         {
  169.             Animal *deleteMe = tmp->next;
  170.             if(tmp->next->next != NULL)
  171.             {
  172.                 tmp->next = tmp->next->next;
  173.             }else
  174.             {
  175.                 tmp->next = NULL;
  176.             }
  177.  
  178.             delete deleteMe;
  179.             deleted = true;
  180.         }
  181.         if (!deleted) {
  182.             tmp = tmp->next;
  183.         }
  184.  
  185.     }
  186.     if(first->owner_id == ownerIdToRemove)
  187.     {
  188.         if(first->next != NULL)
  189.         {
  190.             delete first;
  191.             first = first->next;
  192.         }else
  193.         {
  194.             delete first;
  195.             first = NULL;
  196.         }
  197.     }
  198. }
  199.  
  200. int &AnimalList::modify(char* name,char* spieces, int id)
  201. {
  202.     Animal *tmp = first;
  203.     int counter = 0;
  204.     int *firstSpiecesOwner=NULL;
  205.     while(tmp!=NULL)
  206.     {
  207.         if(tmp->id == id)
  208.         {
  209.             return tmp->owner_id;
  210.         }
  211.         if(strcmp(spieces,tmp->spieces) == 0)
  212.         {
  213.             if(firstSpiecesOwner==NULL)
  214.             {
  215.                 firstSpiecesOwner = &tmp->owner_id;
  216.             }
  217.             counter++;
  218.         }
  219.  
  220.  
  221.         tmp = tmp->next;
  222.     }   //if ID was not found
  223.     if(counter <= 3)
  224.     {
  225.         add_Animal(id,name,spieces,0);
  226.         tmp = first;
  227.         while(tmp!=NULL)
  228.         {
  229.             if(tmp->id == id)
  230.             {
  231.                 return tmp->owner_id;
  232.             }
  233.  
  234.             tmp = tmp->next;
  235.         }
  236.     }else
  237.     {
  238.         return *firstSpiecesOwner;
  239.     }
  240.  
  241. }
  242.  
  243. int main() {
  244.  
  245.  
  246.     AnimalList cats;
  247.     AnimalList* dogs = new AnimalList;
  248.  
  249.     /*
  250.     cout<<"Deleting from empty list"<<endl;
  251.     cats.remove_Owner(1);
  252.     dogs->remove_Owner(1);
  253.     */
  254.  
  255.     /*
  256.     cout<<"Adding in unsorted order"<<endl;
  257.     cats.add_Animal(4, "Pola", "Kot", 2);
  258.     cats.add_Animal(2, "Dzirno", "Kot", 1);
  259.     cats.add_Animal(3, "Kubus", "Kot", 1);
  260.     cats.add_Animal(1, "Mejsonek", "Kot", 3);
  261.     cats.print();
  262.  
  263.     printf("\n\n\n");
  264.  
  265.     dogs->add_Animal(5, "Tina", "Pies", 1);
  266.     dogs->add_Animal(3, "Azor", "Pies", 2);
  267.     dogs->add_Animal(2, "As", "Pies", 2);
  268.     dogs->add_Animal(1, "Reksio", "Pies", 4);
  269.     dogs->print();
  270.     */
  271.  
  272.     /*
  273.     cout<<"Removing owner"<<endl;
  274.     cats.add_Animal(4, "Pola", "Kot", 2);
  275.     cats.add_Animal(2, "Dzirno", "Kot", 1);
  276.     cats.add_Animal(3, "Kubus", "Kot", 1);
  277.     cats.add_Animal(1, "Mejsonek", "Kot", 3);
  278.     cats.remove_Owner(1);
  279.     cats.print();
  280.  
  281.     printf("\n\n\n");
  282.  
  283.     dogs->add_Animal(5, "Tina", "Pies", 1);
  284.     dogs->add_Animal(3, "Azor", "Pies", 2);
  285.     dogs->add_Animal(2, "As", "Pies", 2);
  286.     dogs->add_Animal(1, "Reksio", "Pies", 4);
  287.     dogs->remove_Owner(2);
  288.     dogs->print();
  289.     */
  290.  
  291.     /*
  292.     cout<<"Modyfing owner_id"<<endl;
  293.     cats.add_Animal(1, "Pola", "Kot", 2);
  294.     cats.add_Animal(2, "Dzirno", "Kot", 1);
  295.     cats.add_Animal(3, "Kubus", "Kot", 1);
  296.     cats.add_Animal(4, "Mejsonek", "Kot", 3);
  297.     cats.modify("Tom", "Kot",1) = 5;
  298.     cats.print();
  299.  
  300.     printf("\n\n\n");
  301.  
  302.     dogs->add_Animal(1, "Tina", "Pies", 1);
  303.     dogs->add_Animal(2, "Azor", "Pies", 2);
  304.     dogs->add_Animal(3, "As", "Pies", 2);
  305.     dogs->add_Animal(4, "Reksio", "Pies", 4);
  306.     dogs->modify("Scooby", "Pies", 1) = 10;
  307.     dogs->print();
  308.     */
  309.  
  310.     /*
  311.     cout<<"Adding by Modyfing"<<endl;
  312.     cats.add_Animal(1, "Pola", "Kot", 2);
  313.     cats.add_Animal(4, "Mejsonek", "Kot", 3);
  314.     cats.modify("Tom", "Kot",3) = 5;
  315.     cats.print();
  316.  
  317.     printf("\n\n\n");
  318.  
  319.     dogs->add_Animal(1, "Tina", "Pies", 1);
  320.     dogs->add_Animal(4, "Reksio", "Pies", 4);
  321.     dogs->modify("Scooby", "Pies", 2) = 10;
  322.     dogs->print();
  323.     getchar();
  324.     */
  325.  
  326.     /*
  327.     cout<<"Cannot add to many spieces so modify first occurance"<<endl;
  328.     cats.add_Animal(1, "Pola", "Kot", 2);
  329.     cats.add_Animal(2, "Dzirno", "Kot", 1);
  330.     cats.add_Animal(3, "Kubus", "Kot", 1);
  331.     cats.add_Animal(4, "Mejsonek", "Kot", 3);
  332.     cats.modify("Tom", "Kot",5) = 5;
  333.     cats.print();
  334.  
  335.     printf("\n\n\n");
  336.  
  337.     dogs->add_Animal(1, "Tina", "Pies", 1);
  338.     dogs->add_Animal(2, "Azor", "Pies", 2);
  339.     dogs->add_Animal(3, "As", "Pies", 2);
  340.     dogs->add_Animal(4, "Reksio", "Pies", 4);
  341.     dogs->modify("Scooby", "Pies", 5) = 10;
  342.     dogs->print();
  343.     */
  344.  
  345.     getchar();
  346.     return 0;
  347. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement