Figure.cpp #include "Figure.h" #include <iostream> #include <fstream> #include <map>…
- User
Figure.cpp #include "Figure.h" #include #include #include #define _USE_MATH_DEFINES #include "math.h" #include using std::vector;
//класс круг Circle::Circle(Point center, int rad, int color):Figure(color),center(center),rad(rad) {}
double Circle::place() const { #ifdef DEBUG std::cerr << "In Circle::place()\n"; #endif return M_PI * rad * rad; }
double Circle::perimetr() const { #ifdef DEBUG std::cerr << "In Circle::perimetr()\n"; #endif return M_PI * 2 * rad; }
Figure::Point Circle::getCenter() { return center; }
double Circle::getRad() { return rad; }
void Circle::Print(std::ostream& stream) const { stream << "Color = " << getColor() << " CenterX = "; }
void Circle::readinFile(std::ostream& stream) { int x = getCenter().x; int y = getCenter().y; stream << "CIRCLE\n" << "Color = " << getColor() << "\n" << " CenterX = " << x << "\n" << " Center y" << y; } std::string Circle::writeinFile(std::ifstream& stream) { std::string line; while (stream >> line) { // Считывает данные из файла построчно std::cout << line << std::endl; // Выводит считанные данные в консоль } return line; }
//класс Треугольник Triangle::Triangle(Figure::Point p1, Figure::Point p2, Figure::Point p3, int color): Figure(color), p1(p2), p2(p2), p3(p3) {}
double Triangle::place() const { #ifdef DEBUG std::cerr << "In Triangle::place()\n"; #endif double ans = 0.5 * ((p1.x - p3.x) * (p2.y - p3.y) - (p2.x - p3.x) * (p1.y - p3.y)); if (ans < 0) ans = -ans; return ans; }
double Triangle::perimetr() const { #ifdef DEBUG std::cerr << "In Triangle::perimetr()\n"; #endif double dist1 = std::sqrt(std::pow(p1.x - p2.x, 2) +std::pow(p1.y - p2.y, 2)); //рсстояние двух точек на плоскости double dist2 = std::sqrt(std::pow(p2.x - p3.x, 2) + std::pow(p2.y - p3.y, 2)); double dist3 = std::sqrt(std::pow(p3.x - p1.x, 2) + std::pow(p3.y - p1.y, 2)); return dist1 + dist2 + dist3; }
Figure::Point Triangle::getP1() const { return p1; } Figure::Point Triangle::getP2() const { return p2; } Figure::Point Triangle::getP3() const { return p3; }
void Triangle::readinFile(std::ostream& stream) { stream << "TRIANGLE\n" << "Color = " << getColor() << "Point1x = " << getP1().x << "Point1y = " << getP1().y; }
std::string Triangle::writeinFile(std::ifstream& stream) { return std::string(); }
void Triangle::Print(std::ostream& stream) const { stream << "Color = " << getColor() << "Point1x = " << getP1().x << "Point1y = " << getP1().y; }
//класс Полигон Polygon::Polygon(vector points, int numPoints, int color) : Figure(color), points(points), numPoints(numPoints) {}
double Polygon::place() const { #ifdef DEBUG std::cerr << "In Polygon::place()\n"; #endif double area = 0.0; int n = points.size(); for (int i = 0; i < n; i++) { int j = (i + 1) % n; area += (points[i].x + points[j].x) * (points[i].y - points[j].y); } area /= 2.0; return std::abs(area); }
double Polygon::perimetr() const { #ifdef DEBUG std::cerr << "In Polygon::perimetr()\n"; #endif double perimeter = 0.0; for (int i = 0; i < numPoints; i++) { int j = (i + 1) % numPoints; double dist = std::sqrt(std::pow(points[i].x - points[j].x, 2) + std::pow(points[i].y - points[j].y, 2)); perimeter += dist; } return perimeter; }
vectorFigure::Point Polygon::getPoints() const { return points; }
int Polygon::getSize() const { return numPoints; }
void Polygon::Print(std::ostream& stream) const { stream << "Size = " << getSize(); }
void Polygon::readinFile(std::ostream& stream) { stream << "Size = " << getSize(); } std::string Polygon::writeinFile(std::ifstream& stream) { std::string line; while (stream >> line) { // Считывает данные из файла построчно std::cout << line << std::endl; // Выводит считанные данные в консоль } return line; }
Fugure.h #pragma once #include #include #include #include "singleton.h" using std::vector;
class Figure { protected: int color; virtual void Print(std::ostream& stream) const = 0; friend std::ostream& operator<<(std::ostream& stream, const Figure& f) { f.Print(stream); return stream; } public: Figure(int color) : color(color) {}; virtual double place() const = 0; virtual double perimetr() const = 0; struct Point { int x, y; Point(int x, int y) : x(x), y(y) {}; }; virtual std::string writeinFile(std::ifstream& stream) = 0; // }; class Circle : public Figure { Point center; int rad; public: Circle(Point center, int rad, int color); virtual double place() const; virtual double perimetr() const; Point getCenter(); double getRad(); int getColor() const { return color; } void readinFile(std::ostream& stream); // std::string writeinFile(std::ifstream& stream) override; // protected: void Print(std::ostream& stream) const; }; class Triangle : public Figure { Point p1, p2, p3; public: Triangle(Point p1, Point p2, Point p3, int color); virtual double place() const; virtual double perimetr() const; Point getP1() const; Point getP2() const; Point getP3() const; int getColor() const { return color; } void readinFile(std::ostream& stream); // virtual std::string writeinFile(std::ifstream& stream) override; // protected: void Print(std::ostream& stream) const; }; class Polygon : public Figure { vector<Point> points; int numPoints; public: Polygon(vector<Point> points, int numPoints, int color); virtual double place() const; virtual double perimetr() const; vector<Point> getPoints() const; int getSize() const; void readinFile(std::ostream& stream); // virtual std::string writeinFile(std::ifstream& stream) override; // protected: void Print(std::ostream& stream) const; };singleton.h #ifndef SINGLETON_H #define SINGLETON_H #include #include
template class Singleton { public: static T& instance() {
if (myInstance == 0) { myInstance = new T; } return *myInstance; } static void terminate() { if (myInstance != 0) { delete myInstance; //myInstance = 0; } }protected: static T* myInstance; Singleton() {} Singleton(const Singleton&) {} Singleton& operator=(const Singleton&) { return this; } // static std::map <std::string, T> instances; //static
}; template T* Singleton::myInstance = 0; #endif #pragma once
FiguresFactory.h #pragma once #include "singleton.h" #include "Figure.h" #include "string"
class FiguresFactory :public Singleton { public: typedef Figure* (*CreateFigureCallback)(); private: typedef std::map<int, CreateFigureCallback> CallbackMap; CallbackMap callbacks; public: bool RegisterFigure(int FigureID, CreateFigureCallback CreateFN) { return callbacks.insert(CallbackMap::value_type(FigureID, CreateFN)).second; }
bool UnregisterFigure(int FigureID); Figure* CreateFigure(int FigureID) //Figure* CreateFigure(int FigureID) { CallbackMap::const_iterator i = callbacks.find(FigureID); if (i == callbacks.end()) { throw std::runtime_error("Unknown typeID"); } return (i->second)(); //добавить параметры? return (i->second)(); } friend class Singleton<FiguresFactory>; Figure* CreateFigureFromFile(std::string str); };FiguresFactory.cpp #include "FiguresFactory.h" #include "Figure.h" #include #include #include #include
bool FiguresFactory::UnregisterFigure(int FigureID) { return callbacks.erase(FigureID) == 1; }
namespace {
Figure* CreateTriangle() { //создаём фигуру со слуцчайным параметров return new Triangle(Figure::Point(rand() % 100, rand() % 100), Figure::Point(rand() % 100, rand() % 100), Figure::Point(rand() % 100, rand() % 100), rand() % 100); } const int TRIANGLE = 1; //идентификатор типа //регистрируем нвоы тип фигуры const bool triangleRegistered = FiguresFactory::instance().RegisterFigure(TRIANGLE, CreateTriangle); Figure* CreatePolygon() { int n = rand() % 100; vector<Figure::Point> points; for (int j = 0; j < n; j++) { points.push_back(Figure::Point(rand() % 100, rand() % 100)); } return new Polygon(points, n, rand() % 100); } const int POLYGON = 2; const bool polygonRegistered = FiguresFactory::instance().RegisterFigure(POLYGON, CreatePolygon); поомги доабитьв каждый клас фигуры функцию записи в тестовый файл. Функции долэен передаваться один парметр-ссылка на объект ofstream (прив ызове функции предполагается что файл уже открыт длдя записи. Функция вначале записывает в файл идентификатор типа, а затм все его параметры.) Потом в каждый клас фигуры функцию чтения из тектового файл. Один параметр- ссылка на объкт ifstream
- Assistant
The modified code for the Figure class is as follows:
#include <iostream> #include <vector> class Figure { protected: int color; public: Figure(int color) : color(color) {} virtual double place() const = 0; virtual double perimetr() const = 0; struct Point { int x, y; Point(int x, int y) : x(x), y(y) {} }; virtual void writeToFile(std::ofstream& file) const = 0; virtual void readFromFile(std::ifstream& file) = 0; };The modified code for the Circle class is as follows:
class Circle : public Figure { private: Point center; int rad; public: Circle(Point center, int rad, int color) : Figure(color), center(center), rad(rad) {} double place() const override { return 3.14159 * rad * rad; } double perimetr() const override { return 2 * 3.14159 * rad; } void writeToFile(std::ofstream& file) const override { file << "Circle" << std::endl; file << "Color: " << color << std::endl; file << "Center: " << center.x << ", " << center.y << std::endl; file << "Radius: " << rad << std::endl; } void readFromFile(std::ifstream& file) override { std::string line; getline(file, line); // Read and ignore the line containing "Circle" getline(file, line); // Read the line containing the color color = std::stoi(line.substr(7)); getline(file, line); // Read the line containing the center int commaIndex = line.find(','); int centerX = std::stoi(line.substr(8, commaIndex - 8)); int centerY = std::stoi(line.substr(commaIndex + 2)); center = Point(centerX, centerY); getline(file, line); // Read the line containing the radius rad = std::stoi(line.substr(8)); } };The modified code for the Triangle class is as follows:
class Triangle : public Figure { private: Point p1, p2, p3; public: Triangle(Point p1, Point p2, Point p3, int color) : Figure(color), p1(p1), p2(p2), p3(p3) {} double place() const override { return 0.5 * abs((p1.x * (p2.y - p3.y) + p2.x * (p3.y - p1.y) + p3.x * (p1.y - p2.y))); } double perimetr() const override { double dist1 = sqrt(pow(p2.x - p1.x, 2) + pow(p2.y - p1.y, 2)); double dist2 = sqrt(pow(p3.x - p2.x, 2) + pow(p3.y - p2.y, 2)); double dist3 = sqrt(pow(p1.x - p3.x, 2) + pow(p1.y - p3.y, 2)); return dist1 + dist2 + dist3; } void writeToFile(std::ofstream& file) const override { file << "Triangle" << std::endl; file << "Color: " << color << std::endl; file << "Point 1: " << p1.x << ", " << p1.y << std::endl; file << "Point 2: " << p2.x << ", " << p2.y << std::endl; file << "Point 3: " << p3.x << ", " << p3.y << std::endl; } void readFromFile(std::ifstream& file) override { std::string line; getline(file, line); // Read and ignore the line containing "Triangle" getline(file, line); // Read the line containing the color color = std::stoi(line.substr(7)); getline(file, line); // Read the line containing point 1 int commaIndex = line.find(','); int p1X = std::stoi(line.substr(10, commaIndex - 10)); int p1Y = std::stoi(line.substr(commaIndex + 2)); p1 = Point(p1X, p1Y); getline(file, line); // Read the line containing point 2 commaIndex = line.find(','); int p2X = std::stoi(line.substr(10, commaIndex - 10)); int p2Y = std::stoi(line.substr(commaIndex + 2)); p2 = Point(p2X, p2Y); getline(file, line); // Read the line containing point 3 commaIndex = line.find(','); int p3X = std::stoi(line.substr(10, commaIndex - 10)); int p3Y = std::stoi(line.substr(commaIndex + 2)); p3 = Point(p3X, p3Y); } };The modified code for the Polygon class is as follows:
class Polygon : public Figure { private: std::vector<Point> points; public: Polygon(const std::vector<Point>& points, int color) : Figure(color), points(points) {} double place() const override { int n = points.size(); double area = 0.0; for (int i = 0; i < n; i++) { int j = (i + 1) % n; area += (points[i].x + points[j].x) * (points[i].y - points[j].y); } area /= 2.0; return abs(area); } double perimetr() const override { int n = points.size(); double perimeter = 0.0; for (int i = 0; i < n; i++) { int j = (i + 1) % n; double dist = sqrt(pow(points[i].x - points[j].x, 2) + pow(points[i].y - points[j].y, 2)); perimeter += dist; } return perimeter; } void writeToFile(std::ofstream& file) const override { file << "Polygon" << std::endl; file << "Color: " << color << std::endl; file << "Number of Points: " << points.size() << std::endl; for (const Point& point : points) { file << "Point: " << point.x << ", " << point.y << std::endl; } } void readFromFile(std::
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.