2017年9月18日学习总结

2017-09-18  本文已影响0人  陶雪婷a

一、新知识点掌握摘要

(1)       结构体类型的定义

Struct 结构体名{

成员表列}

(2)       结构体类型变量的定义

Struct student  stu(student为结构体名,stu为结构体变量名)

(3)       结构体类型变量的内涵:当定义了结构体类型后,系统不分配内存空间,只有当定义了结构体类型变量之后,系统才会为之分配内存空间

4)       结构体类型变量的引用遵循规则

a、 不能将一个结构体变量作为一个整体进行输入和

输出。例如,已定义student1和student2为结构体

变量并且它们已有值。不能这样引用:

printf (“%d,%s,%c,%d,%f,%s\n”,student1);只能

对结构体变量中的各个成员分别进行输入和输出。

b、对结构体变量的成员可以像普通变量一样进行各

种运算(根据其类型决定可以进行的运算)

c、可以引用结构体变量成员的地址,也可以引用结

构体变量的地址如:scanf("%d“,&student1.num);

printf(“%o”,&student1);但不能用以下语句整体

读入结构体变量,如:

scanf("%d,%s,%c,%d,%f,%s“,&student1);

二、作业难题积累

录入结构体数组并找出每一刻成绩最高分的同学输出成绩及学号

#include<stdio.h>

struct student

{

  int no;

 char name[20];

    float cj1;

float cj2;

  float cj3;

  } ;

struct student stu[5];

int main(){

   int i,j;

   int max1,max2,max3;

  for(i=0;i<5;i++){

      printf("请输入学号:");

            scanf("%d",&stu[i].no);

            printf("请输入姓名:");

                     getchar();

            gets(stu[i].name);

            printf("请输入成绩1:");

            scanf("%f",&stu[i].cj1);

            printf("请输入成绩2:");

            scanf("%f",&stu[i].cj2);

            printf("请输入成绩3:");

            scanf("%f",&stu[i].cj3);

            printf("\n");

            }

            max1=0;max2=0;max3=0;

               for(i=1;i<5;i++){

                        if(stu[i].cj1>stu[max1].cj1)

                        max1=i;

                        if(stu[i].cj2>stu[max2].cj2)

                        max2=i;

                        if(stu[i].cj3>stu[max3].cj3)

                        max3=i;

            }printf("一颗成绩最高的分数及学号;%f %d",stu[max1].cj1,stu[max1].no);

            printf("第二颗成绩最高的分数及学号;%f %d",stu[max2].cj2,stu[max2].no);

            printf("第三颗成绩最高的分数及学号;%f %d",stu[max3].cj3,stu[max3].no);

   }

上一篇 下一篇

猜你喜欢

热点阅读