Programación General > C/C++
Problema con implementación de Node
(1/1)
Adalte:
Hola !
Por favor, necesito solucionar este problema, es MUY importante para mi. Muchas gracias de antemano por la ayuda que me puedan dar !
Tengo que crear una clase abstracta (llamémosla SHAPE) con las subclases Polygon y Circle, pero los constructores tienen que estar definidos as:
- Polygon(int x, int y, Vertex *varr, int num);
- Circle(int x, int y, int radie);
- area()
- print()
Las variables x,y son las coordenas de posición de las figuras y hay que meterlas en Vertex (definido en el código adjunto) y los puntos que definen el Polygon también hay que meterlos en Vertex.
Además tengo que crear una "doubly linked" lista (llamémosla SHAPELIST) con por lo menos, los siguientes métodos y funciones:
- ShapeList();
- ShapeList(const ShapeList &shapes);
- ~ShapeList();
- void add(const Shape &s); // una COPIA de la figura se guarda en la lista
-void remove(const Vertex &v);
- double area(); // retorna la suma TOTAL del area de TODAS las figuras
- void print(); // escribe TODA la lista
--- Código: C++ ---//--- Vertex.h #ifndef VERTEX_H#define VERTEX_Hclass Vertex { private: int x,y; public: Vertex():x(0),y(0){}; Vertex(int px,int py):x(px),y(py){}; ~Vertex(); int Xvrd(){return x;}; int Yvrd(){return y;}; friend class Polygon;};#endif //--- Shape.h #ifndef SHAPE_H#define SHAPE_H#include <iostream>#include "Vertex.h"using namespace std;class Shape { protected: Vertex PosSh; int AntPlg, AntRec, AntCir, AntPoi; public: Shape(const Shape &s) : AntPlg(0), AntRec(0), AntCir(0), AntPoi(0) {}; virtual ~Shape(); virtual double area() = 0; virtual void printShape();};#endif //--- Polygon.h #ifndef POLYGON_H#define POLYGON_H#include <iostream>#include "Vertex.h"#include "Shape.h"using namespace std;class Polygon : public Shape{ private: Vertex *Pol; int pX,pY,size; public: Polygon(); Polygon(int x, int y, Vertex *varr, int num); ~Polygon(); void add(Shape &s); double area(); void printPol();};#endif //--- Polygon.cpp #include <iostream>#include <cmath>using namespace std;#include "polygon.h"#include "Vertex.h" class Shape; Polygon::Polygon(int x, int y, Vertex *varr, int num) : pX(x), pY(y), size(num) { if (size > 0) { Shape::PosSh = Vertex(pX, pY); Pol = new Vertex[size]; for(int ix=0; ix<size; ix++) Pol[ix] = varr[ix]; } else { Pol = 0; }}Polygon::~Polygon(){} double Polygon::area(){ double s = 0.0; double r = 0.0; for (int ix=1; ix<size; ix++) s += (Pol[ix-1].x * Pol[ix].y) - (Pol[ix].x * Pol[ix-1].y); r = s / 2; return abs(r);}void Polygon::printPol(){ for(int ix=0; ix<size; ix++) cout << "(" << Pol[ix].x << "," << Pol[ix].y << ")";} //--- Circle.h #ifndef CIRCLE_H#define CIRCLE_H#include <iostream>#include "Vertex.h"#include "Shape.h"using namespace std; class Circle : public Shape { private: Vertex *CircPos; int pX, pY; double radius; public: Circle(); Circle(int x, int y, int radie); ~Circle(); void add(Shape &s); virtual double area(); virtual void printCir();};#endif //--- Circle.cpp #include <iostream>#include "Circle.h"#include "Vertex.h"using namespace std; class Shape; Circle::Circle(int x, int y, int radie) : pX(x), pY(y), radius(radie) { Shape::AntCir++; if (radius>0){ CircPos = new Vertex[Shape::AntCir]; CircPos[Shape::AntCir] = Vertex(pX, pY); } else { CircPos = 0; }}Circle::~Circle() {}double Circle::area() { return M_PI * radius * radius; }void Circle::printCir() {} //--- ShapeList.h #ifndef SHAPELIST_H#define SHAPELIST_H#include "vertex.h"#include "Shape.h"using namespace std; class ShapeNode; class [b]ShapeList[/b] { protected: ShapeNode *first; public: ShapeList(); ShapeList(const ShapeList &shapes); ~ShapeList(); void add(const Shape &s); // una COPIA de la figura se guarda en la lista void remove(const Vertex &v); double area(); // retorna la suma TOTAL del area de TODAS las figuras void print(); // escribe TODA la lista};#endif //--- ShapeList.cpp #include <iostream>#include "ShapeList.h"#include "vertex.h"using namespace std; class Shape; class ShapeNode {protected: Shape *s; ShapeNode *prev, *next; ShapeNode(Shape *_s, _ShapeNode *_prev, ShapeNode *_next);};ShapeList::ShapeList() { first = 0;}ShapeList::ShapeList(const ShapeList &shapes) {}ShapeList::~ShapeList() {}void ShapeList::add(const Shape &s){ ShapeNode *tmp = first; first = new ShapeNode(&s, tmp, 0); tmp->prev = first;}void ShapeList::remove(const Vertex &v) {}double ShapeList::area() {}void ShapeList::print() {} // El MAIN para probar es: //--- Test.cpp #include <iostream>#include "vertex.h"#include "Polygon.h"#include "Circle.h"#include "ShapeList.h"using namespace std; int main(){ ShapeList list; Vertex varr[] = { Vertex(0, 0), Vertex(10, 0), Vertex(5, 2), Vertex(5, 5) }; list.add(Polygon(1, 4, varr, 4)); list.add(Rectangle(4, 10, 2, 4)); list.add(Circle(5, 5, 3)); list.add(Point(6, 7, 1)); Vertex nyarr[] = { Vertex(1, 1), Vertex(9, 2) }; list.add(Polygon(6, 8, nyarr, 2)); list.add(Rectangle(10, 4, 4, 2)); list.add(Circle(3, 3, 5)); list.add(Point(9, 6, 1)); list.print(); cout <<"Total Area: " << list.area() << endl; ShapeList list2(list); list2.print(); cout <<"Total Area: " << list2.area() << endl; list.remove(Vertex(5, 5)); list.print(); cout <<"Total Area: " << list.area() << endl; list2.print(); cout <<"Total Area: " << list2.area() << endl; return 0;}
Espero me puedan ayudar, a lo cual estaré muy agradecido.
Adalte.
.
m0skit0:
WTF!
Usa las etiquetas de código <_<
Navegación
Ir a la versión completa