Prompt Title: C++

Created 1 year ago
Votes 0
Views 361 times
IT
0

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; }
avatar
BumBood
Shared 1 prompt
Created 1 year ago

Leave a Comment

Related Tag Prompts

0
0
C++ writer
1 year ago 2023-03-10 16:24:29 Daz
0
0
Unreal Engine Q&A.
1 year ago 2023-03-15 05:51:25 Mzee
0
0
C++ Matrix Constructor Error
1 year ago 2023-03-25 06:10:44 Ghazanfar
0
0
Convertir HTML a C++.
1 year ago 2023-03-31 13:34:27 igna
0
0
Robot Maze Solver.
1 year ago 2023-04-07 07:20:48 Constantin
0
0
Mastering C++17/20 Features
1 year ago 2023-04-08 16:00:23 Son Pham
0
0
获取时间戳差值
1 year ago 2023-04-11 08:04:01 v
0
0
PrinterQueue Management.
1 year ago 2023-05-02 18:33:27 vue
0
0
教学
1 year ago 2023-05-07 12:01:15 xly
0
0
Advantages of Enums & Constexpr
1 year ago 2023-07-26 09:43:19 Asaf
0
1
Learning C++
1 year ago 2023-09-27 13:49:52 Steven