Sunteți pe pagina 1din 8

Exámen ejemplo no 1.

1. (2.5 puntos) Disponemos de los datos del campeonato de fórmula 1 en un fichero. pilotos.dat donde cada
registro consta de un código de 3 letras que indica el piloto y un entero que guarda su puntuación. Tenemos
los datos de la ultima carrera en fichero posiciones.dat, con el mı́smo código del piloto y la posicion obtenida
en la ultima carrera. Se pide que leamos posiciones.dat y actualicemos pilotos.dat, teniendo en cuenta que
solamente puntuan los 8 primeros pilotos, recibiendo 10,8,6,5,4,3,2 y 1 punto, respectivamente.

Solución:
#include <f s t r e a m>
#include <i o s t r e a m>
using namespace s t d ;
void m e z c l a r f i c h e r o s ( void ) ;
const i nt TAM COD( 3 ) ;
const i nt TAM CIF ( 1 2 ) ;
struct P i l o t o {
char c o d i g o [TAM COD] ;
i nt punto s ;
};
struct P o s P i l o t o {
char c o d i g o [TAM COD] ;
i nt o r den ;
};
i nt main ( void ) {
mezclarficheros ( );
}
void m e z c l a r f i c h e r o s ( ) {
ifstream posiciones ;
fstream p i l o t o s ;
str ea mpo s p o s a c t u a l ;
p o s i c i o n e s . open ( ” p o s i c i o n e s . dat ” , i o s : : i n | i o s : : b i n a r y ) ;
if (! posiciones )
e x i t ( − 1 );
p i l o t o s . open ( ” p i l o t o s . dat ” , i o s : : i n | i o s : : out | i o s : : b i n a r y ) ;
if (! pilotos )
e x i t ( − 1 );
P o s P i l o t o pos ;
Piloto p;
p o s i c i o n e s . r ea d ( ( char ∗ ) &pos , s i z e o f ( P o s P i l o t o ) ) ;
while ( ! p o s i c i o n e s . e o f ( ) ) {
p i l o t o s . s e e k g ( 0 , i o s : : beg ) ;
pos actual = pi l o to s . t e l l g ( ) ;
p i l o t o s . r ea d ( ( char ∗ ) &p , s i z e o f ( P i l o t o ) ) ;
while ( ! p i l o t o s . e o f ( ) ) {
i f ( ! strcmp ( p . c o d i g o , pos . c o d i g o ) ) {
i f ( pos . o r den <= 8 ) {
switch ( pos . o r den ) {
case 1 :
p . punto s+=10;
break ;
case 2 :
p . punto s+=8;
break ;
case 3 :
p . punto s+=6;
break ;
case 4 :
p . punto s+=5;
break ;

Página 1 de 8
Exámen ejemplo no 1.

case 5 :
p . punto s+=4;
break ;
case 6 :
p . punto s+=3;
break ;
case 7 :
p . punto s+=2;
break ;
case 8 :
p . punto s+=1;
break ;
}
p i l o t o s . s e e k g ( p o s a c t u a l , i o s : : beg ) ;
p i l o t o s . w r i t e ( ( char ∗ ) &p , s i z e o f ( P i l o t o ) ) ;
}
break ;
}
pos actual = pi l o to s . t e l l g ( ) ;
p i l o t o s . r ea d ( ( char ∗ ) &p , s i z e o f ( P i l o t o ) ) ;
}
p o s i c i o n e s . r ea d ( ( char ∗ ) &pos , s i z e o f ( P o s P i l o t o ) ) ;
}
pilotos . close ();
posiciones . close () ;
}

2. (2.5 puntos) Hemos organizado una carrera a través del pais. En ella pueden participar coches, motos y cuads.
La reglamentacion obliga a cada participante a pagar una cantidad por la incripción. Para los coches se pagarán
10 euros por cada centı́metro cúbico de motor, para las motos 100 euros por cada kilo de peso y para los cuads
una cuota fija de 3000 euros. Hemos creado una clase Participante, de la que derivan las clases Coche, Moto y
Cuad. Crea una funcion recaudacion que reciba un array de punteros a Participante y el número de participantes
inscritos y te devuelva la recaudación.

Solución:

#include <f s t r e a m>


