Download 17_Estructuras_Copiar estructuras_17

Document related concepts
no text concepts found
Transcript
Temas:
1. Estructuras
Lenguage
Tipos estructurales: struct .
Ejemplo: definición del tipo estructural 'ficha':
struct ficha{
int edad;
float peso;
char nombre[40];
};
Operador typedef:
typedef struct ficha tficha; //tficha es un sinónimo de 'struct ficha'
typedef int mi_entero;
//el tipo 'mi_entero' es sinonimo de 'int'
mi_entero y=2;
//variable 'y' tiene tipo 'int' (implicitamente)
Uso combinado de la declaración de un tipo estructural con typedef:
/* Definición de nombre para un tipo:
En el siguiente ejemplo, lo que se encuentra
entre 'typedef' y tficha; es la explicación del
significado de 'qwe' para su futuro uso.
Entonces 'qwe' es un sinónimo del tipo 'struct
otraficha': */
typedef struct otraficha{
float zanaoiria;
int chilis;
float nopal;
} qwe;
//qwe es tipo estructural equivalente a
//struct otraficha
qwe unplatillo;
//declarar variable tipo qwe
qwe variasrecetas[100]; //declarar arreglo de 100 elementos
de tipo qwe
Otros ejemplos de uso del tipo 'ficha':
Declaración de variables: ficha mi_ficha, tu_ficha,
*pficha;/* pficha es apuntador a un
objeto tipo 'ficha' */
Acceso a campos:
mi_ficha.edad=125;//acceso mediante '.'
tu_ficha.edad= mi_ficha.edad – 99;
pficha=&mi_ficha;
pficha->peso=78.;/*!!! para las variables
apuntadas acceso a los campos se
hace mediante '->' */
tu_ficha.peso=pficha->peso+20.;
Operador typedef :
typedef struct ficha{
int edad;
float peso;
char nombre[40];
}tficha;
Ejemplos:
1. Ejemplo de código (clase 23/11/16)
#include "stdafx.h"
#define K 120
char letra(){
char alfabeto[] = { 'a', 'b', 'c', 'd', 'e',
' ' };
return alfabeto[rand() % 6];
}
//void
//
//
//
//
gen_cad(char c[], int dim){
int fin=rand()%(dim-1);
int i;
for(i=0;i<fin;i++)
c[i]=letra();
//
//}
c[fin]='\0';
void gen_cad(char *c, int dim){
int fin = rand() % (dim - 1);
int i;
for (i = 0; i<fin; i++)
*(c + i) = letra();
*(c + fin) = '\0';
}
//struct ficha{
//
int edad;
//
float peso;
//
char nombre[40];
//};
/*juntar definición de struct ficha con 'typedef':*/
typedef
struct ficha{
int edad;
float peso;
char nombre[40];
}
tficha;
/*definir 'entero' como sinonimo de 'int'*/
typedef int entero;
void gen_tficha(tficha * tf){
tf->edad = rand() % 150;
tf->peso = 1 + rand() % 500;
gen_cad(tf->nombre, 39);
}
void copy_tficha(tficha *origen, tficha *destino){
int sz = sizeof(tficha), i; //sizeof(*origen);
char *f = (char*)origen, *f1 = (char*)destino;
for (i = 0; i<sz; i++, f++, f1++)
*f1 = *f;
}
int main()
{
entero qwer;
struct ficha mif;
tficha mif1, batallon[100];
mif.edad = 15;
//mif.nombre[0]='a';
//mif.nombre[1]='\0';
gen_cad(mif.nombre/*char *c*/, 39/*int dim*/);
puts(mif.nombre);
mif.peso = 60.f;
int i, j, *pi; //<-- declaracion de variables enteras
'i', j
// y variable apuntador 'pi' que puede apuntar
// a una variable entera
/*generar arreglo de estructuras tipo 'tficha'*/
for (i = 0; i<100; i++)
gen_tficha(&batallon[i]);
return 0;
}
2.
Ejemplo de código (clase 14/03/16):
#include "stdafx.h"
/*opcion de declarar tipo struct ficha
(comentada) */
//
//struct ficha{
// int edad;
// float peso;
// char nombre[40];
//};
/*opcion de combinar declaración del
tipo struct ficha con definicion de su
sinonimo (alias) 'tficha' */
typedef struct ficha{
int edad;
float peso;
char nombre[40];
} tficha;
typedef long mi_entero;
char genletra2(){
return 'a' + rand() % 26;
}
int main()
{
mi_entero ii;
//ficha unregistro;
tficha unregistro;
unregistro.edad = 65;
unregistro.peso = 80.f;
/*la opcion de generar campo 'nombre'*/
int lon = rand() % 30, i;
for (i = 0; i<lon; i++)
unregistro.nombre[i] = genletra2();
unregistro.nombre[i] = '\0';
puts(" nombre generado es:");
puts(unregistro.nombre);
/*la opcion de introducir campo 'nombre' del usuario*/
//puts("dame nombre");
//gets(unregistro.nombre);
//puts(" nombre introducido es:");
//puts(unregistro.nombre);
return 0;
}
Algoritmos: ¿Cómo copiar una variable estructural a otra?
Implementar algoritmo para copia de objetos estructurales mediante función 'sizeof()'.
Objetivos: comprender mecanismo de copia de objetos estructurales basado en
"borrar" el factor estructural de los datos para la operación de copia.
Una implementación basada en uso de 'sizeof':
void copy_tficha(tficha *origen, tficha *destino){
int sz = sizeof(tficha), i; //sizeof(*origen);
char *f = (char*)origen, *f1 = (char*)destino;
for (i = 0; i<sz; i++, f++, f1++)
*f1 = *f;
}