diff --git a/parte1/bin/programa b/parte1/bin/programa new file mode 100755 index 0000000..2ccd7f1 Binary files /dev/null and b/parte1/bin/programa differ diff --git a/parte1/include/Cliente.h b/parte1/include/Cliente.h new file mode 100644 index 0000000..6608169 --- /dev/null +++ b/parte1/include/Cliente.h @@ -0,0 +1,31 @@ +#ifndef CLIENTE_H +#define CLIENTE_H + +#include //cin, cout +#include "Fecha.h" + +using namespace std; +//SI FALTA ALGUN METODO O FUNCION A袮DIRLO... +class Cliente { + long int dni; + char *nombre; + Fecha fechaAlta; +public: + Cliente(long int d, const char *nom, Fecha f); + virtual ~Cliente(); + Cliente& operator=(const Cliente& c); + + long int getDni() const { return this->dni; } + const char* getNombre() const { return nombre; } //VIP devolver un puntero constante para evitar que desde el main() se puede modificar el nombre + Fecha getFecha() const { return fechaAlta; } + + void setNombre(const char *nom); + void setFecha(Fecha f); + + bool operator==(const Cliente& c) const; // if (c1 ===c2) + +}; + +ostream& operator<<(ostream &s, const Cliente &c); //funcion no amiga de la clase + +#endif // CLIENTE_H diff --git a/parte1/include/Fecha.h b/parte1/include/Fecha.h new file mode 100644 index 0000000..2f09f6b --- /dev/null +++ b/parte1/include/Fecha.h @@ -0,0 +1,31 @@ +#ifndef FECHA_H +#define FECHA_H + +#include //cin, cout + +using namespace std; +//SI FALTA ALGUN METODO O FUNCION A袮DIRLO... +class Fecha { + int dia; + int mes, anio; +public: + Fecha(const int &dia, const int &m, const int &anio); +//virtual ~Fecha(); //NO HACE FALTA +//Fecha(const Fecha &f); //NO HACE FALTA: EL QUE GENERA EL COMPILADOR FUNCIONA BIEN YA QUE NO HAY PUNTEROS + int getDia() const { return dia; } + int getMes() const { return this->mes; } + int getAnio() const { return this->anio; } + void setFecha(const int &dia, const int &mes, const int &a); + void ver() const; + bool bisiesto() const; + Fecha operator++(); //++f + Fecha operator++(int i); //f++ + Fecha operator+(const int &i) const; //f+5 + + friend Fecha operator+(const int &i, const Fecha &f); //const por seguridad y & por velocidad + friend ostream& operator<<(ostream& os, const Fecha& f); +}; + +Fecha operator+(const int &i, const Fecha &f); //const por seguridad y & por velocidad + +#endif // FECHA_H diff --git a/parte1/src/Cliente.cpp b/parte1/src/Cliente.cpp new file mode 100644 index 0000000..62f17f0 --- /dev/null +++ b/parte1/src/Cliente.cpp @@ -0,0 +1,58 @@ +#include +#include //strlen, strcpy +#include //std::setprecision +#include //stringstream +#include "Cliente.h" + +//Cliente::Cliente(long int d, char *nom, Fecha f):fechaAlta(f.getDia(), f.getMes(), f.getAnio()) { +Cliente::Cliente(long int d,const char *nom, Fecha f):fechaAlta(f) { //esta cabecera es mas corta (invoco + this->dni=d; //constructor copia de fecha + //en vez de constructor de 3 parametros) + //this->nombre=nombre; //MAL!!!! + this->nombre=new char[strlen(nom)+1]; + strcpy(this->nombre, nom); + + //this->fechaAlta=f;//MAL!!!! los tipos no primitivos debe ir en zona inicializadores +} + +Cliente::~Cliente() { + delete [] this->nombre; //si en el constructor uso new [] en el destructor uso delete [] +} + +Cliente& Cliente::operator=(const Cliente& c) { + if (this != &c) { //si no es x=x + this->dni=c.dni; + delete [] this->nombre; + //this->nombre=c.nombre; //MAL!!!! + this->nombre=new char[strlen(c.nombre)+1]; + strcpy(this->nombre, c.nombre); + this->fechaAlta=c.fechaAlta; + } + return *this; +} + +bool Cliente::operator==(const Cliente& c) const { + if (this->dni!=c.dni) return false; + if (strcmp(this->nombre, c.nombre)!=0) return false; + if (this->fechaAlta.getDia()!=c.fechaAlta.getDia() || + this->fechaAlta.getMes()!=c.fechaAlta.getMes() || + this->fechaAlta.getAnio()!=c.fechaAlta.getAnio()) return false; + return true; +} + +//RESTO DE METODOS Y FUNCIONES A RELLENAR POR EL ALUMNO.. + +void Cliente::setNombre(const char *nom){ + delete[] this->nombre; + this->nombre = new char[strlen(nom)+1]; + strcpy(this->nombre, nom); +} + +ostream& operator<<(ostream& os, const Cliente& c) { + os << c.getDni() << " - " << c.getNombre() << " (" << c.getFecha() << ")"; + return os; +} + +void Cliente::setFecha(Fecha f) { + this->fechaAlta = f; +} diff --git a/parte1/src/Fecha.cpp b/parte1/src/Fecha.cpp new file mode 100644 index 0000000..241108e --- /dev/null +++ b/parte1/src/Fecha.cpp @@ -0,0 +1,99 @@ +#include "Fecha.h" + +Fecha::Fecha(const int &dia, const int &m, const int &anio) { + this->setFecha(dia, m, anio); //el cogido es el mismo que el del metodo setFecha +} + +void Fecha::setFecha(const int &dia, const int &mes, const int &a) { + int dmax, diaMes[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; + this->anio=a; //VIP debo asignar a駉 para que al llamar a bisiesto() tenga el a駉 bien + if (this->bisiesto()) + diaMes[2]=29; + + if (mes<1) //si el mes es incorrecto + this->mes=1; + else if (mes>12) //si el mes es incorrecto + this->mes=12; + else + this->mes=mes; + dmax=diaMes[this->mes]; //una vez fijado el mes veo cuantos dias tiene ese mes como maximo + + if (dia>dmax) //si dia es superior al numero de dias de dicho mes + this->dia=dmax; + else if (dia<1) //si dia es inferior a 1 + this->dia=1; + else + this->dia=dia; +} + +bool Fecha::bisiesto() const { + if (this->anio%4==0 && (this->anio%100!=0 || this->anio%400==0 )) //esto no es exacto... corregidlo ustedes + return true; + else + return false; +} + +void Fecha::ver() const { + if (this->dia < 10) + cout << "0"; + cout << this->dia << "/"; + if (this->mes < 10) + cout << "0"; + cout << this->mes << "/" << this->anio; +} + +Fecha Fecha::operator++() { //++f + int dmax, diaMes[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; + if (this->bisiesto()) //si el a駉 es bisiesto febrero tiene 29 dias + diaMes[2]=29; + dmax=diaMes[this->mes]; + this->dia++; + if (this->dia>dmax) { //si al incrementar dia superamos el numero de dias de dicho mes + this->dia=1; //pasamos a 1 + this->mes++; //del mes siguiente + if (this->mes>12) { //si al incrementar mes pasamos de 12 meses + this->mes=1; //pasamos al mes 1 + this->anio++; //del a駉 siguiente + } + } + return *this; //devolvemos el objeto fecha ya incrementado +} + +//RESTO DE METODOS Y FUNCIONES A RELLENAR POR EL ALUMNO... + + +Fecha Fecha::operator++(int) { //f++ + Fecha temp = *this; + + int dmax, diaMes[] = {0,31,28,31,30,31,30,31,31,30,31,30,31}; + if (this->bisiesto()) //si el a駉 es bisiesto febrero tiene 29 dias + diaMes[2]=29; + + dmax=diaMes[this->mes]; + this->dia++; + if (this->dia>dmax) { //si al incrementar dia superamos el numero de dias de dicho mes + this->dia=1; //pasamos a 1 + this->mes++; //del mes siguiente + if (this->mes>12) { //si al incrementar mes pasamos de 12 meses + this->mes=1; //pasamos al mes 1 + this->anio++; //del a駉 siguiente + } + } + + return temp; //devolvemos el objeto antes del incrementado +} + +ostream& operator<<(ostream& os, const Fecha& f) { + os << f.getDia() << "/" << f.getMes() << "/" << f.getAnio(); + return os; +} + + +Fecha Fecha::operator+(const int &dias)const { //f+5 + Fecha resultado = *this; + for(int i = 0; i < dias; i++){ + ++resultado; + } + return *this; +} + diff --git a/parte1/src/main.cpp b/parte1/src/main.cpp new file mode 100644 index 0000000..0668d50 --- /dev/null +++ b/parte1/src/main.cpp @@ -0,0 +1,63 @@ + +#include +#include +#include +#include "Fecha.h" //definicion de la clase Fecha +#include "Cliente.h" // definicion de la clase Cliente + +using namespace std; + +int main() { + Fecha f1(29,2,2001), f3(29,2,2004), f4(29,2,1900); //Fecha f5; //no permitido + + const Fecha f2=f1; //indica que metodo se esta ejecutando aqui + + f1.setFecha(f3.getDia()-3, f3.getMes()-2, 2007); //29-3/2-2/2007 --> f1=26/1/2007 + + cout << "Fechas: "; f1.ver(); cout << ", "; f2.ver(); cout << ", "; + f3.ver(); cout << ", "; f4.ver(); cout << endl; + + if (f3.bisiesto() && !f2.bisiesto() && f4.bisiesto()==false) + cout << f3.getAnio() << " es bisiesto, " << f2.getAnio() << " y " << f4.getAnio() << " no\n"; + + + f4.setFecha(31, 12, 2000); //f4=31/12/2000 + +// cout << "f4: "; f4.ver(); cout << endl; + + f3=f4++; //indica que m茅todo/s se esta ejecutando aqui +// cout << "f3: "; f3.ver(); cout << endl; + ++f4; + +// cout << "f4: "; f4.ver(); cout << endl; + +// f1=2+f2+3; + cout << "Fechas: "; f1.ver(); cout << ", "; f2.ver(); cout << ", "; + f3.ver(); cout << ", "; f4.ver(); cout << endl; + + Cliente *p = new Cliente(75547001, "Susana Diaz", f1); + f1.setFecha(7,10,2015); + + Cliente c(75547999, "Juan Sin Miedo", Fecha(29,2,2000)); + const Cliente j(44228547, "Luis", f1); + c.setNombre("Juan Palomo"); + + if (j==c) + cout << "\nj y c son iguales\n"; + else + cout << "\nj y c no son iguales\n"; + cout << p->getDni() << " - " << c.getNombre() << ": " << j.getFecha() << endl; + cout << *p << "\n" << c << "\n" << j << "\n"; + c = *p; + p->setNombre("Susanita"); p->setFecha(p->getFecha()+10); + cout << "\nDatos de los clientes: \n"; + cout << *p << "\n" << c << "\n" << j << "\n"; + delete p; p = NULL; + + if (std::filesystem::exists("/etc/os-release")){ + system("read -p 'Presione una tecla para continuar' " ); return 0; + }else{ + system("PAUSE"); return 0; + } + +} diff --git a/parte2/Makefile b/parte2/Makefile new file mode 100644 index 0000000..29fb841 --- /dev/null +++ b/parte2/Makefile @@ -0,0 +1,39 @@ +# --- Configuraci贸n general --- +CXX = g++ +CXXFLAGS = -Wall -Wextra -std=c++17 -Iinclude +SRC_DIR = src +OBJ_DIR = obj +BIN_DIR = bin +TARGET = $(BIN_DIR)/programa + +# --- Archivos fuente y objeto --- +SRCS = $(wildcard $(SRC_DIR)/*.cpp) +OBJS = $(SRCS:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.o) + +# --- Regla principal --- +all: $(TARGET) + +# --- C贸mo generar el ejecutable --- +$(TARGET): $(OBJS) + @mkdir -p $(BIN_DIR) + $(CXX) $(CXXFLAGS) $(OBJS) -o $(TARGET) + @echo "Compilaci贸n completa. Ejecutable en $(TARGET)" + +# --- C贸mo compilar cada .cpp a .o --- +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp + @mkdir -p $(OBJ_DIR) + $(CXX) $(CXXFLAGS) -c $< -o $@ + +# --- Limpieza --- +clean: + rm -rf $(OBJ_DIR)/*.o + @echo "Archivos objeto eliminados." + +mrproper: clean + rm -rf $(BIN_DIR)/* + @echo "Ejecutables eliminados." + +# --- Ejecuci贸n r谩pida --- +run: all + ./$(TARGET) +