#include <stdio.h>
#include <stdlib.h>
typedef struct
{
char name[20];
char sex;
char job;
union
{
float score;
char position[20];
};
}p,*q;
q createe(int n)
{
q xx=(p*)malloc(sizeof(p)*5);
if(xx==NULL)
return NULL;
else
return xx;
}
void input(q xx,int n)
{
int i;
for(i=0;i<n;i++)
{
puts("please input the name\n");
scanf("%s",(xx+i)->name);
puts("please input the sex(f-boy;m-girl)\n");
scanf(" %c",&((xx+i)->sex));
loop:
puts("please input the job(s-student;t-teacher\n");
scanf(" %c",&((xx+i)->job));
if('s'==(xx+i)->job || 'S'==(xx+i)->job)
{
puts("please input the score\n");
scanf("%f",&((xx+i)->score));
}
else if('t'==(xx+i)->job || 'T'==(xx+i)->job)
{
puts("please input the position\n");
scanf("%s",(xx+i)->position);
}
else
goto loop ;
}}
void output(q xx,int n)
{
int i;
for(i=0;i<n;i++)
{
if('t'==(xx+i)->job || 'T'==(xx+i)->job)
printf("%s\t%c\t%c\t%s\t\n",(xx+i)->name,(xx+i)->sex,(xx+i)->job,(xx+i)->position);
else
printf("%s\t%c\t%c\t%.2f\t\n",(xx+i)->name,(xx+i)->sex,(xx+i)->job,(xx+i)->score);
}}
float aver(q ss,int n)
{
float i=0.00;
int j=0;
int x;
for(x=0;x<n;x++)
{
if((ss+x)->job=='s' ||(ss+x)->job=='S')
{
i=i+((ss+x)->score);
j++;
}
}
return i/j;
}
int numbteach(q ss,int n)
{
int i=0;
int x;
for(x=0;x<n;x++)
{
if((ss+x)->job=='T' ||(ss+x)->job=='t')
{
i++;
}
}
return i;
}
int main()
{
float ave;
int numbt;
q arr=createe(5);
input(arr,5);
output(arr,5);
ave=aver(arr,5);
printf("the average is %.2f\n",ave);
numbt=numbteach(arr,5);
printf("teacher total %d\n",numbt);
free(arr);
arr=NULL;
return 0;
}