
Brian O.
asked 06/01/21Programming in c
En un hospital se registran pacientes, por cada paciente se registra: código del paciente (número entero), edad,
genero, tipo de consulta (1, 2 ó 3) y días que se encuentra internado. Se finaliza el ingreso de datos cuando se
ingresa un producto cuyo código sea negativo o cero.
Debe mostrar a través de un menú:
1. Cantidad de pacientes internados
2. Cantidad de pacientes cuyo código sea 799
3. Promedio de días que llevan internado entre los pacientes cuya edad sea mayor de 55 años
4. Edad mayor entre los pacientes que son atendidos entre consultas tipo 2 y 4
5. Edad menor entre los pacientes que tengan más de 3 días internados
6. Salir
1 Expert Answer

Patrick B. answered 06/01/21
Math and computer tutor/teacher
#include
#include
#include
typedef struct _TPatient
{
int patientCode;
int age;
int gender;
int consultationType;
int numDays;
} * TPatient;
#define PATIENT_SIZE (sizeof(struct _TPatient))
#define MAX_PATIENTS (20000)
int PatientParse(TPatient patient, char * patientRecBuff)
{
int iReturn=0;
patient->patientCode = atoi(strtok(patientRecBuff,","));
//Data Entry Input is Complete when patient code is ZER0 or negative
if (patient->patientCode>0)
{
patient->age = atoi(strtok(NULL,","));
patient->gender = atoi(strtok(NULL,","));
patient->consultationType = atoi(strtok(NULL,","));
patient->numDays = atoi(strtok(NULL,","));
}
else
{
iReturn = -1;
}
return(iReturn);
}
PatientDump(TPatient patient, char * strMsg)
{
if (strMsg!=NULL)
{
printf("**********************************************\n");
printf(strMsg); printf("\n");
}
printf("**************************************************\n");
printf(" patient code : %d \n",patient->patientCode);
printf(" patient age : %d \n",patient->age);
printf(" patient gender : %d \n",patient->gender);
printf(" patient consultation type : %d \n",patient->consultationType);
printf(" patient # of days : %d \n",patient->numDays);
printf("\n");
}
int PatientRead(TPatient A, int * numPatients)
{
int iReturn=0;
int iIndexPos=0;
FILE * fptr = fopen("E:\\patients.dat","r");
char inbuff[255];
char strMsg[25];
struct _TPatient curPatientRec;
int iReadReturn=0;
if (fptr!=NULL)
{
iIndexPos=0;
while (!feof(fptr) && (iIndexPos
{
fgets(inbuff,255,fptr);
iReadReturn = PatientParse(&curPatientRec,inbuff);
if (iReadReturn==0)
{
memcpy(&A[iIndexPos++],&curPatientRec,PATIENT_SIZE);
sprintf(strMsg,"patient rec # %d",iIndexPos);
PatientDump(&A[iIndexPos-1],strMsg);
}
else
{
break;
}
} //while
*numPatients = iIndexPos;
}
else
{
printf(" Error opening input file \n");
iReturn =-1;
}
return(iReturn);
}
int Menu()
{
int iReturn;
do
{
printf("*******************************\n");
printf(" <1> # of patients \n");
printf(" <2> # with code 799 \n");
printf(" <3> Avg # days over age 55 \n");
printf(" <4> Type 2 and 4 max ages \n");
printf(" <5> Min age over 3 days \n");
printf(" <6> EXIT/QUIT \n");
printf("********************************\n");
printf(" Input selection :>");
scanf("%d",&iReturn);
}
while (iReturn<1 || iReturn>6);
return(iReturn);
}
int Count799(TPatient A, int n)
{
int iCount=0;
int iLoop=0;
for (iLoop=0; iLoop
{
if (A[iLoop].patientCode==799)
{
iCount++;
}
}
return(iCount);
}
double Query3(TPatient A, int n)
{
int iTotalNumDays=0;
int iLoop=0;
int iCount=0;
double avgNumDaysReturn=0;
for (iLoop=0; iLoop
{
if (A[iLoop].age>55)
{
iTotalNumDays += A[iLoop].numDays;
iCount++;
}
}
if (iCount>0)
{
avgNumDaysReturn = iTotalNumDays*1.0f/iCount;
}
return(avgNumDaysReturn);
}
int Query4(TPatient A, int n)
{
int iLoop=0;
int maxAge=-1;
int curAge;
int consultationType;
for (iLoop=0; iLoop
{
consultationType = A[iLoop].consultationType;
curAge = A[iLoop].age;
if (consultationType==2 || consultationType==4)
{
if (curAge>maxAge)
{
maxAge = curAge;
}
}
}
return(maxAge);
}
int Query5(TPatient A, int n)
{
int iLoop=0;
int minAge=32767;
int curAge;
int curNumDays;
for (iLoop=0; iLoop
{
curNumDays = A[iLoop].numDays;
curAge = A[iLoop].age;
if (curNumDays>3)
{
if (curAge
{
minAge = curAge;
}
}
}
return(minAge);
}
Go()
{
struct _TPatient A[MAX_PATIENTS];
int NumPatients;
int iMenuChoice=-1;
if (PatientRead(A,&NumPatients)==0)
{
while (iMenuChoice != 6)
{
iMenuChoice = Menu();
switch (iMenuChoice)
{
case 1:
{
printf(" # of patients: %d \n",NumPatients);
break;
}
case 2:
{
printf(" # of patients with code 799: %d \n",Count799(A,NumPatients));
break;
}
case 3:
{
printf(" %8.4lf \n",Query3(A,NumPatients));
break;
}
case 4:
{
printf("%d \n",Query4(A,NumPatients));
break;
}
case 5:
{
printf("%d \n",Query5(A,NumPatients));
break;
}
}
} //while
}
}
main() { Go(); }
/****************************************************************/
/*** INPUT FILE ***********/
333,51,1,3,3
216,51,1,3,3
711,43,0,2,5
469,39,0,1,4
534,69,1,2,3
810,77,0,1,2
159,24,0,3,1
799,12,1,1,25
643,31,0,2,2
928,59,2,4,3
-1
Still looking for help? Get the right answer, fast.
Get a free answer to a quick problem.
Most questions answered within 4 hours.
OR
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.
Patrick B.
Por favor, no me gusta ingresar los datos cada vez que comienza el programa. Es más fácil dejar que lea los datos del archivo.06/01/21