1 / 12

Diseño de algoritmos “Estructuras”

Diseño de algoritmos “Estructuras”. Claudio Gutiérrez-Soto. Estructuras. Estructuras Anidadas Las estructuras anidadas corresponden a variables de tipo estructura dentro de otras estructuras,. Estructuras. main() { int i; for(i=0;i<N;i++) {

faunia
Download Presentation

Diseño de algoritmos “Estructuras”

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Diseño de algoritmos“Estructuras” Claudio Gutiérrez-Soto.

  2. Estructuras Estructuras Anidadas Las estructuras anidadas corresponden a variables de tipo estructura dentro de otras estructuras,

  3. Estructuras main() { int i; for(i=0;i<N;i++) { printf(“Ingrese el nombre para el empleado %d\n”,i); gets(Empleados[i].DatosPers.Nombre); printf(“Ingrese el apellido para el empleado %d\n”,i); gets(Empleados[i].DatosPers.Apellido); printf(“Ingrese la fecha de nacimiento dd/mm/aa para el empleado %d\n”,i); scanf(%d,%d,%d”,&Empleados[i].DatosPers.FechaNac.dia, &Empleados[i].DatosPers.FechaNac.mes,&Empleados[i].DatosPers.FechaNac.anho ); } } Estructuras Anidadas #include<stdio.h> #define N 20 #define LARGO 30 struct fecha{ int dia; int mes; int anho; }; struct Datos{ char Nombre[LARGO]; char Apellido[LARGO]; struct fecha FechaNac; }; struct Empleado{ struct Datos DatosPers; float sueldo; }Empleados[N];

  4. Estructuras Dada la estructura #include<stdio.h> #define N 20 #define LARGO 30 struct fecha{ int dia; int mes; int anho; }; struct Datos{ char Nombre[LARGO]; char Apellido[LARGO]; stuct fecha FechaNac; }; struct Empleado{ struct Datos DatosPers; float sueldo; }Empleados[N]; Crear una función que muestre el sueldo para una persona, dado el nombre y el apellido

  5. Estructuras Dada la estructura #include<stdio.h> #define N 20 #define LARGO 30 struct fecha{ int dia; int mes; int anho; }; struct Datos{ char Nombre[LARGO]; char Apellido[LARGO]; stuct fecha FechaNac; }; struct Empleado{ struct Datos DatosPers; float sueldo; }Empleados[N]; void Sueldo(char Nombre [],char Apellido[]) { int i; for(i=0;i<N;i++) if(!strcmp(Nombre,Empleados[i].DatosPers.Nombre) && !strcmp(Apellido,Empleados[i].DatosPers.Apellido) ) printf(“%d”,Empleados[i].sueldo); else continue; }

  6. Funciones para cadenas • strcpy(str2,str1): Copia str1 en str2 • strcmp(str1,str2): Compara dos cadenas str1 y str2. Si str1=str2 entonces retorna 0. • strlen(str1): devuelve un entero que va a ser el largo de srt1. • strcat(str1,str2): concatena el string str2 al final del string str1.

  7. Estructuras Dada la estructura #include<stdio.h> #define N 20 #define LARGO 30 struct fecha{ int dia; int mes; int anho; }; struct Datos{ char Nombre[LARGO]; char Apellido[LARGO]; stuct fecha FechaNac; }; struct Empleado{ struct Datos DatosPers; float sueldo; }Empleados[N]; Crear una función que muestre a todas las personas que poseen un sueldo mayor o igual a $700.000.

  8. Estructuras Dada la estructura #include<stdio.h> #define N 20 #define LARGO 30 struct fecha{ int dia; int mes; int anho; }; struct Datos{ char Nombre[LARGO]; char Apellido[LARGO]; stuct fecha FechaNac; }; struct Empleado{ struct Datos DatosPers; float sueldo; }Empleados[N]; void MostrarSueldo() { int i; for(i=0;i<N;i++) if(Empleados[i].sueldo>=700000 ) { printf(“Nombre: %s”,Empleados[i].DatosPers.Nombre); printf(“Apellido: %s”,Empleados[i].DatosPers.Apellido); printf(“Sueldo: %f”,Empleados[i].sueldo); printf(“\n”); } else continue; }

  9. Estructuras Dada la estructura struct Curso{ char CodCurso[3]; char NombreCurso[20]; }Class[10]; // cursos del colegio/ struct Ramos{ char CodRamo[5]; char NombreRamo[30]; char Cod_curso[20]; }Ramos_Curso[50]; /*5 ramos por curso*/ struct Notas{ char Rut[12]; float N1; float N2; float N3; float promedio; char Cod_Ramo[5]; char Cod_Curso[3]; }Notas_Ramo_Curso[500]; struc DatosPersonales{ char rut[12]; char nombre[20]; char Direccion[30]; int telefono; char cod_curso[3]; }ALUMNOS[100]; /* Alumnos del colegio*/ • Crear una función que permita ingresar las notas de un determinado curso y ramo • Crear una función que calcule el promedio para un determinado curso y ramo. • Crear una función que muestre a los alumnos de un curso, para un determinado ramo y que cumplen con una calificación mínima

  10. Estructuras También es posible hacer asignaciones directas de una variable estructura a otra, esto debido a que sus miembros se ubican en posiciones contiguas de memoria.

  11. Estructuras Dada la estructura #include<stdio.h> #define N 20 #define LARGO 30 struct fecha{ int dia; int mes; int anho; }; struct Datos{ char Nombre[LARGO]; char Apellido[LARGO]; stuct fecha FechaNac; }; struct Empleado{ struct Datos DatosPers; float sueldo; }Empleados[N]; void ModificarFecha(struct fecha FechaMod,char Nombre,char Apellido[]) { int i; for(i=0;i<N;i++) if(!strcmp(Empleados[i].DatosPers.Nombre) && !strcmp(Empleados[i].DatosPers.Apellido) ) { Empleados[i].DatosPers.FechaNac=FechaMod; } else continue; }

  12. ¿Preguntas?

More Related