commit f549adbb0ff5a03a847b8e948d246028822a5cd5
parent 5b066ce41a6f633954a906160e7a5b1afa80a1bd
Author: Martin Kloeckner <mjkloeckner@gmail.com>
Date: Tue, 21 May 2024 13:47:10 -0300
add solutions for `parciales/2p2C2022`
Diffstat:
3 files changed, 243 insertions(+), 0 deletions(-)
diff --git a/parciales/2p2C2022/1.cpp b/parciales/2p2C2022/1.cpp
@@ -0,0 +1,62 @@
+#include <iostream>
+
+#define ULTIMO_DIGITO_PADRON 8
+
+int main(){
+ int *A, *C, *F;
+ int **B;
+ int H = 20 + ULTIMO_DIGITO_PADRON;
+
+ A = new int[3];
+
+ for (int i = 0; i < 3; i++) {
+ A[i] = H + i;
+ }
+
+ C = new int;
+ (*C) = A[1]; // C = 20 + ULTIMO_DIGITO_PADRON + 1
+ F = A + 2; // es equivalente a F = &A[2] (aritmética de punteros)
+
+ std::cout << (*F) << (*C) << A[0] << std::endl;
+ // (*F) = A[2] == 20 + ULTIMO_DIGITO_PADRON + 2 == 30
+ // (*C) = A[1] == 20 + ULTIMO_DIGITO_PADRON + 1 == 29
+ // A[0] = 20 + ULTIMO_DIGITO_PADRON = 28
+ // la linea anterior imprime `302928\n`
+
+ B = new int*[3]; // arreglo de punteros a int
+ B[0] = C; // B[0] = C = A + 1
+ B[1] = F; // B[1] = F = A + 2
+ B[2] = &H; // B[2] = *(B + 2) = &H = 20 + ULTIMO_DIGITO_PADRON
+
+ std::cout << *B[1] << **B << *B[2] << std::endl;
+ // *B[1] = *(*(B + 1)) = *F = A[2] = 20 + ULTIMO_DIGITO_PADRON + 2
+ // **B = *(B[0]) = *C = A[1] = 20 + ULTIMO_DIGITO_PADRON + 1
+ // *B[2] = H = 20 + ULTIMO_DIGITO_PADRON
+ // la linea anterior imprime `302928\n`
+
+ (*B[0]) = (*F) + 3; // *B[0] = *F + 3 == A[2] + 3
+ H++;
+ A[2] = (*C) + 1; // A[2] = *C + 1 = A[1] + 1 == 29 + 1
+
+ std::cout << *C << *B[1] << *B[2] << std::endl;
+ // *C = *B[0] = (*F) + 3 = A[2] + 3 == 20 + ULTIMO_DIGITO_PADRON + 2 + 3
+ // *B[1] = *F = A[2] = (*C) + 1 = *(B[0]) + 1 == 33 + 1
+ // *B[2] = H = 20 + ULTIMO_DIGITO_PADRON + 1 (++);
+ // la linea anterior imprime `333429\n`
+
+ F = C;
+ C = A + 2;
+ (**B) = A[0];
+ A[2] = ** (B + 2);
+
+ std::cout << H << (*C) << (*F) << std::endl;
+ // H == 20 + ULTIMO_DIGITO_PADRON + 1 (++)
+ // *C = A[2] = *(*(B+2)) = *B[2] = H = 20 + ULTIMO_DIGITO_PADRON + 1 (++)
+ // *F = *C = 20 + ULTIMO_DIGITO_PADRON
+ // la linea anterior imprime `292928\n`
+
+ delete[] B;
+ delete F;
+ delete[] A;
+ return 0;
+}
diff --git a/parciales/2p2C2022/2.cpp b/parciales/2p2C2022/2.cpp
@@ -0,0 +1,93 @@
+#include <iostream>
+#include <string>
+
+class Comentario {
+ std::string comment;
+ int score;
+
+public:
+ // post: inicializa el Comentario con el contenido y calificación 0.
+ Comentario(std::string contenido) {
+ this->comment = contenido;
+ }
+
+ std::string obtenerContenido() {
+ return comment;
+ }
+
+ // post: devuelve la calificación [1 a 10] asociada,
+ // o 0 si el Comentario no tiene calificación
+ int obtenerCalificacion() {
+ return this->score;
+ }
+
+ // pre: calificacion está comprendido entre 1 y 10
+ // post: cambia la calificación del Comentario
+ void calificar(int calificacion) {
+ this->score = calificacion;
+ }
+};
+
+template <typename Type>
+class Lista {
+private:
+ Type *next;
+ Type *current;
+
+public:
+ Lista() {
+ this->current = new Type;
+ this->next = NULL;
+ }
+
+ virtual ~Lista() {
+ delete this->next;
+ this->next = NULL;
+ }
+
+ void Add(Type node) {
+ this->next = new Type;
+ }
+};
+
+class Imagen {
+private:
+ std::string url;
+public:
+ // post: inicializa la Imagen alojada en la URL indicada
+ Imagen(std::string url) {
+ this->url = url;
+ }
+
+ // post: devuelve la URL en la que está alojada
+ std::string obtenerUrl();
+
+ // post: devuelve los comentarios asociados
+ Lista<Comentario*> *obtenerComentarios();
+
+ virtual ~Imagen() {}
+};
+
+
+class Editor {
+public:
+ // post: selecciona de ‘imagenesDisponibles’ aquella que tenga por lo
+ // menos tantos Comentarios como los indicados y
+ // el promedio de calificaciones sea máximo. Ignora los
+ // Comentarios sin calificación.
+ Imagen *seleccionarImagen(Lista<Imagen*> *availableImgs, int commentsLen) {
+ if(commentsLen < 0) {
+ throw "`commentsLen` no puede ser negativo";
+ }
+
+ while(availableImgs->obtenerComentarios() != NULL) {
+ if(availableImgs->obtenerComentarios())
+ return NULL;
+ }
+ }
+};
+
+int main () {
+ std::cout << "Hello, World!\n";
+ return 0;
+}
diff --git a/parciales/2p2C2022/4.cpp b/parciales/2p2C2022/4.cpp
@@ -0,0 +1,88 @@
+#include <iostream>
+#include <string>
+
+class Colecta {
+private:
+ double total, maxDonacion, objetivo;
+ unsigned int cantDonaciones;
+
+public:
+ Colecta() {
+ this->objetivo = this->total = this->maxDonacion = this->cantDonaciones = 0;
+ }
+
+ Colecta(double objetivo, double total, double maxDonacion, size_t cantDonaciones) {
+ this->objetivo = objetivo;
+ this->total = total;
+ this->maxDonacion = maxDonacion;
+ this->cantDonaciones = cantDonaciones;
+ }
+
+ virtual ~Colecta() {}
+
+ void setObjetivo(long objetivo) {
+ this->objetivo = objetivo;
+ }
+
+ double getObjetivo() {
+ return this->objetivo;
+ }
+
+ double calcularRecaudacion() {
+ return this->total;
+ }
+
+ void donar(double amount) {
+ if(amount < 0) {
+ throw "El monto a donar no puede ser negativo";
+ } else if(amount > maxDonacion) {
+ maxDonacion = amount;
+ }
+
+ this->total += amount;
+ this->cantDonaciones++;
+ }
+
+ unsigned int contarDonaciones() {
+ return this->cantDonaciones;
+ }
+
+ double calcularDonacionMaxima() {
+ return this->maxDonacion;
+ }
+
+ double calcularRecaudacionFaltante() {
+ double faltante = (this->objetivo - this->calcularDonacionMaxima());
+ return (faltante < 0) ? 0 : faltante;
+ }
+};
+
+void printStatus(Colecta colecta) {
+ std::cout << "Objetivo: `" << colecta.getObjetivo() << "`\n";
+ std::cout << "Cant. de donaciones: `"
+ << colecta.contarDonaciones() << "`\n";
+ std::cout << "Maxima donacion: `"
+ << colecta.calcularDonacionMaxima() << "`\n";
+ std::cout << "Faltante: `"
+ << colecta.calcularRecaudacionFaltante() << "`\n";
+}
+
+int main() {
+ Colecta c;
+ c.setObjetivo(100000.0);
+ printStatus(c);
+ std::cout << "\n";
+
+ c.donar(91.52);
+ printStatus(c);
+ std::cout << "\n";
+
+ c.donar(1020.00);
+ printStatus(c);
+ std::cout << "\n";
+
+ c.donar(20.00);
+ printStatus(c);
+ std::cout << "\n";
+ return 0;
+}