Sunteți pe pagina 1din 5

Exámen de LP.

Septiembre 2007 Práctica

1. 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 ;
char d i r e c c i o n [ MAXDIRECCION ] ;
unsigned i nt numeroMiembros ;
gasto gastosFamilia ;
};
struct s e t F a m i l i a s {
f a m i l i a f a m i l i a s [ MAXFAMILIAS ] ;
i nt numFamilias ; // Numero t o t a l de f a m i l i a s e x i s t e n t e s en e l v e c t o r f a m i l i a s
};

(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:

# include <i o s t r e a m >


# include <c a s s e r t >
using namespace s t d ;
const unsigned i nt MAXFAMILIAS ( 10 ) ;
const unsigned i nt MAXNOMBRE ( 30 ) ;
const unsigned i nt MAXDIRECCION ( 50 ) ;
class gasto {
public :
float gastosTransporte ;
float gastosVivienda ;
float gastosAlimentacion ;
void operator+=(g a s t o & g ) {
g a s t o s T r a n s p o r t e += g . g a s t o s T r a n s p o r t e ;
g a s t o s V i v i e n d a += g . g a s t o s V i v i e n d a ;
g a s t o s A l i m e n t a c i o n+= g . g a s t o s A l i m e n t a c i o n ;
}
};
class f a m i l i a {
public :
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 ;
char d i r e c c i o n [ MAXDIRECCION ] ;
unsigned i nt numeroMiembros ;
gasto gastosFamilia ;
familia ()
: t i p o D o m i c i l i o ( c a l l e ) , numeroMiembros ( 0 ) {
s t r c p y ( this −>nombre , ” ” ) ;
s t r c p y ( this −>d i r e c c i o 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 ;
};

i nt comparar ( const char ∗ cs , const char ∗ c t ) ;


void o r d e n a r F a m i l i a s ( f a m i l i a ∗ a r r a y [ ] , i nt numFamilias ) ;
f a m i l i a pedirDatosFamilia ( ) ;
o str ea m & operator << ( o str ea m & o , f a m i l i a & f ) ;
i s t r e a m & operator >> ( i s t r e a m & o , f a m i l i a & f ) ;

Página 2 de 5
Exámen de LP. Septiembre 2007 Práctica

i s t r e a m & operator >> ( i s t r e a m & o , g a s t o & g ) ;

bool operator < ( i nt n , const f a m i l i a & f ) {


return ( 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
}

i s t r e a m & operator >> ( i s t r e a m & i , f a m i l i a & f ) {


// 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 ! !
return i
>> f . g a s t o s F a m i l i a ;
}
i s t r e a m & operator >> ( i s t r e a m & i , g a s t o & g ) {
return i
>> g . g a s t o s T r a n s p o r t e
>> g . g a s t o s V i v i e n d a
>> g . g a s t o s A l i m e n t a c i o n
;
}
void o r d e n a r F a m i l i a s ( f a m i l i a ∗ a r r a y [ ] , i nt numFamilias ) {
i f ( numFamilias < 2 ) return ;
bool o r dena da s = f a l s e ;
f a m i l i a ∗ tmp=0;
while ( ! o r dena da s ) {
o r dena da s = true ;
for ( i nt n= 0 ; n<numFamilias −1; n++){
i f ( comparar ( a r r a y [ n]−>nombre , a r r a y [ n+1]−>nombre ) > 0 ) {
tmp = a r r a y [ n ] ;
array [ n ] = array [ n+1];
a r r a y [ n+1] = tmp ;
o r dena da s = f a l s e ;
}
}
}
}
i nt comparar ( const char ∗ cs , const char ∗ c t ) {
char d i f =0;
while ( true ) {
d i f = ∗ cs − ∗ ct ;
c t ++;
i f ( ( d i f != 0 ) | | ( ∗ c s == 0 ) ) {
break ;

}
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

S-ar putea să vă placă și