Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- //head no longer global
- //write the main
- //make constructor and destructor and write inside them how tehy are created
- //use at least 2 different object of classes (dynamic object by new and automated by )
- class Animal {
- private:
- int id;
- char *name;
- char *spieces;
- int owner_id;
- Animal *next;
- Animal(int, char *, char *, int);
- ~Animal();
- void print();
- friend class AnimalList;
- };
- Animal::Animal(int idSet, char *nameSet, char *spiecesSet, int owner_idSet) {
- cout << "Animal constructor called" << endl;
- id = idSet;
- name = new char[strlen(nameSet) + 1];
- strcpy(name, nameSet);
- spieces = new char[strlen(spiecesSet) + 1];
- strcpy(spieces, spiecesSet);
- owner_id = owner_idSet;
- next = NULL;
- }
- Animal::~Animal() {
- cout << "Animal destructor called" << endl;
- delete[] name;
- delete[] spieces;
- }
- void Animal::print() {
- cout << "ID: " << id << endl;
- cout << "Name: " << name << endl;
- cout << "Spieces: " << spieces << endl;
- cout << "Owner ID: " << owner_id << endl;
- printf("\n");
- }
- //-------------------------------------------------------------------
- class AnimalList {
- public:
- Animal* first;
- AnimalList();//create empty list
- ~AnimalList();//delete all list
- void add_Animal(int, char *, char *, int);
- void remove_Owner(int);
- void print();
- int &modify(char* name,char* spieces, int id);
- };
- AnimalList::AnimalList() {
- cout << "AnimalList constructor called" << endl;
- first=NULL;
- }
- AnimalList::~AnimalList() {
- cout << "AnimalList destructor called" << endl;
- if (first == NULL) {
- cout << "first is NULL" << endl;
- return;
- } else {
- Animal *tmp = first;
- while (tmp != NULL) {
- Animal *deleteMe = tmp;
- tmp = tmp->next;
- delete deleteMe;
- }
- first = NULL;
- }
- }
- void AnimalList::add_Animal(int id, char *name, char *spieces, int owner_id) {
- if(first==NULL)
- {
- }else
- {
- Animal *tmp2 = first;
- while (tmp2 != NULL) {
- if (tmp2->id == id) {
- cout << "Cannot add animal ID Repeat" << endl;
- return;
- }
- tmp2 = tmp2->next;
- }
- }
- Animal *newCharacter = new Animal(id, name, spieces, owner_id);
- if (first == NULL) {
- first = newCharacter;
- newCharacter->next = NULL;
- cout << "added first element" << endl;
- return;
- } else {
- Animal *tmp = first;
- if(tmp->id > newCharacter->id)
- {
- newCharacter->next = tmp;
- first = newCharacter;
- cout<<"added as first in not empty list"<<endl;
- return;
- }
- while (tmp->next != NULL) {
- if (newCharacter->id < tmp->next->id) {
- newCharacter->next = tmp->next;
- tmp->next = newCharacter;
- cout << "added element in between" << endl;
- return;
- }
- tmp = tmp->next;
- }
- tmp->next = newCharacter;
- newCharacter->next = NULL;
- cout << "added element as last" << endl;
- }
- }
- void AnimalList::print() {
- if(first==NULL)
- {
- cout<<"List is empty"<<endl;
- return;
- }
- Animal *tmp = first;
- while (tmp != NULL) {
- cout << "ID: " << tmp->id << endl;
- cout << "Name: " << tmp->name << endl;
- cout << "Spieces: " << tmp->spieces << endl;
- cout << "Owner ID: " << tmp->owner_id << endl;
- printf("\n");
- tmp = tmp->next;
- }
- }
- void AnimalList::remove_Owner(int ownerIdToRemove) {
- if(first==NULL)
- {
- cout<<"List is empty"<<endl;
- return;
- }
- Animal *tmp = first;
- bool deleted;
- while (tmp->next != NULL) {
- deleted = false;
- if (tmp->next->owner_id == ownerIdToRemove) //DESTROY HIM
- {
- Animal *deleteMe = tmp->next;
- if(tmp->next->next != NULL)
- {
- tmp->next = tmp->next->next;
- }else
- {
- tmp->next = NULL;
- }
- delete deleteMe;
- deleted = true;
- }
- if (!deleted) {
- tmp = tmp->next;
- }
- }
- if(first->owner_id == ownerIdToRemove)
- {
- if(first->next != NULL)
- {
- delete first;
- first = first->next;
- }else
- {
- delete first;
- first = NULL;
- }
- }
- }
- int &AnimalList::modify(char* name,char* spieces, int id)
- {
- Animal *tmp = first;
- int counter = 0;
- int *firstSpiecesOwner=NULL;
- while(tmp!=NULL)
- {
- if(tmp->id == id)
- {
- return tmp->owner_id;
- }
- if(strcmp(spieces,tmp->spieces) == 0)
- {
- if(firstSpiecesOwner==NULL)
- {
- firstSpiecesOwner = &tmp->owner_id;
- }
- counter++;
- }
- tmp = tmp->next;
- } //if ID was not found
- if(counter <= 3)
- {
- add_Animal(id,name,spieces,0);
- tmp = first;
- while(tmp!=NULL)
- {
- if(tmp->id == id)
- {
- return tmp->owner_id;
- }
- tmp = tmp->next;
- }
- }else
- {
- return *firstSpiecesOwner;
- }
- }
- int main() {
- AnimalList cats;
- AnimalList* dogs = new AnimalList;
- /*
- cout<<"Deleting from empty list"<<endl;
- cats.remove_Owner(1);
- dogs->remove_Owner(1);
- */
- /*
- cout<<"Adding in unsorted order"<<endl;
- cats.add_Animal(4, "Pola", "Kot", 2);
- cats.add_Animal(2, "Dzirno", "Kot", 1);
- cats.add_Animal(3, "Kubus", "Kot", 1);
- cats.add_Animal(1, "Mejsonek", "Kot", 3);
- cats.print();
- printf("\n\n\n");
- dogs->add_Animal(5, "Tina", "Pies", 1);
- dogs->add_Animal(3, "Azor", "Pies", 2);
- dogs->add_Animal(2, "As", "Pies", 2);
- dogs->add_Animal(1, "Reksio", "Pies", 4);
- dogs->print();
- */
- /*
- cout<<"Removing owner"<<endl;
- cats.add_Animal(4, "Pola", "Kot", 2);
- cats.add_Animal(2, "Dzirno", "Kot", 1);
- cats.add_Animal(3, "Kubus", "Kot", 1);
- cats.add_Animal(1, "Mejsonek", "Kot", 3);
- cats.remove_Owner(1);
- cats.print();
- printf("\n\n\n");
- dogs->add_Animal(5, "Tina", "Pies", 1);
- dogs->add_Animal(3, "Azor", "Pies", 2);
- dogs->add_Animal(2, "As", "Pies", 2);
- dogs->add_Animal(1, "Reksio", "Pies", 4);
- dogs->remove_Owner(2);
- dogs->print();
- */
- /*
- cout<<"Modyfing owner_id"<<endl;
- cats.add_Animal(1, "Pola", "Kot", 2);
- cats.add_Animal(2, "Dzirno", "Kot", 1);
- cats.add_Animal(3, "Kubus", "Kot", 1);
- cats.add_Animal(4, "Mejsonek", "Kot", 3);
- cats.modify("Tom", "Kot",1) = 5;
- cats.print();
- printf("\n\n\n");
- dogs->add_Animal(1, "Tina", "Pies", 1);
- dogs->add_Animal(2, "Azor", "Pies", 2);
- dogs->add_Animal(3, "As", "Pies", 2);
- dogs->add_Animal(4, "Reksio", "Pies", 4);
- dogs->modify("Scooby", "Pies", 1) = 10;
- dogs->print();
- */
- /*
- cout<<"Adding by Modyfing"<<endl;
- cats.add_Animal(1, "Pola", "Kot", 2);
- cats.add_Animal(4, "Mejsonek", "Kot", 3);
- cats.modify("Tom", "Kot",3) = 5;
- cats.print();
- printf("\n\n\n");
- dogs->add_Animal(1, "Tina", "Pies", 1);
- dogs->add_Animal(4, "Reksio", "Pies", 4);
- dogs->modify("Scooby", "Pies", 2) = 10;
- dogs->print();
- getchar();
- */
- /*
- cout<<"Cannot add to many spieces so modify first occurance"<<endl;
- cats.add_Animal(1, "Pola", "Kot", 2);
- cats.add_Animal(2, "Dzirno", "Kot", 1);
- cats.add_Animal(3, "Kubus", "Kot", 1);
- cats.add_Animal(4, "Mejsonek", "Kot", 3);
- cats.modify("Tom", "Kot",5) = 5;
- cats.print();
- printf("\n\n\n");
- dogs->add_Animal(1, "Tina", "Pies", 1);
- dogs->add_Animal(2, "Azor", "Pies", 2);
- dogs->add_Animal(3, "As", "Pies", 2);
- dogs->add_Animal(4, "Reksio", "Pies", 4);
- dogs->modify("Scooby", "Pies", 5) = 10;
- dogs->print();
- */
- getchar();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement