1
0

Compare commits

...

2 Commits

Author SHA1 Message Date
b7cb695840
Shape testing. 2023-08-20 22:13:18 +01:00
b736074066 Infinate Heap. 2022-12-09 09:59:07 +00:00
3 changed files with 83 additions and 18 deletions

101
main.cpp
View File

@ -3,6 +3,8 @@
#include <algorithm> #include <algorithm>
#include <map> #include <map>
#include <iomanip> #include <iomanip>
#include <vector>
#include <cmath>
using string = std::string; using string = std::string;
using slist = std::list<string>; using slist = std::list<string>;
@ -19,28 +21,91 @@ void printlm(T m){
printm("\n"); 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*); int prompti(string,bool*);
int prompti(string,bool*); double promptd(string,bool*);
string prompts(string,bool*); string prompts(string,bool*);
int main() { int main() {
printlm("Parse comma deliminated list:"); std::vector<Shape*> s {};
bool s {}; printlm("Creating Cirlces...");
slist lst {}; for (int i = 0; i <= 10; ++i) s.push_back(new Circle(Point(0, i), 10));
auto v = prompts("Enter the string: ", &s); printlm("Circles with the point (5, 12) within them:");
if (!s) { for (int i = 0; i < s.size(); i++) if (s[i]->contains(Point(5, 12))) printlm(i);
printlm("Error, No Data!"); printlm("Destroying Circles...");
return -1; while (s.size() > 0) {delete s.back(); s.pop_back();}
} Circle c {Poi}
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; return 0;
} }

BIN
main.o

Binary file not shown.

BIN
t Executable file → Normal file

Binary file not shown.