#include <i o s t r e a m>
using namespace s t d ;
class Participante {
public :
v i r t u a l i nt p r e c i o ( ) = 0 ;
};
i nt r e c a u d a c i o n ( P a r t i c i p a n t e ∗ p a r t i c i p a n t e s [ ] , i nt n u m P a r t i c i p a n t e s ) ;
c l a s s Coche : public P a r t i c i p a n t e {
public :
Coche ( i nt c c )
: cc ( cc ){
}
i nt p r e c i o ( ) ;
private :
s t a t i c const i nt c u o t a p o r c c = 1 0 ;
i nt c c ;
};
c l a s s Moto : public P a r t i c i p a n t e {

Página 2 de 8
Exámen ejemplo no 1.

public :
Moto ( i nt peso )
: peso ( peso ) {
}
i nt p r e c i o ( ) ;
s t a t i c const i nt c u o t a p o r k i l o = 1 0 0 ;
private :
i nt peso ;
};
c l a s s Cuad : public P a r t i c i p a n t e {
public :
i nt p r e c i o ( ) ;
s t a t i c const i nt c u o t a f i j a= 3 0 0 0 ;
};
i nt Coche : : p r e c i o ( ) {
return c u o t a p o r c c ∗ c c ;
}
i nt Moto : : p r e c i o ( ) {
return c u o t a p o r k i l o ∗ peso ;
}
i nt Cuad : : p r e c i o ( ) {
return c u o t a f i j a ;
}
i nt main ( ) {
Moto m( 3 ) ;
Coche c ( 2 ) ;
Cuad q ;
Participante ∗ inscripcion [ 3 ] ;
i n s c r i p c i o n [ 0 ] = &m;
i n s c r i p c i o n [ 1 ] = &c ;
i n s c r i p c i o n [ 2 ] = &q ;
co ut << ” To ta l r eca uda do : ” << r e c a u d a c i o n ( i n s c r i p c i o n , 3 ) << e n d l ;
}
i nt r e c a u d a c i o n ( P a r t i c i p a n t e ∗ p a r t i c i p a n t e s [ ] , i nt n u m P a r t i c i p a n t e s ) {
i nt t o t a l ( 0 ) ;
for ( i nt n=0; n<n u m P a r t i c i p a n t e s ; n++){
t o t a l += p a r t i c i p a n t e s [ n]−> p r e c i o ( ) ;
}
return t o t a l ;
}

3. (2.5 puntos) Tenemos un tipo Vehiculo que tiene una cantidad variable de objetos Destino. Tenemos una clase
Destino que representa una población con su código como entero y un bool que dice si el vehı́culo la ha visitado
hoy o no.
Haz, mediante la sobrecarga de operadores, que se pueda crear un programa como este.
// Crear d e s t i n o s con un c o d i g o
D e s t i n o d1 ( 1 2 3 ) , d2 ( 4 5 6 ) ;
// Crear v e h i c u l o s con una c a n t i d a d de d e s t i n o s i n i c i a l
V e h i c u l o v1 ( 1 0 ) , v2 ( 3 0 ) ;
// Agregar un d e s t i n o a un v e h i c u l o
v1 += d1 ;
v1 += d2 ;
// Asign ar un d e s t i n o en una p o s i c i o n c o n c r e t a a un v e h i c u l o
v2 [ 0 ] = d2 ;
// Crear un v e h i c u l o a con l o s d e s t i n o s de o t r o
V e h i c u l o v3 ( v2 ) ;

Página 3 de 8
Exámen ejemplo no 1.

// Crear un v e h i c u l o nuevo con l o s d e s t i n o s de o t r o s


V e h i c u l o v4 = v1 + v2 + v3 ;
// Hacer que un v e h i c u l o e x i s t e n t e s e i g u a l e a o t r o
v2 = v3 ;
// Agregar a un v e h i c u l o 100 p o s i c i o n e s mas para d e s t i n o s
v1 += 1 0 0 ;
// Agregar a un v e h i c u l o l o s d e s t i n o s de o t r o
v1 += v2 ;
// Mostrar en p a n t a l l a un v e h i c u l o con s u s d e s t i n o s
co ut << ” v1 y v2 ” << v1 << v2 << e n d l ;
// Comparar s i un v e h i c u l o t i e n e mas d e s t i n o s que o t r o
i f ( v1 > v2 )
co ut << ” v1 e s mayor a v2 ” << v1 << v2<< e n d l ;
// D e c i d i r s i un v e h i c u l o ha v i s i t a d o a t o d o s s u s d e s t i n o s
i f ( ! v1 )
co ut << ” v1 no ha v i s i t a d o a t o d o s ” << v1<< e n d l ;
// Marcar e l d e s t i n o 2 d e l v e h i c u l o como v i s i t a d o
v1 ∗= 2 ;

