ayuda practica1, creacion Makefile

This commit is contained in:
Pablo
2025-11-03 09:56:12 +01:00
parent 72fc476d11
commit 140ab49ea1
6 changed files with 282 additions and 49 deletions

43
Makefile Normal file
View File

@@ -0,0 +1,43 @@
# ===============================
# Makefile para proyecto con Fecha
# ===============================
# --- 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)

31
include/Cliente.h Normal file
View File

@@ -0,0 +1,31 @@
#ifndef CLIENTE_H
#define CLIENTE_H
#include <iostream> //cin, cout
#include "Fecha.h"
using namespace std;
//SI FALTA ALGUN METODO O FUNCION A<>ADIRLO...
class Cliente {
long int dni;
char *nombre;
Fecha fechaAlta;
public:
Cliente(long int d, 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(char *nom);
void setFecha(Fecha f);
bool operator==(Cliente c) const; // if (c1 ===c2)
};
ostream& operator<<(ostream &s, const Cliente &c); //funcion no amiga de la clase
#endif // CLIENTE_H

View File

@@ -1,5 +1,30 @@
#define Fecha_H #ifndef FECHA_H
#define FECHA_H
#include <iostream>
#include <iostream> //cin, cout
using namespace std;
using namespace std;
//SI FALTA ALGUN METODO O FUNCION A<>ADIRLO...
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
};
Fecha operator+(const int &i, const Fecha &f); //const por seguridad y & por velocidad
#endif // FECHA_H

43
src/Cliente.cpp Normal file
View File

@@ -0,0 +1,43 @@
#include <cstdlib>
#include <cstring> //strlen, strcpy
#include <iomanip> //std::setprecision
#include <sstream> //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, 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==(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...

90
src/Fecha.cpp Normal file
View File

@@ -0,0 +1,90 @@
#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) {
/*
if ((mes == 1) || (mes == 3) || (mes == 5) ....
dmax=31;
else if ((mes == 4) || (mes == 6) || (mes == 9) ....
dmax=30;
else if (mes == 2)
dmax=28;
switch (mes) {
case 1:
case 3:
case 5:
...
dmax=31;
break;
case 4:
case 6:
case 9:
...
dmax=30;
break;
default:
dmax=28;
break;
}
*/
//ES MAS RAPIDO Y COMODO USAR UN ARRAY QUE GUARDE LOS DIAS DE CADA MES...
int dmax, diaMes[] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
this->anio=a; //VIP debo asignar a<>o para que al llamar a bisiesto() tenga el a<>o 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) //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<>o 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<>o siguiente
}
}
return *this; //devolvemos el objeto fecha ya incrementado
}
//RESTO DE METODOS Y FUNCIONES A RELLENAR POR EL ALUMNO...

View File

@@ -1,44 +1,45 @@
#include <cstdlib>
#include <iostream> #include <cstdlib>
#include "Fecha.h" //definicion de la clase Fecha #include <iostream>
#include "Cliente.h" // definicion de la clase Cliente #include "Fecha.h" //definicion de la clase Fecha
#include "Cliente.h" // definicion de la clase Cliente
using namespace std;
using namespace std;
int main() {
Fecha f1(29,2,2001), f3(29,2,2004), f4(29,2,1900); //Fecha f5; //no permitido int main() {
const Fecha f2=f1; //indica que metodo se esta ejecutando aqui Fecha f1(29,2,2001), f3(29,2,2004), f4(29,2,1900); //Fecha f5; //no permitido
f1.setFecha(f3.getDia()-3, f3.getMes()-2, 2007); //29-3/2-2/2007 --> f1=26/1/2007 const Fecha f2=f1; //indica que metodo se esta ejecutando aqui
cout << "Fechas: "; f1.ver(); cout << ", "; f2.ver(); cout << ", "; f1.setFecha(f3.getDia()-3, f3.getMes()-2, 2007); //29-3/2-2/2007 --> f1=26/1/2007
f3.ver(); cout << ", "; f4.ver(); cout << endl; 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"; if (f3.bisiesto() && !f2.bisiesto() && f4.bisiesto()==false)
f4.setFecha(31, 12, 2000); //f4=31/12/2000 cout << f3.getAnio() << " es bisiesto, " << f2.getAnio() << " y " << f4.getAnio() << " no\n";
f3=f4++; //indica que método/s se esta ejecutando aqui f4.setFecha(31, 12, 2000); //f4=31/12/2000
++f4; f3=f4++; //indica que método/s se esta ejecutando aqui
f1=2+f2+3; ++f4;
cout << "Fechas: "; f1.ver(); cout << ", "; f2.ver(); cout << ", "; f1=2+f2+3;
f3.ver(); cout << ", "; f4.ver(); cout << endl; 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 *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); Cliente c(75547999, "Juan Sin Miedo", Fecha(29,2,2000));
c.setNombre("Juan Palomo"); const Cliente j(44228547, "Luis", f1);
c.setNombre("Juan Palomo");
if (j==c)
cout << "\nj y c son iguales\n"; if (j==c)
else cout << "\nj y c son iguales\n";
cout << "\nj y c no son iguales\n"; else
cout << p->getDni() << " - " << c.getNombre() << ": " << j.getFecha() << endl; cout << "\nj y c no son iguales\n";
cout << *p << "\n" << c << "\n" << j << "\n"; cout << p->getDni() << " - " << c.getNombre() << ": " << j.getFecha() << endl;
c = *p; cout << *p << "\n" << c << "\n" << j << "\n";
p->setNombre("Susanita"); p->setFecha(p->getFecha()+10); c = *p;
cout << "\nDatos de los clientes: \n"; p->setNombre("Susanita"); p->setFecha(p->getFecha()+10);
cout << *p << "\n" << c << "\n" << j << "\n"; cout << "\nDatos de los clientes: \n";
delete p; p = NULL; cout << *p << "\n" << c << "\n" << j << "\n";
delete p; p = NULL;
system("PAUSE"); return 0;
} system("PAUSE"); return 0;
}