ejemplos de examen - ivep formación sl academia de oposiciones

Anuncio
.
PRACTICOS VI. PROGRAMACION EN C. CAPITULO 1.
EJERCICIOS PROPUESTOS
CAPITULO 1
Ejercicios Propuestos
´Indice
1.
Enunciado agenda.c
Enunciados Ficheros .............................................. 5
5
1.Enunciados Ficheros
Enunciado agenda.c
Hacer una agenda telefonica con listas simples con la siguiente
estructura:
Listing 1.1:
1
2
3
4
5
struc entrada {
char * nombre ;
char * direccion ;
char * telefono ;
};
6
7
8
9
10
struct nodoagenda { struct
entrada datos ; struct
nodoagenda * sig ;
};
11
12
typedef
struct nodoagenda * tipoagenda ;
El menu principal será:
Listing 1.2:
1
2
3
4
5
printf ( " Menú :\ n " );
printf ( " 1) Ver conten ido comple to de la agenda .\ n " );
printf ( " 2) Dar de alta una persona .\ n " );
printf (" 3) Buscar tel e´fonos de una persona .\ n");
printf (" 4) Salir .\ n");
Oposiciones al profesorado de Informa´ tica
www.academiaados.com. Por P. Aldarias
5/10
PRACTICOS VI. PROGRAMACION EN C.
CAPITULO 1. EJERCICIOS PROPUESTOS
• Al inicio del programa deberá leer de un fichero.
Listing 1.3:
1
Tipo age nda lee_ ag enda ( char nomb re_ fiche ro [] )
• Antes de terminar el programa deberá́ guardarse la agenda.
Listing 1.4:
1
void escr ibe _ age nda (( Tip o_ agenda agenda , char no mbre_ fic h ero []);
• Las operaciones del menú se harán con punteros.
6/10
PRACTICOS VI. PROGRAMACION EN C.
CAPITULO 2. SOLUCIONES EJERCICIOS PROPUESTOS
CAPITULO 2
Soluciones ejercicios propuestos
´Indice
1.
Soluciones de Ficheros........................................................... 7
Solución agenda.c
7
1.Soluciones de Ficheros
Soluci ón agenda.c
Listing 2.1: ../cejerciciosp/ficheros/agenda.c
1
2
3
/*
Agenda con listas simples y ficher os
*/
4
5
6
7
# include < stdio . h>
# include < stdlib . h>
# include < string . h>
8
9
# define M AXC ADE N A 200
10
11
enum { Ver =1 , Alta , Buscar , Salir };
12
13
14
15
16
17
struct
char
char
char
};
Entrada {
* nombre ;
* direccion ;
* telefono ;
18
19
20
21
22
struct Nodo Agenda {
struct Entrada datos ;
struct Nodo Agenda * sig ;
};
23
24
typedef struct Nodo Age nd a * Tipo Ag enda ;
25
26
27
void quita_ fin_ de_ linea ( char linea [])
{
Oposiciones al profesorado de Informática
7/10
PRACTICOS VI.
PROGRAMACION EN C. CAPITULO 2. SOLUCIONES EJERCICIOS PROPUESTOS
int i;
for ( i =0; linea [ i ] != ’\0 ’; i ++)
if ( linea [ i] == ’\ n’) {
linea [ i] = ’\0 ’;
break ;
}
28
29
30
31
32
33
34
}
35
36
37
38
39
40
41
42
43
44
45
void m ue st ra _ en tr a da ( struct Nodo Ag enda * e )
// Podr ı́ amos haber pasado e por valor , pero resulta más eficien te
// / y no mucho más
// inc ó modo ) hacerlo por refere nci a :
// pasamos ası́ sólo 4 bytes en lugar de 12.
{
printf ( " Nombre
: %s \ n " , e - > datos . nombre );
printf ( " Dir ecc i ó n : %s \ n " , e - > datos . direccion );
printf ( " Tel é fono : %s \ n " , e - > datos . telefo no );
}
46
47
48
49
void libera_ entrada ( struct Nodo Agenda * e)
{
int i;
50
free ( e-> datos . nombre );
free ( e-> datos . direccion );
free ( e-> datos . telefono );
free ( e);
51
52
53
54
55
}
56
57
58
59
60
61
Tipo Agen da cre a_ agend a ( void )
{
return NULL ;
}
62
63
64
65
struct Nodo Ag enda * b u s ca r _ e n tr a d a _ p or _ no mb r e ( Tipo Ag enda agenda , char nombre [])
{
struct Nodo Agenda * aux ;
66
for ( aux = agenda ; aux != NULL ; aux = aux -> sig )
if ( strcmp ( aux -> datos . nombre , nombre ) == 0)
return aux ;
67
68
69
70
return NULL ;
71
72
}
73
74
75
Tipo Agen da an ya d ir _ e nt r ad a (
76
77
78
Tipo Agen da agenda , char nombre [] ,
char direccion [] , char telefono [])
{
struct Nodo Agenda * aux , * e;
79
80
81
82
/* Aver igu ar si ya tenemos una persona con ese nombre */
if ( b us c a r _ e n t ra d a _ p o r _ no m br e ( agenda , nombre )
!= NULL )
return agenda ;
83
84
85
86
87
88
89
90
91
92
/* Si llegamos aquı́ , es porque no ten ı́ amos regis trad a a esa persona . */
e = malloc ( sizeof ( struct Nodo A gen da ));
e - > datos . nombre = malloc (( strlen ( nombre )+ 1 )* sizeof ( char ));
strcpy ( e - > datos . nombre , nombre );
e-> datos . direccion = malloc (( strlen ( direccion )+ 1 )* sizeof ( char ));
strcpy (e-> datos . direccion , direccion );
e-> datos . telefono = malloc (( strlen ( telefono )+ 1 )* sizeof ( char ));
strcpy ( e-> datos . telefono , telefono );
e-> sig = agenda ;
8/10
Oposiciones al profesorado de Informática
PRACTICOS VI. PROGRAMACION EN C.
CAPITULO 2. SOLUCIONES EJERCICIOS PROPUESTOS
agenda = e;
return agenda ;
93
94
95
}
96
97
98
99
void muest ra _ agend a ( Tipo A gen da agenda )
{
struct Nodo Agenda * aux ;
100
for ( aux = agenda ; aux != NULL ; aux = aux -> sig )
muestra_ entrada ( aux );
101
102
103
}
104
105
106
107
108
void li ber a_ agen da ( Tipo A genda agenda )
{
struct Nodo Agenda * aux , * siguiente ;
109
aux = agenda ;
while ( aux != NULL ) {
siguiente = aux -> sig ;
libera_ entrada ( aux );
aux = siguiente ;
}
110
111
112
113
114
115
116
}
117
118
119
120
121
void escr ibe _ age nda ( Ti po A gen da agenda , char nomb r e_ ficher o [])
{
struct Nodo Agenda * aux ;
FILE * fp;
122
fp = fopen ( nombre_ fichero , " w " );
for ( aux = agenda ; aux != NULL ; aux = aux -> sig )
fprintf ( fp , " %s \ n %s \ n %s \ n " ,
aux - > datos . nombre ,
aux -> datos . direccion ,
aux -> datos . telefono );
fclose ( fp );
123
124
125
126
127
128
129
}
130
131
132
133
134
135
136
137
Tipo Agen da lee_ ag enda ( char nombr e_ fiche ro [])
{
Tipo Agen da agenda ;
struct Entrada * entrada_ leida ;
FILE * fp;
char nombre [ M A X C A D E N A +1] , direccion [ M A X C A D E N A +1] , telefono [ M A X C A D E N A +1];
int longitud ;
138
139
agenda = crea_ agenda ();
140
141
142
143
144
145
146
147
fp = fopen ( nombre_ fichero , " r");
if ( fp != NULL ) // Si hay fichero
{
while (1) {
fgets ( nombre , MAXCADENA , fp );
if ( feof ( fp )) break ; // Si se acab ´o el fichero ,
quita_ fin_ de_ linea ( nombre );
acabar la lectura .
148
149
150
fgets ( direccion , M AXC ADENA , fp );
quita_ fin_ de_ linea ( direccion );
151
152
153
fgets ( telefono , M AXC ADE NA , fp );
quita_ fin_ de_ linea ( telefono );
154
155
156
157
agenda = a ny ad i r_ en tr a da ( agenda , nombre , direccion , tele fono );
}
fclose ( fp );
Oposiciones al profesorado de Informática
9/10
PRACTICOS VI. PROGRAMACION EN C.
CAPITULO 2. SOLUCIONES EJERCICIOS PROPUESTOS
} // if
return agenda ;
158
159
160
}
161
162
163
164
165
166
/*
***********************************************************************
* Programa prin ci pal
***********************************************************************
*/
167
168
169
170
171
172
173
174
175
176
int main ( void )
{
Tipo Agen da miage nda ;
struct Nodo Agenda * encontrada ;
int opcion ;
char nombre [ M A X C A D E N A +1];
char direcc ion [ M A X C A D E N A +1];
char tel efono [ M A X C A D E N A +1];
char linea [ M A X C A D E N A +1];
177
miagenda = lee_ agenda (" agenda . txt ");
do {
printf ( " Menú :\ n " );
printf ( " 1) Ver conten ido comple to de la agenda .\ n " );
printf ( " 2) Dar de alta una persona .\ n " );
printf (" 3) Buscar tel e´fonos de una persona .\ n");
printf (" 4) Salir .\ n");
printf ( " Opci ó n : " );
gets ( linea ); sscanf ( linea , " %d " , & opcion );
178
179
180
181
182
183
184
185
186
187
switch ( opcion ) {
188
189
case Ver :
muestra_ agenda ( miagenda );
break ;
190
191
192
193
case Alta :
printf ( " Nombre
: " ); gets ( nombre );
printf ( " Dir ecc i ó n : " ); gets ( d irecc ion );
printf ( " Tel é fono : " ); gets ( t el efo no );
miagenda = anyadir_ entrada ( miagenda , nombre , direccion , telefono );
break ;
194
195
196
197
198
199
200
case Buscar :
printf ( " Nombre : " ); gets ( nombre );
encontrada = buscar_ entrada_ por_ nombre ( miagenda , nombre );
if ( encontrada == NULL )
printf (" No hay nadie llamado %s en la agenda .\ n", nombre );
else
muestra_ entrada ( encontrada );
break ;
201
202
203
204
205
206
207
208
}
} while ( opcion != Salir );
209
210
211
escri be_ agen da ( miagenda , " agenda . txt " );
lib era_ ag enda ( mi age nda );
212
213
214
return 0;
215
216
}
10/10
Oposiciones al profesorado de Informática
Descargar