terminado parte1

This commit is contained in:
Pablo
2025-11-09 22:12:40 +01:00
parent d90429a4d9
commit 44826a2687
7 changed files with 321 additions and 0 deletions

58
parte1/src/Cliente.cpp Normal file
View File

@@ -0,0 +1,58 @@
#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,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;
}

99
parte1/src/Fecha.cpp Normal file
View File

@@ -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<>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 && (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<>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...
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<>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 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;
}

63
parte1/src/main.cpp Normal file
View File

@@ -0,0 +1,63 @@
#include <cstdlib>
#include <iostream>
#include <filesystem>
#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;
}
}