Solución:

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


using namespace s t d ;
class Destino {
public :
D e s t i n o ( i nt c o d i g o ) ;
D e s t i n o ( const D e s t i n o & d e s t i n o ) ;
D e s t i n o & operator=(const D e s t i n o & d e s t i n o ) ;
fri end o str ea m & operator <<(o str ea m & out , D e s t i n o & d ) ;

i nt c o d i g o ;
bool v i s i t a d o ;

};
D e s t i n o : : D e s t i n o ( const D e s t i n o & d )
: codigo (d . codigo ) , v i s i t a d o (d . v i s i t a d o ){
}
D e s t i n o : : D e s t i n o ( i nt c o d i g o =0)
: codigo ( codigo ) , v i s i t a d o ( fal s e ){
}
D e s t i n o & D e s t i n o : : operator= ( const D e s t i n o & d e s t i n o ) {
codigo = destino . codigo ;
visitado = destino . visitado ;
return ∗ t h i s ;
}
o str ea m & operator <<(o str ea m & out , D e s t i n o & d ) {
out << ” D e s t i n o : ” << d . c o d i g o
<< ” v i s i t a d o : ” << ( d . v i s i t a d o ? ” SI ” : ”NO” )
<< e n d l ;
return out ;
}
class Vehiculo {
public :
V e h i c u l o ( const i nt a r r a y S i z e ) ;
V e h i c u l o ( const V e h i c u l o & v ) ;
V e h i c u l o & operator=(const V e h i c u l o & v e h i c u l o ) ;
˜ Vehiculo ( ) ;

Página 4 de 8
Exámen ejemplo no 1.

void operator +=(const D e s t i n o & d e s t i n o ) ;


D e s t i n o & operator [ ] ( i nt numDestino ) ;
void operator +=(i nt numDestinos ) ;
void operator +=(const V e h i c u l o & v e h i c u l o ) ;
bool operator >(const V e h i c u l o & v ) ;
bool operator ! ( ) ;
void operator ∗=( i nt numDestino ) ;
fri end o str ea m & operator <<(o str ea m & out , V e h i c u l o & v ) ;
private :
Destino ∗ d e s t i n o s ;
i nt numDestinos ;
i nt a r r a y S i z e ;

fri end V e h i c u l o operator+(const V e h i c u l o & a , const V e h i c u l o & b ) ;


};
V e h i c u l o : : V e h i c u l o ( const i nt a r r a y S i z e )
: a r r a y S i z e ( a r r a y S i z e ) , numDestinos ( 0 ) {
i f ( arraySize > 0){
d e s t i n o s = new D e s t i n o [ a r r a y S i z e ] ;
} else{
destinos = 0;
}
}
V e h i c u l o : : V e h i c u l o ( const V e h i c u l o & v )
: a r r a y S i z e ( v . a r r a y S i z e ) , numDestinos ( v . numDestinos ) {
i f ( a r r a y S i z e >0){
d e s t i n o s = new D e s t i n o [ a r r a y S i z e ] ;
for ( i nt n= 0 ; n< numDestinos ; n++){
destinos [ n] = v . destinos [ n ] ;
}
} else{
destinos = 0;
}
}
Vehiculo : : ˜ Vehiculo (){
i f ( d e s t i n o s != 0 ) {
delete [ ] d e s t i n o s ;
}
}
void V e h i c u l o : : operator +=(const V e h i c u l o & v e h i c u l o ) {
i f ( numDestinos+ v e h i c u l o . numDestinos >= a r r a y S i z e ) {
D e s t i n o ∗ newDestino s = new D e s t i n o [ a r r a y S i z e ∗ 2 ] ;
for ( i nt n= 0 ; n< a r r a y S i z e ; n++){
newDestino s [ n]= d e s t i n o s [ n ] ;
}
delete [ ] d e s t i n o s ;
d e s t i n o s = newDestino s ;
}
for ( i nt n= 0 ; n< v e h i c u l o . numDestinos ; n++){
d e s t i n o s [ numDestinos++]= v e h i c u l o . d e s t i n o s [ n ] ;
}
}
void V e h i c u l o : : operator +=(const D e s t i n o & d e s t i n o ) {
i f ( numDestinos >= a r r a y S i z e ) {
D e s t i n o ∗ newDestino s = new D e s t i n o [ a r r a y S i z e ∗ 2 ] ;
for ( i nt n= 0 ; n< a r r a y S i z e ; n++){
newDestino s [ n]= d e s t i n o s [ n ] ;

Página 5 de 8
Exámen ejemplo no 1.

}
delete [ ] d e s t i n o s ;
d e s t i n o s = newDestino s ;
}
d e s t i n o s [ numDestinos ++] = d e s t i n o ;
}
void V e h i c u l o : : operator +=(i nt i n c ) {
D e s t i n o ∗ newDestino s = new D e s t i n o [ a r r a y S i z e + i n c ] ;
for ( i nt n= 0 ; n< a r r a y S i z e ; n++){
newDestino s [ n]= d e s t i n o s [ n ] ;
}
delete [ ] d e s t i n o s ;
d e s t i n o s = newDestino s ;
a r r a y S i z e+=i n c ;
}
D e s t i n o & V e h i c u l o : : operator [ ] ( i nt numDestino ) {
return d e s t i n o s [ numDestino ] ;
}
V e h i c u l o operator+(const V e h i c u l o & a , const V e h i c u l o & b ) {

V e h i c u l o newVehiculo ( a . a r r a y S i z e + b . a r r a y S i z e ) ;
newVehiculo . numDestinos = a . numDestinos+ b . numDestinos ;
i nt n=0;
for ( n= 0 ; n< a . numDestinos ; n++){
newVehiculo . d e s t i n o s [ n]= a . d e s t i n o s [ n ] ;
}
for ( i nt m = 0 ; m< b . numDestinos ; m++){
newVehiculo . d e s t i n o s [ n+m]= b . d e s t i n o s [m] ;
}
return newVehiculo ;
}
V e h i c u l o & V e h i c u l o : : operator=(const V e h i c u l o & v e h i c u l o ) {
// p r o t e c c i o n c o n t r a ’ x = x ; ’
i f (& v e h i c u l o == t h i s ) {
return ∗ t h i s ;
}
i f ( d e s t i n o s != 0 ) {
delete [ ] d e s t i n o s ;
}
d e s t i n o s = new D e s t i n o [ v e h i c u l o . a r r a y S i z e ] ;
numDestinos = v e h i c u l o . numDestinos ;
for ( i nt n= 0 ; n< numDestinos ; n++){
d e s t i n o s [ n]= v e h i c u l o . d e s t i n o s [ n ] ;
}
return ∗ t h i s ;
}
bool V e h i c u l o : : operator >(const V e h i c u l o & v ) {
return numDestinos > v . numDestinos ;
}
bool V e h i c u l o : : operator ! ( ) {
for ( i nt n= 0 ; n< numDestinos ; n++){
if (! destinos [n ] . visitado )
return f a l s e ;
}
return true ;
}
void V e h i c u l o : : operator ∗=( i nt numDestino ) {

Página 6 de 8
Exámen ejemplo no 1.

d e s t i n o s [ numDestino ] . v i s i t a d o = true ;
}
o str ea m & operator <<(o str ea m & out , V e h i c u l o & v ) {
out << e n d l << ” V e h i c u l o pa r a ” << v . a r r a y S i z e
<< ” d e s t i n o s , con ” << v . numDestinos << ” . ” <<e n d l ;
for ( i nt n= 0 ; n< v . numDestinos ; n++){
out << ” \ t ” << v . d e s t i n o s [ n ] ;
}
out << e n d l ;
return out ;
}
i nt main ( ) {
D e s t i n o d1 ( 1 2 3 ) , d2 ( 4 5 6 ) ;
co ut << ” D e s t i n o s i n i c i a l e s ” << d1 << d2 << e n d l ;
V e h i c u l o v1 ( 1 0 ) , v2 ( 3 0 ) ;
co ut << ” V e h i c u l o s i n i c i a l e s ” << v1 << v2 << e n d l ;
v1 += d1 ;
v1 += d2 ;
co ut << ” v1 a g r eg a ndo d e s t i n o s d1 y d2” << v1 << e n d l ;
co ut << ” v2 o r i g i n a l ” << v2 << e n d l ;
v2 += d1 ;
v2 [ 0 ] = d2 ;
co ut << ” v2 cambiando l a 0 por d2 ” << v2 << e n d l ;
V e h i c u l o v3 ( v2 ) ;
co ut << ” v3 c r e a d o a p a r t i r de v2 ” << v3 << e n d l ;
V e h i c u l o v4 = v1 + v2 + v3 ;
co ut << ”v4 , suma de v1 , v2 y v3 ” << v4 << e n d l ;
v2 = v3 ;
co ut << ” v2 a s i g n a n d o l e v3 ” << v2 << e n d l ;
v1 += 1 0 0 ;
co ut << ” v1 a g r eg a ndo con += e l e n t e r o 100 ” << v1 << e n d l ;
v1 += v2 ;
co ut << ” v1 a g r eg a ndo con += v2 ” << v1 << e n d l ;
co ut << ” v1 y v2 ” << v1 << v2 << e n d l ;
i f ( v1 > v2 )
co ut << ” v1 e s mayor a v2 ” << v1 << v2<< e n d l ;
i f ( ! v1 )
co ut << ” v1 no ha v i s i t a d o a t o d o s ” << v1<< e n d l ;
co ut << ” v1 a n t e s de v i s i t a r e l 1 ” << v1 << e n d l ;
v1 ∗= 1 ;
co ut << ” v1 d e s p u e s de v i s i t a r e l 1 ” << v1 << e n d l ;
}

