Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 190 additions & 5 deletions animals/animal.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,192 @@
#include "animal.h"

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

int main() {
return 0;
}


class Animal {
private:
float the_average_value_of_the_duration_of_life;
public:
void set_the_average_value_of_the_duration_of_life(float new_the_average_value_of_the_duration_of_life) { the_average_value_of_the_duration_of_life = new_the_average_value_of_the_duration_of_life; }
float get_the_average_value_of_the_duration_of_life() const { return the_average_value_of_the_duration_of_life; }

bool Gender;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не выполнен пункт с protected.

virtual string about() const;
protected:
Animal();

};

string Animal::about() const {
stringstream ss;
ss << "Gender = " << " " << Gender;
return ss.str();
};

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Отсутствует перегрузка операции вывода в поток для Animal.

class Mammal : public Animal {
private:
int number_of_individuals;
public:
void set_number_of_individuals(int new_number_of_individuals) { number_of_individuals = new_number_of_individuals; }
int get_number_of_individuals() const { return number_of_individuals; }

string Coat_color;
virtual string about() const;
protected:
Mammal();
};

string Mammal::about() const {
stringstream ss;
ss << Animal::about() << " " << " Coat_color = " << " " << Coat_color;
return ss.str();
};



class Quadrupeds : public Mammal {
private:
float percentage_of_quality_of_life;
public:
void set_percentage_of_quality_of_life(float new_percentage_of_quality_of_life) { percentage_of_quality_of_life = new_percentage_of_quality_of_life; }
float get_percentage_of_quality_of_life() const { return percentage_of_quality_of_life; }

bool limbs;
virtual string about() const;
};

string Quadrupeds::about() const {
stringstream ss;
ss << Animal::about() << " " << " limbs = " << " " << limbs;
return ss.str();
};


class Birds : public Mammal {
private:
int the_average_value_of_the_flight_for_one_season;
public:
void set_the_average_value_of_the_flight_for_one_season(int new_the_average_value_of_the_flight_for_one_season) { the_average_value_of_the_flight_for_one_season = new_the_average_value_of_the_flight_for_one_season; }
int get_the_average_value_of_the_flight_for_one_season() const { return the_average_value_of_the_flight_for_one_season; }


bool ability_to_fly;
virtual string about() const;
};

string Birds::about() const {
stringstream ss;
ss << Mammal::about() << " " << " ability_to_fly = " << " " << ability_to_fly;
return ss.str();
};

class Cat : public Animal {
private:
int the_number_of_mice_caught_per_unit_of_time;
public:
void set_the_number_of_mice_caught_per_unit_of_time(int new_the_number_of_mice_caught_per_unit_of_time) { the_number_of_mice_caught_per_unit_of_time = new_the_number_of_mice_caught_per_unit_of_time; }
int get_the_number_of_mice_caught_per_unit_of_time() const { return the_number_of_mice_caught_per_unit_of_time; }


float vibrissaLength;
virtual string about() const;
};

string Cat::about() const {
stringstream ss;
ss << Animal::about() << " " << " vibrissaLength = " << " " << vibrissaLength;
return ss.str();
};

class Manul : public Cat {
private:
int average_weight;
public:
void set_average_weight(int new_average_weight) { average_weight = new_average_weight; }
int get_average_weight() const { return average_weight; }


float Average_length_of_wool;
virtual string about() const;
};

string Manul::about() const {
stringstream ss;
ss << Cat::about() << " " << " Average_length_of_wool = " << " " << Average_length_of_wool;
return ss.str();
};

class Mainkun : public Cat {
private:
string eye_color;
public:
void set_eye_color(string new_eye_color) { eye_color = new_eye_color; }
string get_eye_color() const { return eye_color; }

float Number_of_fleas;
virtual string about() const;
};

string Mainkun::about() const {
stringstream ss;
ss << Cat::about() << " " << " Number_of_fleas = " << " " << Number_of_fleas;
return ss.str();
};


Animal::Animal()
: Gender()
, the_average_value_of_the_duration_of_life()
{
cerr << "" << endl;
}

Mammal::Mammal()
: number_of_individuals()
, Coat_color()
{
cerr << "" << endl;
}

inline ostream& operator <<(ostream& os, const Animal& animal) {
return os << animal.about();
}

