#ifndef __HEAD2_H__
#define __HEAD2_H__
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
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=%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)->name);
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;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进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。