4. (2.5 puntos) Crea una funcion formatear que al recibir una cadena (de un tamaño desconocido) te devuelve otra
con los espacios duplicados y con un salto de linea agregado después de cada punto. No puedes usar la función
strlen para calcular su tamaño. En lugar de indexar los arrays usa punteros y desplazamientos.

Solución:

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


using namespace s t d ;
char ∗ f o r m a t e a r ( char ∗ s ) {
const char ∗ s c ;
i nt l e n =0;
// f o r ( s c = s ; ∗ s c != ’ \ 0 ’ ; ++s c ){

Página 7 de 8
Exámen ejemplo no 1.

for ( s c = s ; ∗ s c ; ++s c ) {
l e n ++;
}
// e l d o b l e de gran de mas e s p a c i o para \0
char ∗ r = new char [ ( l e n ∗ 2 ) + 1 ] ;
i nt s i =0; // i n d i c e de s
i nt r i =0; // i n d i c e d e l r e t o r n o
// w h i l e ( ∗ ( s+s i ) != ’ \ 0 ’ ) {
while ( ∗ ( s+s i ) ) {
i f ( ∗ ( s+s i ) == ’ ’ ) {
∗ ( r+r i )= ’ ’ ;
r i ++;
∗ ( r+r i )= ’ ’ ;
r i ++;
} e l s e i f ( ∗ ( s+s i ) == ’ . ’ ) {
∗ ( r+r i )= ’ . ’ ;
r i ++;
∗ ( r+r i )= ’ \n ’ ;
r i ++;
} else{
∗ ( r+r i )= ∗ ( s+s i ) ;
r i ++;
}
s i ++;
}
// ∗ ( r+r i )= ’ \ 0 ’ ;
∗ ( r+r i )= 0 ;
return r ;
}
i nt main ( void ) {
char ∗ s ;
s = f o r m a t e a r ( ” a bc d e f g h i j . a b c d e f g . ABCD1234” ) ;
co ut << s << e n d l ;
delete [ ] s ;
s = f o r m a t e a r ( ” F a c i l , d i f i c i l . Muy d i f i c i l . ” ) ;
co ut << s << e n d l ;
delete [ ] s ;
}

Página 8 de 8

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