PROGRAMACION II scanf("%f",&emp[e].
salario);
if(emp[e].salario>mayor) {
DOC. JAVIER DE LA CRUZ
mayor=emp[e].salario;
EJERCICIOS RESUELTOS DE STRUCT C++
pmay=e;
}
1). La informacin de todos los empleados de
la empresa DATASYSTEM est almacenada if(emp[e].salario<menor) {
en una variable de tipo struct llamada
menor=emp[e].salario;
empleado. La informacin con que se
cuenta de cada empleado es: nombre, sexo y pmen=e;
sueldo.
}
Por tanto se pide: Realizar un programa en C
que lea en un array de estructuras los datos }
de los N trabajadores de la empresa y que printf("\n\n%s es el trabajador con MAYOR
imprima los datos del empleado con mayor y salario\n",emp[pmay].nombre);
menor salario.
printf("%s es el trabajador con MENOR
Solucin: salario\n",emp[pmen].nombre);
#include<stdio.h> struct empleado }
{
char nombre[30]; char sexo[12]; float salario; 2). A partir de la declaracin de las siguientes
}; estructuras realice un programa en C que lea
el array ats y devuelva los datos (nombre,
void main() { pas, deporte) del atleta que ha ganado
mayor nmero de medallas.
struct empleado emp[100];
int e,nemp,pmay,pmen;
struct datos{
float mayor=0.0, menor=9999.0;
printf("Introduzca el numero de empleados: char nombre[40]; char pais[25];
");
};
scanf("%d",&nemp);
struct atleta{
for(e=0;e<nemp;e++) {
char deporte[30]; struct datos pers; int
printf("\n"); printf("Nombre[%d]: ",e+1); nmedallas;
scanf("%s",emp[e].nombre); };
printf("Sexo[%d]: ",e+1); struct atleta ats[30];
scanf("%s",emp[e].sexo); Solucin:
printf("Salario[%d]: ",e+1); //atletas.c
#include<stdio.h> printf("PAIS: %s\n",ats[posmay].pers.pais);
printf("DEPORTE:
struct datos{
%s\n",ats[posmay].deporte);
char nombre[40]; char pais[25];
printf("NUMERO DE MEDALLAS:
}; %d\n",ats[posmay].nmedallas);
struct atleta{ }
char deporte[30]; int nmedallas; struct datos
pers;
*************************************
};
Nota: El siguiente ejercicio es un ejemplo de
//Array de Estructuras struct atleta ats[3]; un registro de pacientes, por favor hacer el
seguimiento paso a paso para que puedan
void main(){ analizar que es lo que en el se realiza en
int a,posmay,maynmed=0; pocas palabras una prueba de escritorio.
for(a=0;a<3;a++) { *************************************
printf("\n"); printf("Nombre[%d]: ",a+1); 3). Ejercicios con Listas Enlazadas Simples en
C++: Sistema sobre registro de Pacientes de
scanf("%s",ats[a].pers.nombre); un Hospital que cumpla con un
printf("Pais[%d]: ",a+1); menu(registrar, modificar, eliminar
pacientes,...)
scanf("%s",ats[a].pers.pais);
printf("Deporte[%d]: ",a+1);
#include<iostream>
scanf("%s",ats[a].deporte);
#include<cstdlib>
printf("Numero de Medallas[%d]: ",a+1);
scanf("%d",&ats[a].nmedallas); #include<string.h>
} #define maxchar 50
//nmed=ats[0].nmedallas; for(a=1;a<3;a++) using namespace std;
{ struct nodo{
if(ats[a].nmedallas>maynmed) { int codigo; // campo codigo
posmay=a; maynmed=ats[a].nmedallas; char nombres[maxchar]; // campo que
almacena el nombre
}
char apellidos[maxchar];// campo que
} almacena el apellido
printf("\n\t****DATOS DEL ATLETA CON char direccion[maxchar];// campo que
MAYOR NUMERO DE MEDALLAS****\n"); almacena la direccion
printf("NOMBRE:
%s\n",ats[posmay].pers.nombre);
int telefono;// campo que almacena el cout<<" 2. APELLIDOS "<<endl;
telefono
cout<<" 3. DIRECCION "<<endl;
struct nodo *sgte;
cout<<" 4. TELEFONO "<<endl;
};
cout<<" 5. SALIR "<<endl;
typedef struct nodo *PLista;
/*--------------------- FUNCION MENU
cout<<"\n Ingrese opcion : ";
PRINCIPAL --------------------*/
}
void menu(void){
cout<<"\n\t\t[ REGISTRO DE PACIENTES
]\n";
cout<<"\t\t----------------------------\n\n"; /*-------------------- FUNCION REGISTRAR
PACIENTES ------------------*/
cout<<" 1. REGISTRAR PACIENTE
"<<endl; void registrar_pacientes(PLista &lista){
cout<<" 2. ELIMINAR DATOS DE PACIENTE PLista t,q = new(struct nodo);
"<<endl;
cout<<"\n\n\t\t[ REGISTRO ]\n";
cout<<" 3. ACTUALIZAR PACIENTES
"<<endl; cout<<"\t\t------------";
cout<<" 4. MOSTRAR LISTADO cout<<"\n\tDATOS DEL PACIENTE ";
"<<endl; cin.ignore();
cout<<" 5. COPIAR DATOS DE UN cout<<"\n\n\tCODIGO:";
PACIENTE A OTRO "<<endl;
cin>>q->codigo;
cout<<" 6. SALIR
"<<endl; cin.ignore();
cout<<"\n Ingrese opcion : "; cout<<"\n\tNOMBRES:";
} cin.getline(q->nombres,maxchar);
/*----------------- CUNCION PARA ACTUALIZAR cin.ignore();
UN DATO---------------*/ cout<<"\tAPELLIDOS:";
void menu_actualizar(void){ cin.getline(q->apellidos,maxchar);
cin.ignore();
cout<<"\n\t\t[ ESPECIFIQUE CAMPO A cout<<"\tDIRECCION:";
ACTUALIZAR ]\n";
cin.getline(q->direccion,maxchar);
cout<<"\t\t----------------------------\n\n";
cin.ignore();
cout<<" 1. NOMBRES "<<endl;
cout<<"\n\tTELEFONO:"; delete(q);
cin>>q->telefono; cout<<"\n\n\tREGISTRO
ELIMINADO...!!!!!\n";
system("cls");
return;
q->sgte = NULL;
}else {
if(lista==NULL){
t=q;
lista = q;
q=q->sgte;
} else {
}
t = lista;
while(t->sgte!=NULL){
}
t = t->sgte;
if(q==NULL)
}
cout<<"\n\tCODIGO
t->sgte = q;
INCORRECTO...!!\n";
}
}
}
/*-------------------- FUNCION ACTUALIZAR
PACIENTE -------------------*/
/*------------------------ FUNCION ELIMINAR void actualizar_paciente(PLista lista){
PACIENTE ---------------------*/
int cod, x;
void eliminar_paciente(PLista &lista){
PLista q;
int cod;
q=lista;
PLista q,t;
cout<<"\n\n\n\tACTUALIZAR REGISTRO DE
q=lista; PACIENTE";
cout<<"\n\n\n\tELIMINAR UN PACIENTE"; cout<<"\n\n\tINGRESE CODIGO:";
cin>>cod;
cout<<"\n\n\tINGRESE CODIGO:";
cin>>cod; while(q!=NULL){
while(q!=NULL){ if(q->codigo==cod){
if(q->codigo==cod){ system("cls");
if(q==lista) cout<<"\n\tDATOS DEL PACIENTE ";
lista=lista->sgte; cout<<"\n\t--------------------";
else cout<<"\n\n\tCODIGO : "<<q-
>codigo<<endl;
t->sgte=q->sgte;
cout<<"\n\tNOMBRES : "<<q- default: cout<<"\nINGRESE UNA
>nombres<<endl; OPCION VALIDA...\n"; break;
cout<<"\tAPELLIDOS: "<<q- }
>apellidos<<endl;
cout<<"\n\n\tREGISTRO
cout<<"\tDIRECCION: "<<q- ACTUALIZADO...!!!!!\n";
>direccion<<endl;
return;
cout<<"\tTELEFONO : "<<q-
}else {
>telefono<<endl;
q=q->sgte;
menu_actualizar();
}
cin>>x;
switch(x){
}
case 1: cout<<"\n\n\tINGRESE
NOMBRES:"; if(q==NULL)
cin.ignore(); cin.getline(q- cout<<"\n\tCODIGO
>nombres,maxchar); INCORRECTO...!!\n";
break; }
case 2: cout<<"\n\n\tINGRESE /*---------------------- FUNCION MOSTRAR
APELLIDOS:"; PACIENTE -------------------*/
cin.ignore(); cin.getline(q- void mostrar_pacientes(PLista q){
>apellidos,maxchar);
int i=1;
break;
while(q!=NULL){
case 3: cout<<"\n\n\tINGRESE
DIRECCION:"; cout<<"\n\tDATOS DEL PACIENTE
["<<i<<"] ";
cin.ignore(); cin.getline(q-
>direccion,maxchar); cout<<"\n\t------------------------";
break; cout<<"\n\n\tCODIGO : "<<q-
>codigo<<endl;
cout<<"\n\tNOMBRES : "<<q-
case 4: cout<<"\n\n\tINGRESE >nombres<<endl;
TELEFONO:";
cout<<"\tAPELLIDOS: "<<q-
cin>>q->telefono; >apellidos<<endl;
break; cout<<"\tDIRECCION: "<<q-
>direccion<<endl;
cout<<"\tTELEFONO : "<<q- cout<<"\tAPELLIDOS: "<<p-
>telefono<<endl; >apellidos<<endl;
q=q->sgte; cout<<"\tDIRECCION: "<<p-
>direccion<<endl;
i++;
cout<<"\tTELEFONO : "<<p-
}
>telefono<<endl;
}
}
/*--------------- FUNCION COPIAR DATOS DE
p=p->sgte;
UN PACIENTEA OTRO ------------------*/
}
void copiar_datos_paciente(PLista lista){
while(q!=NULL){
int cod1,cod2, x;
if(q->codigo==cod2){
PLista p,q,t;
p=lista;
cout<<"\n\tDATOS DEL PACIENTE A
q=lista;
SUSITUIR ";
char dato[maxchar];
cout<<"\n\t--------------------";
cout<<"\n\n\n\tCOPIAR DATOS DE
cout<<"\n\n\tCODIGO : "<<q-
PACIENTE A OTRO";
>codigo<<endl;
cout<<"\n\n\n\t--------------------------------";
cout<<"\n\tNOMBRES : "<<q-
cout<<"\n\n\tINGRESE CODIGO DE >nombres<<endl;
PACIENTE A COPIAR:"; cin>>cod1;
cout<<"\tAPELLIDOS: "<<q-
cout<<"\n\n\tINGRESE CODIGO DE >apellidos<<endl;
PACIENTE A SUSTITUIR:";cin>>cod2;
cout<<"\tDIRECCION: "<<q-
system("cls"); >direccion<<endl;
while(p!=NULL){ cout<<"\tTELEFONO : "<<q-
>telefono<<endl;
if(p->codigo==cod1){
menu_actualizar();
t=p;
cin>>x;
cout<<"\n\tDATOS DEL PACIENTE
COPIAR "; switch(x){
cout<<"\n\t---------------------------"; case 1: strcpy(dato,t->nombres);
cout<<"\n\n\tCODIGO : "<<p- strcpy(q->nombres,dato);
>codigo<<endl;
break;
cout<<"\n\tNOMBRES : "<<p-
case 2: strcpy(dato,t->apellidos);
>nombres<<endl;
strcpy(q->apellidos,dato); cin>>opcion;
break; switch(opcion){
case 3: strcpy(dato,t->direccion); case 1: registrar_pacientes(lista);
strcpy(q->direccion,dato); break;
break; case 2: if(lista==NULL){
case 4: q->telefono=t->telefono; cout<"\n\tNo Hay Pacientes
Registrados.....!!!!\n";
break;
}else{
default: cout<<"\nINGRESE UNA
OPCION VALIDA...\n"; break; eliminar_paciente(lista);
} break;
cout<<"\n\n\tREGISTRO case 3: if(lista==NULL){
ACTUALIZADO...!!!!!\n";
cout<"\n\tNo Hay Pacientes
return; Registrados.....!!!!\n";
}else { }else{
q=q->sgte; actualizar_paciente(lista);
} }
} break;
if(q==NULL) case 4: if(lista==NULL){
cout<<"\n\tCODIGO cout<"\n\tNo Hay Pacientes
INCORRECTO...!!\n"; Registrados.....!!!!\n";
} }else {
/*------------------------- FUNCION PRINCIPAL --- mostrar_pacientes(lista);
----------------*/
}
int main(void){
break;
system("color 0a");
case 5: if(lista==NULL){
PLista lista=NULL;
cout<"\n\tNo Hay Pacientes
int opcion; Registrados.....!!!!\n";
do{ }else{
menu(); copiar_datos_paciente(lista);
}
break;
case 6: return 0;
default: cout<<"\nINGRESE UNA
OPCION VALIDA...\n"; break;
system("pause"); system("cls");
}while(opcion!=6);
system("pause");
return 0;