作业

#include<stdio.h>
struct  Student//声明结构体类型struct student 
{int   num;
 char  name[20];
 float score;
};
int  main()
{struct  Student  stu[5]={{10101,"Zhang",78},{10103,"Wang",98.5},{10106,"Li",86},{10108,"Ling",73.5},{10110,"Sun",100}};//定义结构体数组并初始化 
struct  Student  temp;//定义结构体变量temp,用作交换时的临时变量 
const  int  n=5;//定义常变量n 
int  i,j,k;
printf("The order  is:\n");
for(i=0;i<n-1;i++)
{k=i;
for(j=i+1;j<n;j++)
if(stu[j].score>stu[k].score)//进行成绩比较 
k=j;
temp=stu[k];stu[k]=stu[i];stu[i]=temp;//stu[k]和stu[i]元素互换 
}
for(i=0;i<n;i++)
printf("%6d%8s%6.2f\n",stu[i].num,stu[i].name,stu[i].score);
printf("\n");
return 0;}
 The order is:
10110 Sun100.00
10103 Wang 98.50
10106 Li 86.00
10101 Zhang 78.00
10108 Ling 73.50


--------------------------------
Process exited after 0.3278 seconds with return value 0
请按任意键继续. . .
#include<stdio.h>
# define  N 3
struct  Student
{int    num;
 char   name[20];
 float  score[3];
 float  aver;
};
int  main()
{  void  input(struct  Student stu[]);
   struct  Student  max(struct  Student  stu[]);
   void  print(struct  Student  stu);
   struct  Student stu[N] , *p=stu;
   input(p);
   print(max(p));
   return 0;
}
void  input(struct  Student  stu[])
{  int  i;
   printf("请输入各学生的信息:学号,姓名,三门课成绩:\n");
   for(i=0;i<N;i++)
   {scanf("%d%s%f%f%f",&stu[i].num,stu[i].name,&stu[i].score[0],&stu[i].score[1],&stu[i].score[2]);
   stu[i].aver=(stu[i].score[0]+stu[i].score[1]+stu[i].score[2])/3.0;
   }
}
struct  Student  max(struct  Student  stu[])
{  int i,m=0;
  for(i=0;i<N;i++)
  if(stu[i].aver>stu[m].aver)
  m=i;
  return  stu[m];
}
void  print(struct  Student  stud)
{ printf("\n成绩最高的学生是:\n");
printf("学号:%d姓名:%s\n三门课成绩:%5.1f,%5.1f,%5.1f\n平均成绩:%6.2f\n",stud.num,stud.name,stud.score[0],stud.score[1],stud.score[2],stud.aver);
 }
 请输入各学生的信息:学号,姓名,三门课成绩:
1001 张三 70 80 90
1002 李明 50 65 70

1003 李四 97 84 65

成绩最高的学生是:
学号:1003姓名:李四
三门课成绩: 97.0, 84.0, 65.0
平均成绩: 82.00

--------------------------------
Process exited after 77.83 seconds with return value 0
请按任意键继续. . .
#include<stdio.h>
struct  Student//声明结构体类型struct student 
{  int  num;
   char name[20];
   char sex;
   int  age;
};
struct  Student  stu[3]={{10101,"LiLin",'M',18},{10102,"ZhangFang",'M',19},{10104,"WangMing",'F',20}};//定义指向struct student结构体变量的指针变量 
int  main()
{struct  Student  *p;//
printf("No.Name     sex  age\n");
for(p=stu;p<stu+3;p++)
printf("%5d%-20s%2c%4d\n",p->num,p->name,p->sex,p->age);//输出结果 
return 0;
}
 No.Name sex age
10101LiLin M 18
10102ZhangFang M 19
10104WangMing F 20

--------------------------------
Process exited after 0.2853 seconds with return value 0
请按任意键继续. . .
#include<stdio.h>
#include<string.h>
int  main()
 {struct  Student
   { long  num;
     char  name[22];
     char  sex;
     float score;
   };
   struct  Student  stu_1;//定义struct  student类型的变量stu1 
   struct  Student  *p;//定义指向struct student类型数据的指针变量p 
   p=&stu_1;//p指向stu1 
   stu_1.num=10101;//对结构体的变量成员赋值 
   strcpy(stu_1.name,"LiLin");//用字符串复制函数给stu1.name赋值 
   stu_1.sex='M';
   stu_1.score=89.5;
   printf("No.:%ld\nname:%s\nsex:%c\nscore:%5.1f\n",stu_1.num,stu_1.name,stu_1.sex,stu_1.score);//输出结果 
   printf("No.:%ld\nname:%s\nsex:%c\nscore:%5.1f\n",(*p).num,(*p).name,(*p).sex,(*p).score);
   return 0;
 }
 No.:10101
name:LiLin
sex:M
score: 89.5
No.:10101
name:LiLin
sex:M
score: 89.5

--------------------------------
Process exited after 0.238 seconds with return value 0
请按任意键继续. . .
#include<stdio.h>
#include<string.h>
struct Person//声明结构体类型 
{   char  name[20];//定义候选人姓名 
    int   count;//定义候选人得票数 
 } leader[3]={"li",0,"zhang",0,"sun",0};//定义结构体数组并初始化 
 int  main( )
 {  int  i,j;
    char leader_name[20];//定义 字符数组 
    for(i=1;i<=10;i++)
    {scanf("%s",leader_name);//输入候选人姓名 
    for(j=0;j<=3;j++)
    if(strcmp(leader_name,leader[j].name)==0)leader[j].count++;
     } 
     printf("\nResult:\n");
     for(i=0;i<3;i++)
     printf("%5s:%d\n",leader[i].name,leader[i].count);
     return 0;
 }
 zhang
zhang
li
sun
sun
li
zhanng
zhang
li
sun

Result:
li:3
zhang:3
sun:3

--------------------------------
Process exited after 28.82 seconds with return value 0
请按任意键继续. . .
#include<stdio.h>
#include<stdlib.h>
int  main()
{struct  Student//声明结构体类型 
  {  int   num;
     char  name[20];
     float score; 
  }
  student1,student2;//定义两个结构体变量 
  scanf("%d%s%f",&student1.num,student1.name,&student1.score);//输入两个学生的数据 
  scanf("%d%s%f",&student2.num,student2.name,&student2.score);
  printf("The  higher  score is:\n");
  if(student1.score>student2.score)
  printf("%d %s %6.2f\n",student1.num,student1.name,student1.score);
  else  if(student1.score<student2.score) 
  printf("%d %s %6.2f\n",student2.num,student2.name,student2.score);
  else
  {printf("%d %s %6.2f\n",student1.num,student1.name,student1.score);
   printf("%d %s %6.2f\n",student2.num,student2.name,student2.score);
  }
  return 0;
}
 2016 yang 78
2017 gang 89
The higher score is:
2017 gang 89.00

--------------------------------
Process exited after 21.43 seconds with return value 0
请按任意键继续. . .
#include<stdio.h>
#include<stdlib.h>
    int  main()
    {struct    Student//声明结构体类型 
    {long  int num;//输出4行结构体的成员 
     char  name[20];
     char  sex;
     char  addr[20];
    }
    a={10101,"Li Lin",'M',"123Beingjing Road"};//定义结构体变量并初始化 
    printf("NO.:%ld\nname:%s\nsex:%c\naddresss:%s\n",a.num,a.name,a.sex,a.addr);
    return 0;
    }
 NO.:10101
name:Li Lin
sex:M
addresss:123Beingjing Road

--------------------------------
Process exited after 0.3403 seconds with return value 0
请按任意键继续. . .

 

posted @ 2017-04-11 20:45  醉疯染梦  阅读(244)  评论(0编辑  收藏  举报