int main(){
Mainkun kot_bOris;
Manul kot_Vasily;
Cat Roudi;
Birds ANgry_birds;
Quadrupeds dogs;

kot_bOris.Gender = true;
kot_bOris.Number_of_fleas = 50000;
kot_bOris.vibrissaLength = 5;

kot_Vasily.Average_length_of_wool = 3;
kot_Vasily.Gender = true;
kot_Vasily.vibrissaLength = 6;

Roudi.Gender = true;
Roudi.vibrissaLength = 4;

ANgry_birds.ability_to_fly = true;
ANgry_birds.Coat_color = "red";
ANgry_birds.Gender = false;

dogs.Coat_color = "red";
dogs.Gender = true;
dogs.limbs = 4;

cout << "-------------------------------------------------------------" << endl;
cout << "kot_bOris: " << kot_bOris << endl;
cout << "kot_Vasily: " << kot_Vasily << endl;
cout << "Roudi: " << Roudi << endl;
cout << "ANgry_birds: " << ANgry_birds << endl;
cout << "dogs: " << dogs << endl;
cout << "-------------------------------------------------------------";


}
109 changes: 85 additions & 24 deletions classwork/tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,47 +80,94 @@ class Tree {
}
}

void SmallTurnLeft(Node *upper, Node *lower) {
lower -> parent = upper -> parent;
lower -> left = upper;
upper -> parent = lower;
if(lower -> parent != nullptr){
if(lower ->parent ->left == upper ) {lower ->parent ->left = lower;}
else {lower ->parent -> right = lower;}
void SmallTurnLeft(Node* upper, Node* lower) {
lower->parent = upper->parent;
lower->left = upper;
upper->parent = lower;
if (lower->parent != nullptr) {
if (lower->parent->left == upper) { lower->parent->left = lower; }
else { lower->parent->right = lower; }
}
}
};

public:
class iterator {
Node *node;
iterator(Node *n) : node(n) {}
friend class Tree;
public:
T& operator *() { return node->data; }
iterator& operator ++() { // ++a lvalue
//-----------
class other_iterator {
protected:
Node* node;
other_iterator(Node* n) : node(n) {}

other_iterator& next() {
if (node->right) {
node = node->right;
while (node->left)
node = node->left;
} else {
}
else {
while (node->parent && node->parent->right == node)
node = node->parent;
node = node->parent;
}
return *this
}
other_iterator& prev() {
if (node->left) {
node = node->left;
while (node->right)
node = node->right;
}
else {
while (node->parent && node->parent->left == node)
node = node->parent;
node = node->parent;
}
return *this;
}
};
///-----

public:
class iterator {
Node *node;
iterator(Node *n) : node(n) {}
friend class Tree;
public:
T& operator *() { return node->data; }
iterator& operator ++() { // ++a lvalue
next();
}
iterator operator ++(int) { // a++ rvalue
iterator prev = *this;
++(*this);
return prev;
}
iterator& operator --() {
auto temp = *this;
--(*this);
return temp;
}
iterator operator --(int) { node->prev(); return *this }
};

class reverse_iterator : public other_iterator {
reverse_iterator(Node* n) : other_iterator(n) {}

friend class Tree;

public:
T& operator *() { return node->data; }
reverse_iterator& operator--() {
node->next(); return *this;
}
reverse_iterator operator--(int) {
auto temp = *this;
--(*this);
return temp;
}
iterator& operator --() { /* TODO */ }
iterator operator --(int) { /* TODO */ }
};

class reverse_iterator {
// TODO 1: Написать аналог iterator, работающий в обратном направлении.

// TODO 2: Вынести общий код iterator и reverse_iterator в базовый класс.
};

iterator begin() {
if (root == nullptr)
Expand All @@ -131,8 +178,15 @@ class Tree {
return iterator(node);
}
iterator end() { return iterator(nullptr); }
reverse_iterator rbegin() { /* TODO */ }
reverse_iterator rend() { /* TODO */ }
reverse_iterator rbegin() {
if (root == nullptr)
return rend();
auto node = root;
while (node->right)
node = node->right;
return reverse_iterator(node);
}
reverse_iterator rend() { return reverse_iterator(nullptr); }

Tree() : root(nullptr) {}

Expand Down Expand Up @@ -274,9 +328,16 @@ int main() {
tree.Insert(345, "Bleah");
tree.Insert(893, "Moah");
tree.Insert(043, "CooKoo");
tree.Insert(935, "Foobarbass");
tree.Insert(1000, "FooParam");
std::cout << "Printing tree:" << std::endl;
tree.Walk<std::ostream&>(PrintNodeToFile, std::cout);

cout << "-------------------" << endl;
auto it = tree.rbegin();
cout << *it << endl;
it--;
cout << *it << endl;
cout << "-------------------" << endl;
cout << "Vector:" << endl;
std::vector<int> myvector { 121, 443, 565, 323 };
for (size_t i = 0; i != myvector.size(); i++) {
Expand Down
Loading