Compare commits
No commits in common. "b7cb695840c6e8b3e67dba109c65a3dfa484b0dc" and "1da7074bfc981d4ee98f0dd2fba5621c6af7207f" have entirely different histories.
b7cb695840
...
1da7074bfc
101
main.cpp
101
main.cpp
@ -3,8 +3,6 @@
|
||||
#include <algorithm>
|
||||
#include <map>
|
||||
#include <iomanip>
|
||||
#include <vector>
|
||||
#include <cmath>
|
||||
|
||||
using string = std::string;
|
||||
using slist = std::list<string>;
|
||||
@ -21,91 +19,28 @@ void printlm(T m){
|
||||
printm("\n");
|
||||
}
|
||||
|
||||
class Point {
|
||||
public:
|
||||
const double x;
|
||||
const double y;
|
||||
Point(double a, double b) : x(a), y(b) {}
|
||||
};
|
||||
|
||||
double dist(Point a, Point b) {
|
||||
const auto dx = a.x - b.x;
|
||||
const auto dy = a.y - b.y;
|
||||
return sqrt(dx*dx + dy*dy);
|
||||
}
|
||||
|
||||
std::string printp(const Point &p) {
|
||||
return "( " + std::to_string(p.x) + " , " + std::to_string(p.y) + " )";
|
||||
}
|
||||
|
||||
class Shape {
|
||||
public:
|
||||
virtual bool contains(Point p) const = 0;
|
||||
virtual std::string description() const = 0;
|
||||
virtual void scale(double s) = 0;
|
||||
virtual ~Shape() {}
|
||||
};
|
||||
class Circle : public Shape {
|
||||
public:
|
||||
Point center;
|
||||
double radius;
|
||||
Circle(Point c, double r) : center(c), radius(r) {}
|
||||
virtual bool contains(Point p) const override {
|
||||
return dist(center, p) <= radius;
|
||||
}
|
||||
virtual std::string description() const override {
|
||||
return "Circle ; " + printp(center) + " ; " + std::to_string(radius);
|
||||
}
|
||||
virtual void scale(double s) override {
|
||||
radius *= s;
|
||||
}
|
||||
};
|
||||
class Rectangle : public Shape {
|
||||
public:
|
||||
Point topleft;
|
||||
double width, height;
|
||||
Rectangle(Point tl, double w, double h) : topleft(tl), width(w), height(h) {}
|
||||
virtual bool contains(Point p) const override {
|
||||
return topleft.x <= p.x && topleft.x + width >= p.x && topleft.y <= p.y && topleft.y + height >= p.y;
|
||||
}
|
||||
virtual std::string description() const override {
|
||||
return "Rectangle ; " + printp(topleft) + " ; [ " + std::to_string(width) + " , " + std::to_string(height) + " ]";
|
||||
}
|
||||
virtual void scale(double s) override {
|
||||
width *= s;
|
||||
height *= s;
|
||||
}
|
||||
};
|
||||
class CompositeShape : public Shape {
|
||||
public:
|
||||
Shape* shape1;
|
||||
Shape* shape2;
|
||||
CompositeShape(Shape* s1, Shape* s2) : shape1(s1), shape2(s2) {}
|
||||
virtual bool contains(Point p) const override {
|
||||
return shape1->contains(p) || shape2->contains(p);
|
||||
}
|
||||
virtual std::string description() const override {
|
||||
return "Composite ; " + shape1->description() + " ; " + shape2->description();
|
||||
}
|
||||
virtual void scale(double s) override {
|
||||
shape1->scale(s);
|
||||
shape2->scale(s);
|
||||
}
|
||||
};
|
||||
|
||||
int prompti(string,bool*);
|
||||
double promptd(string,bool*);
|
||||
int prompti(string,bool*);
|
||||
string prompts(string,bool*);
|
||||
|
||||
int main() {
|
||||
std::vector<Shape*> s {};
|
||||
printlm("Creating Cirlces...");
|
||||
for (int i = 0; i <= 10; ++i) s.push_back(new Circle(Point(0, i), 10));
|
||||
printlm("Circles with the point (5, 12) within them:");
|
||||
for (int i = 0; i < s.size(); i++) if (s[i]->contains(Point(5, 12))) printlm(i);
|
||||
printlm("Destroying Circles...");
|
||||
while (s.size() > 0) {delete s.back(); s.pop_back();}
|
||||
Circle c {Poi}
|
||||
printlm("Parse comma deliminated list:");
|
||||
bool s {};
|
||||
slist lst {};
|
||||
auto v = prompts("Enter the string: ", &s);
|
||||
if (!s) {
|
||||
printlm("Error, No Data!");
|
||||
return -1;
|
||||
}
|
||||
auto comma = find(v.cbegin(),v.cend(),',');
|
||||
lst.push_back(string(v.cbegin(),comma));
|
||||
do {
|
||||
auto start = ++comma;
|
||||
comma = find(start,v.cend(),',');
|
||||
lst.push_back(string(start, comma));
|
||||
} while(comma != v.cend());
|
||||
printlm("All entries:");
|
||||
for (const auto c : lst) printlm(c);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user