数据结构day2作业

head2.h

#ifndef __HEAD2_H__
#define __HEAD2_H__
#include
#include
#include
typedef union 
{
	int source;
	char pro[20];
}four;

typedef struct 
{
	char name[20];
	char sex;
	char job;
	four s;

}per_t;

per_t* creat_space(int n);
void input(per_t* p,int n);
int num_T(per_t *p,int n);
double num_s(per_t *p,int n);
void output(per_t* p,int n);
per_t* free_space(per_t* p);



#endif

04text.c

#include"head2.h"

int main(int argc, const char *argv[])
{
	int n;
	printf("please input num:");
	scanf("%d",&n);
	per_t* p=creat_space(n);
	input(p,n);
	int teather_num=num_T(p,n);
	printf("teather_num=%d\n",teather_num);
	double student_source=num_s(p,n);
	printf("student_source=%f\n",student_source);
	output(p,n);
	p=free_space(p);
	return 0;
}

044text.c

#include"head2.h"


per_t* creat_space(int n)
{
	per_t *p=(per_t*)malloc(sizeof(per_t)*n);
	if(NULL==p)
		return NULL;
	return p;
}
void input(per_t* p,int n)
{
	int i;
	for(i=0;iname);
		printf("please input %d sex",i+1);
		scanf(" %c",&(p+i)->sex);
		printf("please input %d job s/t",i+1);
		scanf(" %c",&(p+i)->job);
		if((p+i)->job=='s' || (p+i)->job=='S'){
			printf("please input %d source",i+1);
			scanf("%d",&(p+i)->s.source);
		}
		else if((p+i)->job=='t' || (p+i)->job=='T'){
			printf("please input %d pro",i+1);
			scanf("%s",(p+i)->s.pro);
		}
		putchar(10);
	}
}
int num_T(per_t *p,int n)
{
	int i;
	int count=0;
	for(i=0;ijob=='t' || (p+i)->job=='T'){
			count++;
		}
	}
	return count;
}
double num_s(per_t *p,int n)
{
	double sum=0;
	int counts=0;
	int i=0;
	for(i=0;ijob=='s' || (p+i)->job=='S'){
			counts++;
			sum+=(p+i)->s.source;
			
		}

		
	}
	return sum/counts;
}
void output(per_t* p,int n)
{
	int i;
	for(i=0;iname);
		printf("sex=%c\n",(p+i)->sex);
		printf("job=%c\n",(p+i)->job);
		if((p+i)->job=='s' || (p+i)->job=='S'){
			printf("source=%d\n",(p+i)->s.source);
		}
		else if((p+i)->job=='t' || (p+i)->job=='T'){
			printf("pro=%s",(p+i)->s.pro);
		}
	}
}
per_t* free_space(per_t* p)
{
	if(NULL==p)
		return p;
	free(p);
	p=NULL;
	return p;
}

结果:

please input num:3
please input 1 namea
please input 1 sexm
please input 1 job s/ts
please input 1 source90

please input 2 nameb
please input 2 sexm
please input 2 job s/ts
please input 2 source80

please input 3 namec
please input 3 sexm
please input 3 job s/tt
please input 3 projia

teather_num=1
student_source=85.000000
name=a
sex=m
job=s
source=90
name=b
sex=m
job=s
source=80
name=c
sex=m
job=t
pro=jia

第一个:

数据结构day2作业_第1张图片

第二个:

数据结构day2作业_第2张图片

第三个:

数据结构day2作业_第3张图片

你可能感兴趣的:(数据结构)