Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string>
- #include<iostream>
- #include<sstream>
- #include<list>
- #include<cmath>
- #include<stdlib.h>
- //using namespace std;
- class Point{
- private:
- float x, y;
- public:
- Point(){}
- Point(float x, float y){
- this->x = x;
- this->y = y;
- }
- void print(){
- cout<<"x = "<<x<<" si y = "<<y<<"\n";
- }
- float getX(){
- return x;
- }
- float getY(){
- return y;
- }
- void setX(int x){
- this->x = x;
- }
- void setY(int y){
- this->y = y;
- }
- };
- class Figure{
- protected:
- list<Point*> points;
- public:
- float getPerimeter(){
- double perimeter = 0;
- Point* start = this->points.front();
- list<Point*>::iterator it;
- for (it = this->points.begin(); it != prev(this->points.end()); it++){
- Point* p1 = *it;
- Point* p2 = *(++it);
- perimeter += sqrt(pow((p2->getX() - p1->getX()),2) + pow((p2->getY() - p1->getY()),2));
- it--;
- }
- perimeter += sqrt(pow(((*it)->getX() - start->getX()),2) + pow(((*it)->getY() - start->getY()),2));
- return perimeter;
- }
- void printPoints(){
- cout<<"Coordonatele sunt:\n";
- for (list<Point*>::iterator it = this->points.begin(); it != this->points.end(); it++)
- (*it)->print();
- }
- template<class T>
- void printPanta(T& figure){
- list<Point*>::iterator it;
- it = figure.points.begin();
- Point* p1 = *it;
- Point* p2 = *(++it);
- double slope = (p2->getY() - p1->getY()) / (p2->getX() - p1->getX());
- if(isinf(slope)){
- cout<<"Dreapta e verticala\n";
- }
- else if(slope == 0){
- cout<<"Dreapta e orizontala\n";
- }
- else{
- cout<<"Panta dreptei formata de primele 2 este: "<<slope<<"\n";
- }
- }
- template<class T, class C>
- bool hasPointsOnCircle(T& figure, C& circle){
- for (list<Point*>::iterator it = figure.points.begin(); it != figure.points.end(); it++){
- float x = (*it)->getX();
- float y = (*it)->getY();
- float ox = circle.getOrigine().getX();
- float oy = circle.getOrigine().getY();
- if( (pow(x - ox, 2) + pow(y - oy, 2)) == circle.getRadius())
- return true;
- }
- return false;
- }
- };
- class Triangle : public Figure{
- private:
- public:
- Triangle(){
- this->points.push_back(new Point(0,1));
- this->points.push_back(new Point(-1,-1));
- this->points.push_back(new Point(1,-1));
- }
- void afiseaza(){
- cout<<"Ati creat un triunghi\n";
- }
- };
- class Trapezoid : public Figure{
- private:
- public:
- Trapezoid(){
- this->points.push_back(new Point(1,1));
- this->points.push_back(new Point(0,-1));
- this->points.push_back(new Point(-2,-1));
- this->points.push_back(new Point(-1,1));
- }
- void afiseaza(){
- cout<<"Ati creat un trapez\n";
- }
- };
- class Square : public Figure{
- private:
- public:
- Square(){
- this->points.push_back(new Point(0,0));
- this->points.push_back(new Point(0,1));
- this->points.push_back(new Point(1,1));
- this->points.push_back(new Point(1,0));
- }
- void afiseaza(){
- cout<<"Ati creat un patrat\n";
- }
- };
- class Circle{
- private:
- float radius;
- Point center;
- Circle(float radius, Point center){
- this->radius = radius;
- this->center = center;
- }
- public:
- static Circle* instance;
- static Circle *getInstance(){
- if(!instance)
- instance = new Circle(1, Point(0,0));
- else throw 1;
- return instance;
- }
- void afiseaza(){
- cout<<"Cercul cu raza "<<radius<<" si originea in ("<<center.getX()<<","<<center.getY()<<")\n";
- }
- float getRadius(){
- return radius;
- }
- Point getOrigine(){
- return center;
- }
- };
- Circle* Circle::instance = NULL;
- int main(){
- Square* p1;
- Triangle* t1;
- Trapezoid* z1;
- Circle* circle;
- Point* pct1;
- Figure f1;
- cout<<"1. Adauga figura\n";
- cout<<"2. Adauga puncte\n";
- cout<<"3. Modifica puncte\n";
- cout<<"4. Afisare panta figurii\n";
- cout<<"5. Creeaza cerc\n";
- cout<<"6. E figura pe cerc ?\n";
- cout<<"0. Exit\n";
- int key = -1;
- while(key != 0){
- cout<<"Alegeti o optiune\n";
- cin>>key;
- switch(key){
- case 1:
- int c1;
- cout<<"1. Square\n";
- cout<<"2. Triangle\n";
- cout<<"3. Trapezoid\n";
- cin>>c1;
- switch(c1){
- case 1:
- p1 = new Square();
- p1->afiseaza();
- break;
- case 2:
- t1 = new Triangle();
- t1->afiseaza();
- break;
- case 3:
- z1 = new Trapezoid();
- z1->afiseaza();
- break;
- }
- break;
- case 2:
- pct1 = new Point(2,2);
- pct1->print();
- break;
- case 3:
- int x;
- int y;
- cout<<"Dati noile valori pentru punct\nx= ";
- cin>>x;
- cout<<"y= ";
- cin>>y;
- pct1->setX(x);
- pct1->setY(y);
- break;
- case 4:
- int c4;
- cout<<"1. Square\n";
- cout<<"2. Triangle\n";
- cout<<"3. Trapezoid\n";
- cin>>c4;
- switch(c4){
- case 1:
- f1.printPanta(*p1);
- break;
- case 2:
- f1.printPanta(*t1);
- break;
- case 3:
- f1.printPanta(*z1);
- break;
- }
- break;
- case 5:
- try{
- circle = circle->getInstance();
- circle->afiseaza();
- }catch(int x){
- cout<<"Nu puteti instantia 2 cercuri!\n";
- }
- break;
- case 6:
- int c6;
- cout<<"1. Square\n";
- cout<<"2. Triangle\n";
- cout<<"3. Trapezoid\n";
- cin>>c6;
- switch(c6){
- case 1:
- if(f1.hasPointsOnCircle(*p1,*circle)){
- cout<<"E pe cerc\n";
- }
- else{
- cout<<"Nu e pe cerc\n";
- }
- break;
- case 2:
- if(f1.hasPointsOnCircle(*t1,*circle)){
- cout<<"E pe cerc\n";
- }
- else{
- cout<<"Nu e pe cerc\n";
- }
- break;
- case 3:
- if(f1.hasPointsOnCircle(*z1,*circle)){
- cout<<"E pe cerc\n";
- }
- else{
- cout<<"Nu e pe cerc\n";
- }
- break;
- }
- break;
- case 0:
- cout<<p1->getPerimeter();
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement