计算机二级题目之结构体与共用体练习学习

#include    <stdio.h>
#include    <stdlib.h>
#define    N    8
typedef  struct list
{  int  data;
   struct list  *next;
} SLIST;
void fun( SLIST  *h, int  x)
{  SLIST  *p, *q, *s;
   s=(SLIST *)malloc(sizeof(SLIST));
/**********found**********/
   s->data=x;
   q=h;
   p=h->next;
   while(p!=NULL && x>p->data) {
/**********found**********/
      q=___2___;
      p=p->next;
   }
   s->next=p;
/**********found**********/
   q->next=s;
}
SLIST *creatlist(int  *a)
{  SLIST  *h,*p,*q;      int  i;
   h=p=(SLIST *)malloc(sizeof(SLIST));
   for(i=0; i<N; i++)
   {  q=(SLIST *)malloc(sizeof(SLIST));
      q->data=a[i];  p->next=q;  p=q;
   }
   p->next=0;
   return  h;
}
void outlist(SLIST  *h)
{  SLIST  *p;
   p=h->next;
   if (p==NULL)  printf("\nThe list is NULL!\n");
   else
   {   printf("\nHead");
       do { printf("->%d",p->data);  p=p->next;  } while(p!=NULL);
       printf("->End\n");
   }
}
main()
{  SLIST  *head;      int  x;
   int  a[N]={11,12,15,18,19,22,25,29};
   head=creatlist(a);
   printf("\nThe list before inserting:\n");  outlist(head);
   printf("\nEnter a number :  ");  scanf("%d",&x);
   fun(head,x);
   printf("\nThe list after inserting:\n");  outlist(head);
}

#include <stdio.h>
#include <string.h>
#define  N  10
typedef  struct  ss
{  char  num[10];   int  s;  } STU;
fun(STU a[], STU *s)
{
/**************found**************/
   STU  h;  int   i ;
   h = a[0];
   for ( i = 1;  i< N; i++ )
/**************found**************/
     if ( a[i].s < h.s )  h = a[i];
/**************found**************/
   *s = h ;
}

main()
{  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;
   printf("***** The original data *****\n");
   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 lowest  :  %s , %d\n",m.num, m.s);
}

 

 

#include <stdio.h>

struct stu_list
{
  int num;
  char name[20];
  char sex;
  float score;
}s[5]={{9801,"Li Jie",'M',56.5},
       {9802,"Yang YiHang",'M',83.5},
       {9803,"Zhang Ping",'F',90.5},
       {9804,"Yuan Rui",'F',93},
       {9805,"Li Yanli",'F',56}
      };

main()
{
  int i,c=0;
  float ave,sum=0;
  for (i=0;i<5;i++)
  {
/***************found***************/  
    sum+=s[i].score;
/***************found***************/
    if (s[i].score<60) c+=1;
  }
  printf("sum=%f \n",sum);
/***************found***************/
  ave=sum/5;
  printf("average = %f \ncount= %d \n",ave,c);
  return;
  }      

 

#include <stdio.h>
#define N 5
typedef struct
  { char name[20];
    char num[10];
  }USER;
/***************found***************/
getdata(USER *sp)
{ int i;
  printf("Enter name & phone number:\n");
  for(i=0;i<N;i++)
  {printf("i:= %d\n",i+1);
/***************found***************/  
   printf("  name="); gets(sp[i].name);
/***************found***************/   
   printf("  num=");  gets(sp[i].num);}
}

getsort (USER *sp)
{ int i,j,k;
  USER temp;
  for(i=0;i<N-1;i++)
  { k=i;
    for(j=i+1;j<N;j++)
       if (strcmp(sp[k].name,sp[j].name)>0) k=j;
    temp=sp[k];sp[k]=sp[i];sp[i]=temp;
  }
}

outdata(USER *sp)
{ int i;
  printf("after sorted:\n");
  for (i=0;i<N;i++)
    printf("%s, %s\n",sp[i].name,sp[i].num);
}

main()
{
  USER sp[N],temp;
  getdata(sp);
  getsort(sp);
  outdata(sp);
}

      

 

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
typedef  struct  aa
{  int  data;  struct  aa  *next; }NODE;
int  fun(NODE  *h)
{ int   sum = 0 ;
  NODE  *p;
/***********found**********/
  p=h;
  while(p)
  {  if(p->data%2==0)
     sum +=p->data;
/***********found**********/
     p=h->next;
  }
  return  sum;
}
NODE  *creatlink(int  n)
{  NODE  *h, *p, *s, *q;
   int  i, x;
   h=p=(NODE *)malloc(sizeof(NODE));
   for(i=1; i<=n; i++)
   {  s=(NODE *)malloc(sizeof(NODE));
      s->data=rand()%16;
      s->next=p->next;
      p->next=s;
      p=p->next;
   }
   p->next=NULL;
   return  h;
}
outlink(NODE  *h, FILE  *pf)
{  NODE *p;
   p = h->next;
   fprintf(pf ,"\n\nTHE  LIST :\n\n  HEAD " );
   while(p)
   {  fprintf(pf ,"->%d ",p->data ); p=p->next; }
      fprintf (pf,"\n");
}
outresult(int  s, FILE *pf)
{  fprintf(pf,"\nThe sum of even numbers  :  %d\n",s);}
main()
{  NODE  *head;    int  even;

   head=creatlink(12);
   head->data=9000;
   outlink(head , stdout);
   even=fun(head);
   printf("\nThe  result  :\n"); outresult(even, stdout);
}

 

 

#include <stdio.h>
#include <string.h>
#include <conio.h>
#define  N  10
typedef  struct  ss
{  char  num[10];   int  s;  } STU;

fun(  STU  a[], STU  *s  )
{
	int j;
	STU bs;
	bs=a[0];
	for(j=1;j<N;j++)
	{	
		if(bs.s>a[j].s)
		bs=a[j];
	}
	*s=bs;


}
NONO( )
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
  FILE *rf, *wf ;
  STU a[N], m ;
  int i ;

  rf = fopen("bc10.in", "r") ;
  wf = fopen("bc10.out", "w") ;
  for(i = 0 ; i < 10; i++) fscanf(rf, "%s %d", a[i].num, &a[i].s) ;
  fun(a, &m) ;
  fprintf (wf, "The lowest : %s, %d\n", m.num, m.s) ;
  fclose(rf) ;
  fclose(wf) ;
}
main ( )
{  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;
   printf("***** The original data *****\n");
   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 lowest  :  %s , %d\n",m.num, m.s);
   NONO( );
}


 

#include <stdio.h>
#define   N   8
typedef  struct
{  char  num[10];
   double  s[N];
   double  ave;
} STREC;

void  fun(STREC *aa)
{
	int j;
	aa->ave=0.0;
	for(j=0;j<N;j++)
		aa->ave+=aa->s[j];
	aa->ave/=N;

}
NONO()
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
  FILE *out ;
  int i,j ; STREC s[10] = {
  {"GA005",85.5,76,69.5,85,91,72,64.5,87.5},
  {"GA001",82.5,66,76.5,76,89,76,46.5,78.5},
  {"GA002",72.5,56,66.5,66,79,68,46.5,58.5},
  {"GA003",92.5,76,86.5,86,99,86,56.5,88.5},
  {"GA004",82,66.5,46.5,56,76,75,76.5,63.5},
  {"GA006",75.5,74,71.5,85,81,79,64.5,71.5},
  {"GA007",92.5,61,72.5,84,79,75,66.5,72.5},
  {"GA008",72.5,86,73.5,80,69,63,76.5,53.5},
  {"GA009",66.5,71,74.5,70,61,82,86.5,58.5},
  {"GA010",76,66.5,75.5,60,76,71,96.5,93.5},
};
  out = fopen("out.dat","w") ;
  for(i = 0 ; i < 10 ; i++) {
    fun(&s[i]) ;
    fprintf(out, "%7.3f\n", s[i].ave) ;
  }
  fclose(out) ;
}
main()
{  STREC  s={"GA005",85.5,76,69.5,85,91,72,64.5,87.5};
   int  i;
   fun( &s );
   printf("The %s's student data:\n", s.num);
   for(i=0;i<N; i++)
     printf("%4.1f\n",s.s[i]);
   printf("\nave=%7.3f\n",s.ave);
   NONO();
}


 

#include <stdio.h>
#include <stdlib.h>
#define   N   8
struct  slist
{  double   s;
   struct slist  *next;
};
typedef  struct slist  STREC;
double  fun( STREC *h  )
{
	int i;
	double max=0.0;
	for(i=0;i<N;i++)
	{
		h=h->next;
		if(h->s>max)
			max=h->s;
	}
	return (max);

}

STREC * creat( double *s)
{ STREC  *h,*p,*q;   int  i=0;
  h=p=(STREC*)malloc(sizeof(STREC));p->s=0;
  while(i<N)
  { q=(STREC*)malloc(sizeof(STREC));
    q->s=s[i]; i++; p->next=q; p=q;
  }
  p->next=0;
  return  h;
}
outlist( STREC *h)
{ STREC  *p;
  p=h->next;   printf("head");
  do
  { printf("->%2.0f",p->s);p=p->next;}
  while(p!=0);
  printf("\n\n");
}
NONO()
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
  FILE *in, *out ;
  int i,j ; double  s[N],max;
  STREC *h ;
  in = fopen("in.dat","r") ;
  out = fopen("out.dat","w") ;
  for(i = 0 ; i < 10 ; i++) {
    for(j=0 ; j < N; j++) fscanf(in, "%lf,", &s[j]) ;
    h=creat( s );
    max=fun( h );
    fprintf(out, "%6.1lf\n", max) ;
  }
  fclose(in) ;
  fclose(out) ;
}
main()
{  double  s[N]={85,76,69,85,91,72,64,87}, max;
   STREC  *h;
   h=creat( s );   outlist(h);
   max=fun( h );
   printf("max=%6.1f\n",max);
   NONO();
}


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值