Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef INFO_H
- #define INFO_H
- #include <iostream>
- using std::string;
- struct place_size
- {
- int x;
- int y;
- };
- class Info
- {
- public:
- Info();
- Info(const Info &otherInfo);
- virtual ~Info();
- void addName(string name);
- void addPlace(place_size place);
- string getName() const;
- place_size getPlace() const;
- virtual int defineChild(){return 0;}
- protected:
- string _name;
- place_size _place;
- };
- #endif // INFO_H
- ___________________________________________________
- #ifndef CAR_H
- #define CAR_H
- #include <iostream>
- #include "info.h"
- #include "math.h"
- using std::string;
- class Car : public Info
- {
- public:
- Car() : Info()
- {
- _name = "Default String";
- _place.x = 0;
- _place.y = 0;
- _size.x = 1;
- _size.y = 1;
- _angle = 0;
- }
- Car(string name, place_size place, place_size size, double angle) : Info(name,,angle)
- {
- _name = "Default String";
- _place.x = 0;
- _place.y = 0;
- _size.x = 1;
- _size.y = 1;
- _angle = 0;
- addName(name);
- addPlace(place);
- addSize(size);
- addAngle(angle);
- }
- Car(const Car& otherCar) : Info(otherCar)
- {
- _name = otherCar._name;
- _place.x = otherCar._place.x;
- _place.y = otherCar._place.y;
- _size.x = otherCar._size.x;
- _size.y = otherCar._size.y;
- _angle = otherCar._angle;
- }
- inline void addSize(place_size size)
- {
- if (size.x > 0 && size.y > 0)
- {
- _size.x = size.x;
- _size.y = size.y;
- }
- }
- inline void addAngle(double angle)
- {
- if (angle >= 0 && angle < M_PI*2) // pi*2
- _angle = angle;
- }
- place_size getSize() const;
- double getAngle() const;
- bool check(const Car& otherCar) const;
- virtual ~Car();
- virtual int defineChild(){return 1;}
- private:
- double _angle;
- place_size _size;
- };
- #endif // CAR_H
- _________________________________________________
- #ifndef PODIUM_H
- #define PODIUM_H
- #include "info.h"
- class Podium : public Info
- {
- public:
- Podium() : Info()
- {
- _name = "Default String";
- _place.x = 0;
- _place.y = 0;
- _radius = 1;
- }
- Podium(string name, place_size place, int radius) : Info(name,place,radius)
- {
- _name = "Default String";
- _place.x = 0;
- _place.y = 0;
- _radius = 1;
- addName(name);
- addPlace(place);
- addRadius(radius);
- }
- Podium(const Podium& otherPodium) : Info(otherPodium)
- {
- _name = otherPodium._name;
- _place.x = otherPodium._place.x;
- _place.y = otherPodium._place.y;
- _radius = otherPodium._radius;
- }
- void addRadius(int radius);
- int getRadius() const;
- virtual ~Podium();
- virtual int defineChild(){return 2;}
- private:
- int _radius;
- };
- #endif // PODIUM_H
- ____________________________________________
- #ifndef ARRAY_H
- #define ARRAY_H
- #include "info.h"
- #include "car.h"
- #include "podium.h"
- class Array
- {
- public:
- Array();
- Array(place_size size);
- Array(const Array &prototype);
- ~Array();
- void insert(Info &otherInfo, int position);
- void insert(Info &otherInfo); // Метод добавления элементов машин
- void deleteRecord(int position); // Метод удаления машин
- void deleteAll(); // Метод, удаляющий все машины
- int getLen() const; // Метод, возвращающий число хранящихся машин
- Info* getObject(int position) const;
- bool compare(Array &container) const;
- bool checkCar(int position, Info *otherInfo); // метод проверки на наличие контактов между автомобилями и стенами автосалона
- bool checkPodium(int position, Info *otherInfo); // метод проверки на наличие контактов между подиумом и стенами автосалона
- void writeToFile(const std:: string &path) const;
- void readFromFile(const std:: string &path);
- Car convertToCar(Info &otherInfo) const;
- Podium convertToPodium(Info &otherInfo) const;
- private:
- Info **_arr;
- int _len;
- int _arrSize;
- place_size _size;
- };
- #endif // ARRAY_H
- _____________________________________________
- #include "array.h"
- #include <fstream>
- #include <cmath>
- Array::Array()
- {
- _arrSize = 10;
- _arr = new Info*[_arrSize];
- _len = 0;
- _size.x = 1000;
- _size.y = 1000;
- }
- Array::Array(place_size size)
- {
- _arrSize = 10;
- _arr = new Info*[_arrSize];
- _len = _arrSize;
- _size.x = size.x;
- _size.y = size.y;
- }
- Array::Array(const Array& prototype)
- {
- _len = prototype.getLen();
- _arrSize = prototype._arrSize;
- _arr = new Info*[_arrSize];
- for(int i = 0; i < _len; i++)
- {
- Info *otherInfo = prototype.getObject(i);
- if (otherInfo->defineChild() == 1)
- {
- Car *helper = dynamic_cast<Car*>(otherInfo);
- Car *newRecord = new Car(*helper);
- _arr[i] = newRecord;
- }
- else if (otherInfo->defineChild() == 2)
- {
- Podium *helper = dynamic_cast<Podium*>(otherInfo);
- Podium *newRecord = new Podium(*helper);
- _arr[i] = newRecord;
- }
- }
- }
- void Array::insert(Info &otherInfo, int position)
- {
- if (position > _len)
- position = _len;
- if (position == _len){
- if (otherInfo.defineChild() == 1)
- {
- Car *helper = dynamic_cast<Car*>(&otherInfo);
- Car *newRecord = new Car(*helper);
- _arr[position] = newRecord;
- }
- else if (otherInfo.defineChild() == 2)
- {
- Podium *helper = dynamic_cast<Podium*>(&otherInfo);
- Podium *newRecord = new Podium(*helper);
- _arr[position] = newRecord;
- }
- }
- else
- {
- for (int i = _len; i > position; i--)
- {
- _arr[i] = _arr[i-1]; // move array
- }
- if (otherInfo.defineChild() == 1)
- {
- Car *helper = dynamic_cast<Car*>(&otherInfo);
- Car *newRecord = new Car(*helper);
- _arr[position] = newRecord;
- }
- else if (otherInfo.defineChild() == 2)
- {
- Podium *helper = dynamic_cast<Podium*>(&otherInfo);
- Podium *newRecord = new Podium(*helper);
- _arr[position] = newRecord;
- }
- }
- _len++;
- }
- bool check(const Car& car1, const Car& car2)
- {
- float r1, r2;
- r1 = sqrt(pow(float(car1.getSize().x) / 2, 2) + pow(float(car1.getSize().y) / 2, 2))+0.5;
- r2 = sqrt(pow(float(car2.getSize().x) / 2, 2) + pow(float(car2.getSize().y) / 2, 2))+0.5;
- return (r1 + r2) < sqrt(pow(float(car1.getSize().x - car2.getSize().x), 2) + pow(float(car1.getSize().y - car2.getSize().y), 2));
- }
- bool Array::checkCar(int position, Info *otherInfo)
- {
- Car *p = nullptr;
- p = dynamic_cast<Car*>(otherInfo);
- float r = sqrt(pow(float(p->getSize().x)/2, 2) + pow(float(p->getSize().y)/2, 2))+0.5;
- if ((p->getPlace().x + r >= _size.x) || (p->getPlace().y + r >= _size.y)|| (p->getPlace().x - r <= 0) || (p->getPlace().y - r <= 0))
- return false;
- for (int i=0; i < _len; i++)
- {
- if ((i != position) && (_arr[i]->getName() != "Default String")) //&& (!(otherCar.check(_arr[i]))))
- return false;
- }
- return true;
- }
- bool Array::checkPodium(int position, Info *otherInfo)
- {
- Podium *p = nullptr;
- p = dynamic_cast<Podium*>(otherInfo);
- if ((p->getPlace().x + p->getRadius() >= _size.x) || (p->getPlace().y + p->getRadius() >= _size.y)|| (p->getPlace().x - p->getRadius() <= 0) || (p->getPlace().y - p->getRadius() <= 0))
- return false;
- for (int i=0; i < _len; i++)
- {
- if ((i != position) && ((_arr)[i]->getName() != "Default String"))
- return false;
- }
- return true;
- }
- void Array::insert(Info &otherInfo)
- {
- insert(otherInfo, _len);
- }
- void Array::deleteRecord(int position)
- {
- if (position == _len-1)
- {
- delete (_arr[position]);
- }
- else
- {
- for (int i = position; i < _len-1; i++)
- {
- if (_arr[i+1]->defineChild() == 1)
- {
- Car *helper = dynamic_cast<Car*>(_arr[i+1]);
- Car *newRecord = new Car(*helper);
- _arr[position] = newRecord;
- }
- else if (_arr[i+1]->defineChild() == 2)
- {
- Podium *helper = dynamic_cast<Podium*>(_arr[i+1]);
- Podium *newRecord = new Podium(*helper);
- _arr[position] = newRecord;
- }
- }
- delete (_arr[_len - 1]);
- }
- _len--;
- }
- void Array:: deleteAll()
- {
- for (int i = _len-1; i >=0 ; i--){
- delete (_arr[i]);
- }
- _len = 0;
- _arrSize = 10;
- _arr = new Info*[_arrSize];
- }
- Array:: ~Array()
- {
- for (int i = _len-1; i >=0; i--)
- {
- delete (_arr[i]);
- }
- _len = 0;
- _arrSize = 0;
- delete[](_arr);
- }
- int Array::getLen() const
- {
- return _len;
- }
- Info* Array::getObject(int position) const
- {
- return _arr[position];
- }
- void Array:: writeToFile(const string &path) const
- {
- std::ofstream stream;
- stream.open(path);
- if (stream.is_open())
- {
- for (int i = 0; i < _len; i++)
- {
- Info *otherInfo = _arr[i];
- stream << (*_arr[i]).defineChild() << '\n';
- if((*_arr[i]).defineChild() == 1)
- {
- Car* p1 = dynamic_cast<Car*>(otherInfo);
- stream << p1 ->getName() << '\n';
- stream << p1 ->getPlace().x << '\n';
- stream << p1 ->getPlace().y << '\n';
- stream << p1 ->getSize().x << '\n';
- stream << p1 ->getSize().y << '\n';
- stream << p1 ->getAngle() << '\n';
- }
- else if ((*_arr[i]).defineChild() == 2)
- {
- Podium* p2 = dynamic_cast<Podium*>(otherInfo);
- stream << p2 ->getName() << '\n';
- stream << p2 ->getPlace().x << '\n';
- stream << p2 ->getPlace().y << '\n';
- stream << p2 ->getRadius() << '\n';
- }
- }
- }
- stream.flush();
- stream.close();
- }
- void Array:: readFromFile(const string &path)
- {
- std::ifstream stream;
- char buffer[50];
- string name;
- place_size placeXY;
- place_size sizeXY;
- double angle;
- int radius;
- int elementDefinition;
- stream.open(path);
- if (stream.is_open())
- {
- for (int i = 0; !stream.eof(); i++)
- {
- stream.getline(buffer,50);
- elementDefinition = atoi(buffer);
- if (elementDefinition == 1)
- {
- stream.getline(buffer,50);
- name = string(buffer);
- stream.getline(buffer,50);
- placeXY.x = atoi(buffer);
- stream.getline(buffer,50);
- placeXY.y = atoi(buffer);
- stream.getline(buffer,50);
- sizeXY.x = atoi(buffer);
- stream.getline(buffer,50);
- sizeXY.y = atoi(buffer);
- stream.getline(buffer,50);
- angle = atof(buffer);
- Car otherCar = Car(name,placeXY,sizeXY,angle);
- this->insert(otherCar);
- }
- else if (elementDefinition == 2)
- {
- stream.getline(buffer,50);
- name = string(buffer);
- stream.getline(buffer,50);
- placeXY.x = atoi(buffer);
- stream.getline(buffer,50);
- placeXY.y = atoi(buffer);
- stream.getline(buffer,50);
- radius = atoi(buffer);
- Podium otherPodium = Podium(name, placeXY, radius);
- this->insert(otherPodium);
- }
- }
- }
- }
- /*
- bool Array::compare(Array &container) const
- {
- for (int i=0; i < container._len; i++)
- {
- Info *firstRecord = container.getObject(i);
- Info *secondRecord = this->getObject(i);
- if (firstRecord->defineChild() == 1 && secondRecord->defineChild()==1)
- {
- Car* ptr1 = dynamic_cast<Car*>(firstRecord);
- Car* ptr2 = dynamic_cast<Car*>(secondRecord);
- if (ptr1->getName().compare(ptr2->getName())!=0)
- return false;
- if (ptr1->getPlace().x.compare(ptr2->getPlace().x)!=0)
- return false;
- if (ptr1->getPlace().y.compare(ptr2->getPlace().y)!=0)
- return false;
- if (ptr1->getSize().x.compare(ptr2->getSize().x)!=0)
- return false;
- if (ptr1->getSize().y )
- return false;
- else if (firstRecord->defineChild() == 2 && secondRecord->defineChild() == 2)
- {
- Podium* ptr1 = dynamic_cast<Podium*>(firstRecord);
- Podium* ptr2 = dynamic_cast<Podium*>(secondRecord);
- }
- else return false;
- }
- return true;
- }
- }
- */
- _______________________________________________________
- #include "car.h"
- place_size Car::getSize() const
- {
- //Селектор поля size
- //Ничего не принимает
- //Возвращает значение поля size
- return _size;
- }
- double Car::getAngle() const
- {
- //Селектор поля angle
- //Ничего не принимает
- //Возвращает значение поля angle
- return _angle;
- }
- bool Car::check(const Car &otherCar) const
- {
- float r1, r2;
- r1 = sqrt(pow(float(_size.x) / 2, 2) + pow(float(_size.y) / 2, 2))+0.5;
- r2 = sqrt(pow(float(otherCar.getSize().x) / 2, 2) + pow(float(otherCar.getSize().y) / 2, 2))+0.5;
- return (r1 + r2) < sqrt(pow(float(_place.x - otherCar.getPlace().x), 2) + pow(float(_place.y - otherCar.getPlace().y), 2));
- }
- ______________________________________________________
- #include "info.h"
- void Info::addName(string name)
- {
- //Модификатор поля name
- //Принимает значение поля name
- //Ничего не возвращает
- if (name != "")
- _name = name;
- }
- void Info::addPlace(place_size place)
- {
- //Модификатор поля place
- //Принимает значение поля place
- //Ничего не возвращает
- if (place.x >= 0 && place.x <= 100 && place.y >= 0 && place.y <= 100)
- {
- _place = place;
- }
- }
- string Info::getName() const
- {
- //Селектор поля name
- //Ничего не принимает
- //Возвращает значение поля name
- return _name;
- }
- place_size Info::getPlace() const
- {
- //Селектор поля place
- //Ничего не принимает
- //Возвращает значение поля place
- return _place;
- }
- __________________________________________________
- #include "podium.h"
- void Podium::addRadius(int radius)
- {
- //Модификатор поля radius
- //Принимает значение поля radius
- //Ничего не возвращает
- if (radius > 0 )
- _radius = radius;
- }
- int Podium::getRadius() const
- {
- //Селектор поля radius
- //Ничего не принимает
- //Возвращает значение поля radius
- return _radius;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement