• Jueves 2 de Mayo de 2024, 21:32

Autor Tema:  Problema con implementación de Node  (Leído 818 veces)

Adalte

  • Nuevo Miembro
  • *
  • Mensajes: 1
    • Ver Perfil
Problema con implementación de Node
« en: Lunes 18 de Mayo de 2009, 20:27 »
0
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++
  1. //--- Vertex.h
  2.  
  3. #ifndef VERTEX_H
  4. #define VERTEX_H
  5. class Vertex {
  6.  private:
  7.    int x,y;
  8.  public:
  9.    Vertex():x(0),y(0){};
  10.    Vertex(int px,int py):x(px),y(py){};
  11.    ~Vertex();
  12.    int Xvrd(){return x;};
  13.    int Yvrd(){return y;};
  14.    friend class Polygon;
  15. };
  16. #endif
  17.  
  18. //--- Shape.h
  19.  
  20. #ifndef SHAPE_H
  21. #define SHAPE_H
  22. #include <iostream>
  23. #include "Vertex.h"
  24. using namespace std;
  25. class Shape {
  26.  protected:
  27.    Vertex PosSh;
  28.    int AntPlg, AntRec, AntCir, AntPoi;
  29.  public:
  30.    Shape(const Shape &s) : AntPlg(0), AntRec(0), AntCir(0), AntPoi(0) {};
  31.    virtual ~Shape();
  32.    virtual double area() = 0;
  33.    virtual void printShape();
  34. };
  35. #endif
  36.  
  37. //--- Polygon.h
  38.  
  39. #ifndef POLYGON_H
  40. #define POLYGON_H
  41. #include <iostream>
  42. #include "Vertex.h"
  43. #include "Shape.h"
  44. using namespace std;
  45. class Polygon : public Shape{
  46.  private:
  47.    Vertex *Pol;
  48.    int pX,pY,size;
  49.  public:
  50.    Polygon();
  51.    Polygon(int x, int y, Vertex *varr, int num);
  52.    ~Polygon();
  53.    void add(Shape &s);
  54.    double area();
  55.    void printPol();
  56. };
  57. #endif
  58.  
  59. //--- Polygon.cpp
  60.  
  61. #include <iostream>
  62. #include <cmath>
  63. using namespace std;
  64. #include "polygon.h"
  65. #include "Vertex.h"
  66.  
  67. class Shape;
  68.  
  69. Polygon::Polygon(int x, int y, Vertex *varr, int num) : pX(x), pY(y), size(num) {
  70.   if (size > 0) {
  71.      Shape::PosSh = Vertex(pX, pY);
  72.      Pol = new Vertex[size];
  73.      for(int ix=0; ix<size; ix++)
  74.         Pol[ix] = varr[ix];
  75.    } else {
  76.      Pol = 0; }
  77. }
  78. Polygon::~Polygon(){}
  79.  
  80. double Polygon::area(){
  81.   double s = 0.0;
  82.   double r = 0.0;
  83.   for (int ix=1; ix<size; ix++)
  84.        s += (Pol[ix-1].x * Pol[ix].y) - (Pol[ix].x * Pol[ix-1].y);
  85.   r = s / 2;
  86.   return abs(r);
  87. }
  88. void Polygon::printPol(){
  89.   for(int ix=0; ix<size; ix++)
  90.      cout << "(" << Pol[ix].x << "," << Pol[ix].y << ")";
  91. }
  92.  
  93. //--- Circle.h
  94.  
  95. #ifndef CIRCLE_H
  96. #define CIRCLE_H
  97. #include <iostream>
  98. #include "Vertex.h"
  99. #include "Shape.h"
  100. using namespace std;
  101.  
  102. class Circle : public Shape {
  103.  private:
  104.    Vertex *CircPos;
  105.    int pX, pY;
  106.    double radius;
  107.  public:
  108.    Circle();
  109.    Circle(int x, int y, int radie);
  110.    ~Circle();
  111.    void add(Shape &s);
  112.    virtual double area();
  113.    virtual void printCir();
  114. };
  115. #endif
  116.  
  117. //--- Circle.cpp
  118.  
  119. #include <iostream>
  120. #include "Circle.h"
  121. #include "Vertex.h"
  122. using namespace std;
  123.  
  124. class Shape;
  125.  
  126. Circle::Circle(int x, int y, int radie) : pX(x), pY(y), radius(radie) {
  127.   Shape::AntCir++;
  128.   if (radius>0){
  129.      CircPos = new Vertex[Shape::AntCir];
  130.      CircPos[Shape::AntCir] = Vertex(pX, pY);
  131.     } else {
  132.      CircPos = 0;
  133.    }
  134. }
  135. Circle::~Circle() {}
  136. double Circle::area() { return M_PI * radius * radius; }
  137. void Circle::printCir() {}
  138.  
  139. //--- ShapeList.h
  140.  
  141. #ifndef SHAPELIST_H
  142. #define SHAPELIST_H
  143. #include "vertex.h"
  144. #include "Shape.h"
  145. using namespace std;
  146.  
  147. class ShapeNode;
  148.  
  149. class [b]ShapeList[/b] {
  150.  protected:
  151.    ShapeNode *first;
  152.  public:
  153.    ShapeList();
  154.    ShapeList(const ShapeList &shapes);
  155.    ~ShapeList();
  156.    void add(const Shape &s); // una COPIA de la figura se guarda en la lista
  157.    void remove(const Vertex &v);
  158.    double area(); // retorna la suma TOTAL del area de TODAS las figuras
  159.    void print(); // escribe TODA la lista
  160. };
  161. #endif
  162.  
  163. //--- ShapeList.cpp
  164.  
  165. #include <iostream>
  166. #include "ShapeList.h"
  167. #include "vertex.h"
  168. using namespace std;
  169.  
  170. class Shape;
  171.  
  172. class ShapeNode {
  173. protected:
  174.   Shape *s;
  175.   ShapeNode *prev, *next;
  176.   ShapeNode(Shape *_s, _ShapeNode *_prev, ShapeNode *_next);
  177. };
  178. ShapeList::ShapeList() { first = 0;}
  179. ShapeList::ShapeList(const ShapeList &shapes) {}
  180. ShapeList::~ShapeList() {}
  181. void ShapeList::add(const Shape &s){
  182.   ShapeNode *tmp = first;
  183.   first = new ShapeNode(&s, tmp, 0);
  184.   tmp->prev = first;
  185. }
  186. void ShapeList::remove(const Vertex &v) {}
  187. double ShapeList::area() {}
  188. void ShapeList::print() {}
  189.  
  190. // El MAIN para probar es:
  191.  
  192. //--- Test.cpp
  193.  
  194. #include <iostream>
  195. #include "vertex.h"
  196. #include "Polygon.h"
  197. #include "Circle.h"
  198. #include "ShapeList.h"
  199. using namespace std;
  200.  
  201. int main()
  202. {
  203.   ShapeList list;
  204.   Vertex varr[] = { Vertex(0, 0), Vertex(10, 0), Vertex(5, 2), Vertex(5, 5) };
  205.   list.add(Polygon(1, 4, varr, 4));
  206.   list.add(Rectangle(4, 10, 2, 4));
  207.   list.add(Circle(5, 5, 3));
  208.   list.add(Point(6, 7, 1));
  209.  
  210.   Vertex nyarr[] = { Vertex(1, 1), Vertex(9, 2) };
  211.   list.add(Polygon(6, 8, nyarr, 2));
  212.   list.add(Rectangle(10, 4, 4, 2));
  213.   list.add(Circle(3, 3, 5));
  214.   list.add(Point(9, 6, 1));
  215.  
  216.   list.print();
  217.   cout <<"Total Area: " << list.area() << endl;
  218.   ShapeList list2(list);
  219.   list2.print();
  220.   cout <<"Total Area: " << list2.area() << endl;
  221.   list.remove(Vertex(5, 5));
  222.   list.print();
  223.   cout <<"Total Area: " << list.area() << endl;
  224.   list2.print();
  225.   cout <<"Total Area: " << list2.area() << endl;
  226.   return 0;
  227. }
  228.  
  229.  
  230.  

Espero me puedan ayudar, a lo cual estaré muy agradecido.

Adalte.
.
« última modificación: Miércoles 20 de Mayo de 2009, 04:55 por Adalte »

m0skit0

  • Miembro de PLATA
  • *****
  • Mensajes: 2337
  • Nacionalidad: ma
    • Ver Perfil
    • http://fr33kk0mpu73r.blogspot.com/
Re: Problema con implementación de Node
« Respuesta #1 en: Martes 19 de Mayo de 2009, 10:49 »
0
WTF!

Usa las etiquetas de código  &lt;_&lt;