head2.h

#ifndef __HEAD2_H__
#define __HEAD2_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h&gt;
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",&amp;n);
	per_t* p=creat_space(n);
	input(p,n);
	int teather_num=num_T(p,n);
	printf("teather_num=%dn",teather_num);
	double student_source=num_s(p,n);
	printf("student_source=%fn",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;i<n;i++){
		printf("please input %d name",i+1);
		scanf("%s",(p+i)-&gt;name);
		printf("please input %d sex",i+1);
		scanf(" %c",&amp;(p+i)-&gt;sex);
		printf("please input %d job s/t",i+1);
		scanf(" %c",&amp;(p+i)-&gt;job);
		if((p+i)-&gt;job=='s' || (p+i)->job=='S'){
			printf("please input %d source",i+1);
			scanf("%d",&amp;(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;i<n;i++){
		if((p+i)->job=='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;i<n;i++){
		if((p+i)->job=='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;i<n;i++){
		printf("name=%sn",(p+i)->name);
		printf("sex=%cn",(p+i)->sex);
		printf("job=%cn",(p+i)->job);
		if((p+i)->job=='s' || (p+i)->job=='S'){
			printf("source=%dn",(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

一个:

第二个:

第三个:

原文地址:https://blog.csdn.net/Djdbds/article/details/134561306

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_11999.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注