1
0
LearningCPP/main.cpp

150 lines
3.4 KiB
C++
Raw Normal View History

2022-10-14 09:27:47 +01:00
#include <iostream>
2022-10-28 10:11:47 +01:00
#include <list>
2022-11-04 11:58:21 +00:00
#include <algorithm>
#include <map>
2022-10-19 09:33:42 +01:00
#include <iomanip>
2022-12-09 09:59:07 +00:00
#include <vector>
2023-08-20 22:13:18 +01:00
#include <cmath>
2022-10-14 09:27:47 +01:00
using string = std::string;
using slist = std::list<string>;
2022-10-14 09:27:47 +01:00
2022-10-28 09:43:51 +01:00
//Place in header!
2022-10-19 09:33:42 +01:00
template <typename T>
void printm(T m){
2022-10-14 09:27:47 +01:00
std::cout << m;
}
2022-10-19 09:33:42 +01:00
template <typename T>
void printlm(T m){
2022-10-14 09:27:47 +01:00
printm(m);
printm("\n");
}
2023-08-20 22:13:18 +01:00
class Point {
2022-12-09 09:59:07 +00:00
public:
2023-08-20 22:13:18 +01:00
const double x;
const double y;
Point(double a, double b) : x(a), y(b) {}
2022-12-09 09:59:07 +00:00
};
2023-08-20 22:13:18 +01:00
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 {
2022-12-09 09:59:07 +00:00
public:
2023-08-20 22:13:18 +01:00
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);
}
2022-12-09 09:59:07 +00:00
};
2022-10-28 09:43:51 +01:00
int prompti(string,bool*);
2022-12-09 09:59:07 +00:00
double promptd(string,bool*);
string prompts(string,bool*);
2022-10-19 09:33:42 +01:00
int main() {
2023-08-20 22:13:18 +01:00
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}
2022-10-19 09:33:42 +01:00
return 0;
2022-10-14 09:27:47 +01:00
}
2022-10-28 09:43:51 +01:00
int prompti(string message, bool *status) {
int tr {};
2022-10-19 09:33:42 +01:00
*status = false;
2022-10-14 09:27:47 +01:00
std::cout << message;
2022-10-19 09:33:42 +01:00
if (!(std::cin >> tr)){
2022-10-14 09:27:47 +01:00
std::cin.clear();
std::cin.ignore();
2022-10-19 09:33:42 +01:00
return 0;
2022-10-14 09:27:47 +01:00
}
2022-10-19 09:33:42 +01:00
*status = true;
2022-10-14 09:27:47 +01:00
return tr;
}
2022-10-19 09:33:42 +01:00
2022-10-28 10:11:47 +01:00
double promptd(string message, bool *status) {
double tr {};
*status = false;
std::cout << message;
if (!(std::cin >> tr)){
std::cin.clear();
std::cin.ignore();
return 0.0;
}
*status = true;
return tr;
}
string prompts(string message, bool *status) {
string tr {""};
*status = false;
std::cout << message;
if (!(std::cin >> tr)){
std::cin.clear();
std::cin.ignore();
return "";
}
*status = true;
return tr;
2022-10-28 10:11:47 +01:00
}