结构体总结一

//找出指定编号人员的数据,作为函数值返回,若指定编号不存在,返回编号为空串
#include    <stdio.h>
#include    <string.h>
#define    N    8
typedef  struct
{  char  num[10];
   int  year,month,day ;
}STU;
/**********found**********/
STU fun(STU  *std, char  *num)//变量a可以看出,函数为STU类型
{  int  i;       STU  a={"",9999,99,99};
   for (i=0; i<N; i++)
/**********found**********/
      if( strcmp(std[i].num,num)==0 )//指定编号调用std[i].num
/**********found**********/
           return (std[i]);//编号对应数据
   return  a;
}
void main()
{  STU  std[N]={ {"111111",1984,2,15},{"222222",1983,9,21},{"333333",1984,9,1},
                 {"444444",1983,7,15},{"555555",1984,9,28},{"666666",1983,11,15},
                 {"777777",1983,6,22},{"888888",1984,8,19}};
   STU  p;         char  n[10]="666666";
   p=fun(std,n);
   if(p.num[0]==0)
      printf("\nNot found !\n");
   else
   {   printf("\nSucceed !\n  ");
       printf("%s   %d-%d-%d\n",p.num,p.year,p.month,p.day);
   }
}
//找出指定出生日期年份的人员
#include    <stdio.h>
#define    N    8
typedef  struct
{  int  num;
   int  year,month,day ;
}STU;
int fun(STU  *std, STU  *k, int  year)
{  int  i,n=0;
   for (i=0; i<N; i++)
/**********found**********/
      if( std[i].year==year)//结构体变量中的年份与指定year是否相等
/**********found**********/
         k[n++]= std[i];//符合条件的存入k中
/**********found**********/
   return (n);//满足条件的人数
}
void main()
{  STU  std[N]={ {1,1984,2,15},{2,1983,9,21},{3,1984,9,1},{4,1983,7,15},
                 {5,1985,9,28},{6,1982,11,15},{7,1982,6,22},{8,1984,8,19}};
   STU  k[N];         int  i,n,year;
   printf("Enter a year :  ");  scanf("%d",&year);
   n=fun(std,k,year);
   if(n==0)
      printf("\nNo person was born in %d \n",year);
   else
   {   printf("\nThese persons were born in %d \n",year);
       for(i=0; i<n; i++)
         printf("%d  %d-%d-%d\n",k[i].num,k[i].year,k[i].month,k[i].day);
   }
}

 

//找出成绩最高的学生记录
#include<stdio.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#define N 10
typedef struct ss    /*定义结构体*/
{ char num[10]; 
  int s;
} STU;
void fun(STU a[], STU *s)
{
	int i;
	*s = a[0];
	for (i = 0; i < N; i++)
		if (s->s < a[i].s)//s->s等价于(*s).s
			*s = a[i];
}
void main()
{ 
  FILE *wf;
  STU a[N]={{ "A01",81},{ "A02",89},{ "A03",66},{ "A04",87},{ "A05",77},
  { "A06",90},{ "A07",79},{ "A08",61},{ "A09",80},{ "A10",71}},m;
  int i;
  system("CLS");
  printf("*****The original data*****");
  for(i=0;i<N;i++) 
     printf("No=%s Mark=%d\n", a[i].num,a[i].s);
  fun(a,&m);
  printf("*****THE RESULT*****\n");
  printf("The top :%s, %d\n",m.num,m.s);
/******************************/
  wf=fopen("out.dat","w");
  fprintf(wf,"%s,%d",m.num,m.s);
  fclose(wf);
/*****************************/
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值