CB100

Notas, resueltos y tps de la materia Algoritmos y Estructuras de Datos
Index Commits Files Refs README
parciales/2022-2C-1p/2.cpp (4085B)
   1 #include <iostream>
   2 #include <string>
   3 
   4 template <typename Type>
   5 class Node {
   6 private:
   7     Node<Type> *next;
   8     Type data;
   9 
  10 public:
  11     Node() {
  12         this->next = NULL;
  13     }
  14 
  15     Node(Type data) {
  16         this->next = NULL;
  17         this->data = data;
  18     }
  19 
  20     Node<Type> *getNext() {
  21         return this->next;
  22     }
  23 
  24     virtual ~Node() {}
  25 
  26     Type getData() {
  27         return this->data;
  28     }
  29 };
  30 
  31 template <typename Type>
  32 class List {
  33 private:
  34     Node<Type> *first;
  35     Node<Type> *last;
  36     Node<Type> *cursor;
  37     unsigned int len;
  38 
  39 public:
  40     List() {
  41         this->first = new Type;
  42         this->last = this->first;
  43     }
  44 
  45     virtual ~List() {
  46         Node<Type> *curr = this->first;
  47         Node<Type> *prev = curr;
  48         while(curr->getNext() != NULL) {
  49             prev = curr;
  50             curr = curr->getNext();
  51             delete prev;
  52         }
  53     }
  54 
  55     void Add(Type data) {
  56         this->last = new Node<Type>(data);
  57     }
  58 
  59     void startCursor() {
  60         this->cursor = this->first;
  61     }
  62 
  63     bool forwardCursor() {
  64         return (((this->cursor = this->cursor->getNext()) == NULL) ? true : false);
  65     }
  66 
  67     Node<Type> *getCursor() {
  68         return this->cursor;
  69     }
  70 
  71     unsigned int size() {
  72         return this->len;
  73     }
  74 };
  75 
  76 class Comment {
  77     std::string comment;
  78     int score;
  79 
  80 public:
  81     /*
  82      * post: inicializa el Comentario con el contenido y calificación 0.
  83      */
  84     Comment(std::string contenido) {
  85         this->comment = contenido;
  86     }
  87 
  88     std::string getContent() {
  89         return this->comment;
  90     }
  91 
  92     /*
  93      * post: devuelve la calificación [1 a 10] asociada,
  94      *       o 0 si el Comentario no tiene calificación
  95      */
  96     int getScore() {
  97         return this->score;
  98     }
  99 
 100     /*
 101      * pre: calificacion está comprendido entre 1 y 10
 102      * post: cambia la calificación del Comentario
 103      */
 104     void setScore(int calificacion) {
 105         this->score = calificacion;
 106     }
 107 };
 108 
 109 class Image {
 110 private:
 111     std::string url;
 112 public:
 113     /*
 114      * post: inicializa la Imagen alojada en la URL indicada
 115      */
 116     Image(std::string url) {
 117         this->url = url;
 118     }
 119 
 120     virtual ~Image() {}
 121 
 122     /*
 123      * post: devuelve la URL en la que está alojada
 124      */
 125     std::string getUrl();
 126 
 127     /*
 128      * post: devuelve los comentarios asociados
 129      */
 130     List<Comment*> *getComments();
 131 };
 132 
 133 class Editor {
 134 public:
 135 
 136     /*
 137      * post: devuelve de `imgComments` aquel comentario que tenga por lo menos 
 138      *       `scoreCount` calificaciones y ademas tenga el promedio de
 139      *       calificaciones maximo. Debe recorrer toda la lista para hallar el
 140      *       el comentario con mayor promedio de calificacion
 141      */
 142     Comment *getCommentWithHighestScore(List<Comment *> *imgComments) {
 143         Comment *returnComment;
 144         Comment *currentComment;
 145         int currentScore, maxScore;
 146 
 147         returnComment = currentComment = NULL;
 148         currentScore = maxScore = 0;
 149         imgComments->startCursor();
 150 
 151         while(imgComments->forwardCursor()) {
 152             currentComment = imgComments->getCursor()->getData();
 153             currentScore = returnComment->getScore();
 154             if(currentScore > 0) {
 155                 if(currentScore > maxScore) {
 156                     maxScore = currentScore;
 157                     returnComment = currentComment;
 158                 }
 159             }
 160         }
 161         return returnComment;
 162     }
 163 
 164     /*
 165      * post: selecciona de `availableImages` aquella que tenga por lo
 166      *       menos tantos Comentarios como los indicados en `commentsCount` y
 167      *       el promedio de calificaciones sea máximo. Ignora los
 168      *       Comentarios sin calificación.
 169      */
 170     Image *seleccionarImagen(List<Image*> *availableImages, int commentsCount) {
 171         if(commentsCount < 0) {
 172             throw "la cantidad de comentarios no puede ser negativo";
 173         } else if(availableImages == NULL) {
 174             throw "puntero `NULL`";
 175         }
 176 
 177         Image *returnImg;
 178         Image *currentImg;
 179         List<Comment*> *currentImgComments;
 180 
 181         returnImg = currentImg = NULL;
 182 
 183         // se iteran las imagenes
 184         availableImages->startCursor();
 185         while(availableImages->forwardCursor()) {
 186             currentImg = availableImages->getCursor()->getData();
 187             currentImgComments = currentImg->getComments();
 188             if(currentImgComments->size() > (unsigned int)commentsCount) {
 189                 // iteran los comentarios de la imagen y se obtiene el 
 190                 // comentario con mas calificaciones
 191                 if(this->getCommentWithHighestScore(currentImgComments) != NULL) {
 192                     return returnImg = currentImg;
 193                 }
 194             }
 195         }
 196     }
 197 };
 198 
 199 int main () {
 200     Editor *e = new Editor;
 201     delete e;
 202     return 0;
 203 }