Documente Academic
Documente Profesional
Documente Cultură
(a) Implementar una función que devuelva los datos de una familia completa, leı́dos por teclado dentro de la
propia función.
(b) Implementar una función que ordene alfabéticamente un conjunto de familias por el campo nombre. Para
ello no se podrá modificar el array con los datos de las familias sino que se deberá utilizar un array adicional
de punteros. Además para la comparación de cadenas no se podrán utilizar las funciones predefinidas para
cadenas (strlen, strcmp, . . . ).
2. Dadas las siguientes estructuras y declaraciones:
# include <i o s t r e a m >
using namespace s t d ;
const unsigned i nt MAXFAMILIAS ( 10 ) ;
const unsigned i nt MAXNOMBRE ( 30 ) ;
const unsigned i nt MAXDIRECCION ( 50 ) ;
struct g a s t o {
float gastosTransporte ;
float gastosVivienda ;
float gastosAlimentacion ;
};
struct f a m i l i a {
char nombre [ MAXNOMBRE ] ;
enum TDo micilio { c a l l e , p l a z a , a v e n i d a } t i p o D o m i c i l i o ;
unsigned i nt numer o g a sto s ;
gasto ∗ gastosFamilia ;
};
Convertir las estructuras gasto y familia en clases e implementar las funciones necesarias para que el siguiente
código se ejecute de forma correcta:
i nt main ( ) {
F a m i l i a f 1 ( ” Lopez ” , c a l l e , 3 ) ; // l a f a m i l i a t i e n e 3 g a s t o s
Gasto g ;
cout<<” \ n I n t r o d u c i r g a s t o ( f l o a t , f l o a t , f l o a t ) ” ;
c i n >>g ;
cout<<” \ n I n t r o d u c i r g a s t o s f a m i l i a ” ;
c i n >>f 1 ; // e l e x t r a c t o r s o l o t i e n e que l e e r l o s g a s t o s de l a f a m i l i a
i f (0 < f1 )
Página 1 de 5
Exámen de LP. Septiembre 2007 Práctica
f 1+g ; // a g r e g a r un g a s t o a l a f a m i l i a
system ( ” pa use ” ) ;
return 0 ;
}
Solución:
}
f a m i l i a ( const char nombre [ ] , TDo micilio t i p o D o m i c i l i o , i nt numMiembros )
: t i p o D o m i c i l i o ( ( TDo micilio ) t i p o D o m i c i l i o ) , numeroMiembros ( numeroMiembros ) {
s t r c p y ( this −>nombre , nombre ) ;
}
void operator +( g a s t o g ) {
g a s t o s F a m i l i a+=g ;
}
};
class setFamilias {
public :
f a m i l i a f a m i l i a s [ MAXFAMILIAS ] ;
i nt numFamilias ;
};
Página 2 de 5
Exámen de LP. Septiembre 2007 Práctica
}
c s ++;
}
return d i f ;
}
f a m i l i a pedirDatosFamilia (){
familia f ;
co ut << e n d l << ”Nombre : ” ;
c i n . g e t l i n e ( f . nombre ,MAXNOMBRE) ;
i nt t i p o ;
co ut << e n d l << ” Tipo d i r e c c i o n ( 0 : c a l l e , 1 : p l a z a , 2 : a v e n i d a ) : ” ;
cin . clear ( ) ;
c i n >> t i p o ;
switch ( t i p o ) {
Página 3 de 5
Exámen de LP. Septiembre 2007 Práctica
case f a m i l i a : : c a l l e :
f . tipoDomicilio = familia : : c a l l e ;
break ;
case f a m i l i a : : p l a z a :
f . tipoDomicilio = fa mi l i a : : plaza ;
break ;
case f a m i l i a : : a v e n i d a :
f . tipoDomicilio = f a m i l i a : : avenida ;
break ;
}
co ut << e n d l << ” D i r e c c i o n : ” ;
cin . ignore ( ) ;
c i n . g e t l i n e ( f . d i r e c c i o n , MAXDIRECCION) ;
co ut << e n d l << ”Numero de miembros : ” ;
c i n >> f . numeroMiembros ;
co ut << e n d l << ” Ga sto s ( Tr a nspo r te , Vivienda , A l i m e n t a c i o n ) : ” ;
c i n >> f . g a s t o s F a m i l i a . g a s t o s T r a n s p o r t e
>> f . g a s t o s F a m i l i a . g a s t o s V i v i e n d a
>> f . g a s t o s F a m i l i a . g a s t o s A l i m e n t a c i o n ;
return f ;
}
o str ea m & operator << ( o str ea m & o , f a m i l i a & f ) {
const char ∗ p = f . t i p o D o m i c i l i o == f a m i l i a : : c a l l e ?
” calle ”
:
f . t i p o D o m i c i l i o==f a m i l i a : : a v e n i d a ?
” avenida”
:
” plaza ” ;
return o
<< f . nombre
<< ” ”
<< p
<< ” ”
<< f . d i r e c c i o n
<< ” ”
<< f . numeroMiembros << ” p e r s o n a s ”
<< ” ”
<< ” t r a n s p o r t e : ” << f . g a s t o s F a m i l i a . g a s t o s T r a n s p o r t e
<< ” v i v i e n d a : ” << f . g a s t o s F a m i l i a . g a s t o s V i v i e n d a
<< ” a l i m e n t a c i o n : ” << f . g a s t o s F a m i l i a . g a s t o s A l i m e n t a c i o n ;
}
/∗
C o n v e r t i r l a s e s t r u c t u r a s g a s t o y f a m i l i a en c l a s e s e implemen t ar l a s f u n c i o n e s
n e c e s a r i a s para que e l s i g u i e n t e c o d i g o s e e j e c u t e de forma c o r r e c t a :
∗/
i nt main ( ) {
f a m i l i a f 1 ( ” Lopez ” , f a m i l i a : : c a l l e , 3 ) ; // l a f a m i l i a t i e n e 3 miembros
gasto g ;
cout<<” \ n I n t r o d u c i r g a s t o ( f l o a t , f l o a t , f l o a t ) ” ;
c i n >>g ;
cout<<” \ n I n t r o d u c i r g a s t o s f a m i l i a ” ;
c i n >>f 1 ; // e l e x t r a c t o r s o l o t i e n e que l e e r l o s g a s t o s de l a f a m i l i a
i f (0 < f1 )
Página 4 de 5
Exámen de LP. Septiembre 2007 Práctica
f 1+g ; // a g r e g a r un g a s t o a l a f a m i l i a
cin . get ( ) ;
return 0 ;
}
Página 5 de 5