Prompt Detail:
Код на C++:
cpp#include <iostream>
#include <vector>
using namespace std;
class Shape {
public:
virtual void draw() = 0;
};
class Rectangle : public Shape {
private:
int width;
int height;
public:
Rectangle(int width, int height) {
this->width = width;
this->height = height;
}
virtual void draw() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
cout << "#";
}
cout << endl;
}
}
};
class Circle : public Shape {
private:
int radius;
public:
Circle(int radius) {
this->radius = radius;
}
virtual void draw() {
int diameter = radius * 2;
for (int i = 0; i <= diameter; i++) {
for (int j = 0; j <= diameter; j++) {
int distance = (i - radius) * (i - radius) + (j - radius) * (j - radius);
if (distance <= (radius * radius)) {
cout << "#";
}
else {
cout << ".";
}
}
cout << endl;
}
}
};
class HollowRectangle : public Rectangle {
public:
HollowRectangle(int width, int height) : Rectangle(width, height) {}
void draw() {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (i == 0 || i == height - 1 || j == 0 || j == width - 1) {
cout << "#";
}
else {
cout << ".";
}
}
cout << endl;
}
}
};
class HollowCircle : public Circle {
public:
HollowCircle(int radius) : Circle(radius) {}
void draw() {
int diameter = radius * 2;
for (int i = 0; i <= diameter; i++) {
for (int j = 0; j <= diameter; j++) {
int distance = (i - radius) * (i - radius) + (j - radius) * (j - radius);
if (distance <= (radius * radius) && distance > ((radius - 1) * (radius - 1))) {
cout << "#";
}
else {
cout << ".";
}
}
cout << endl;
}
}
};
int main() {
int n;
cin >> n;
vector<Shape*> shapes;
for (int i = 0; i < n; i++) {
string type;
cin >> type;
if (type == "Rectangle") {
int width, height;
cin >> width >> height;
shapes.push_back(new Rectangle(width, height));
}
else if (type == "Circle") {
int radius;
cin >> radius;
shapes.push_back(new Circle(radius));
}
else if (type == "HollowRectangle") {
int width, height;
cin >> width >> height;
shapes.push_back(new HollowRectangle(width, height));
}
else if (type == "HollowCircle") {
int radius;
cin >> radius;
shapes.push_back(new HollowCircle(radius));
}
}
for (int i = 0; i < shapes.size(); i++) {
shapes[i]->draw();
if (i != shapes.size() - 1) {
cout << endl;
}
Add a comment