zoukankan      html  css  js  c++  java
  • 作业6 结构体

    一、预习检查:自学教材第九章、阅读博客[结构体简介],并回答以下问题:

    1.什么是结构?什么情况下使用结构管理数据?

          有时需要由不同类型的数据来构成一个整体。 这就需要结构体,它允许内部的元素是不同类型的

    2.什么是结构定义和结构变量?

         结构体的一般定义形式为:

    struct 结构体名{
        
        类型名1 成员名1;
        
        类型名2 成员名2;
        
        ……
        
        类型名n 成员名n;   
        
    };

        struct是关键字,是结构体类型的标志

    例子:

    struct Student {
        char *name; // 姓名
        int age; // 年龄
        float height; // 身高
    };

        定义了一个叫做Student的结构体,共有name、age、height3个成员。

     结构变量:1.先定义结构体类型,再定义变量。

    struct Student {
        char *name;
        int age;
    };
    
    struct Student stu;

                    2.定义结构体类型的同时定义变量。

    struct Student {
        char *name;
        int age;
    } stu;

                    3.直接定义结构体类型变量,省略类型名。

    struct {
        char *name;
        int age;
    } stu;

                   上述的结构体变量名为stu 

    3.什么是结构的成员,如何初始化和引用结构成员的值?

          结构体内部的元素,也就是组成成分,我们一般称为"成员"

          将各成员的初值,按顺序地放在一对大括号{}中,并用逗号分隔,一一对应赋值

          只能在定义变量的同时进行初始化赋值,初始化赋值和变量的定义不能分开

    struct Student {
        char *name;
        int age;
    };
    
    struct Student stu = {"MJ", 27};

    错误做法:

    struct Student stu;
    stu = {"MJ", 27};

       结构体的使用:

    1.一般对结构体变量的操作是以成员为单位进行的,引用的一般形式为:结构体变量名.成员名

    struct Student {
        char *name;
        int age;
    };
    
    struct Student stu;
    
    // 访问stu的age成员
    9、stu.age = 27;

    第9行对结构体的age成员进行了赋值。"."称为成员运算符,它在所有运算符中优先级最高

    2.如果某个成员也是结构体变量,可以连续使用成员运算符"."访问最低一级成员

    struct Date {
         int year;
         int month;
         int day;
    };
    
    struct Student {
        char *name;
        struct Date birthday;
    };
    
    struct Student stu;
    
    stu.birthday.year = 1986;
    stu.birthday.month = 9;
    stu.birthday.day = 10;

    第14行以后的代码 

    3.相同类型的结构体变量之间可以进行整体赋值

    struct Student {
        char *name;
        int age;
    };
    
    struct Student stu1 = {"MJ", 27};
    
    // 将stu1直接赋值给stu2
    9.struct Student stu2 = stu1;
    
    printf("age is %d", stu2.age);

    注意第9行。输出结果

    4.结构变量如何作为函数参数使用?

          将结构体变量作为函数参数进行传递时,其实传递的是全部成员的值,也就是将实参中成员的值一一赋值给对应的形参成员。因此,形参的改变不会影响到实参。

    5.什么是结构数组,如何定义和使用结构数组?

     

    6.什么是结构指针,如何利用结构指针操作结构成员?

           每个结构体变量都有自己的存储空间和地址,因此指针也可以指向结构体变量

           结构体指针变量的定义形式:struct 结构体名称 *指针变量名

          有了指向结构体的指针,那么就有3种访问结构体成员的方式

    • 结构体变量名.成员名
    • (*指针变量名).成员名
    • 指针变量名->成员名
      #include <stdio.h>
      
      int main(int argc, const char * argv[]) {
          // 定义一个结构体类型
          struct Student {
              char *name;
              int age;
          };
          
          // 定义一个结构体变量
          struct Student stu = {"MJ", 27};
          
          // 定义一个指向结构体的指针变量
          struct Student *p;
          
          // 指向结构体变量stu
          p = &stu;
      
          /*
           这时候可以用3种方式访问结构体的成员
           */
          // 方式1:结构体变量名.成员名
          printf("name=%s, age = %d 
      ", stu.name, stu.age);
          
          // 方式2:(*指针变量名).成员名
          printf("name=%s, age = %d 
      ", (*p).name, (*p).age);
          
          // 方式3:指针变量名->成员名
          printf("name=%s, age = %d 
      ", p->name, p->age);
          
          return 0;
      }

     二、运行和理解教材例题9-1

    /* 构建学生信息库,实现建立,查询,输出功能 */
    #include<stdio.h>
    #define MaxSize 50
    struct student{
        int num;
        char name[10];
        int computer,english,math;
        double average;
    };
    int Count=0;
    void new_student(struct student students[]);
    void search_student(struct student students[],int num);
    void output_student(struct student students[]);
    int main(void)
    {
        int choice,num;
        struct student students[MaxSize];
        do{
            printf("choice: 1:new 2:search 3:output 0:exit
    ");
            scanf_s("%d",&choice);
            switch(choice){
            case 1: new_student(students); break;
            case 2:printf("输入学生学号:");
                scanf_s("%d",&num);
                search_student(students,num);
                break;
            case 3:output_student(students); break;
            case 0:break;
            }
        }while(choice!=0);
        printf("END!
    ");
        return 0;
    }
    /* 新建学生信息 */
    void  new_student(struct student students[]){
        struct student s;
        if(Count==MaxSize){
            printf("The array is full!
    ");
            return ;
        }
            printf("输入学生学号:");
            scanf_s("%d",&s.num);
            printf("Input the student's name:");
            scanf_s("%s",s.name);
            printf("Input the student's math score:");
            scanf_s("%d",&s.math);
            printf("Input the student's english score:");
            scanf_s("%d",&s.english);
            printf("Input the student's computer score:");
            scanf_s("%d",&s.computer);
            s.average=(s.math+s.english+s.computer)/3.0;
            students[Count]=s;
            Count++;
    }
    void search_student(struct student students[],int num)
    {
        int i,flag=0;
        if(Count==0){
            printf("Count of students is zero!
    ");
            return ;
        }
        for(i=0;i<Count;i++)
            if(students[i].num==num){
                flag=1;
                break;
            }
            if(flag){
                printf("num:%d,",students[i].num);
                printf("name:%s,",students[i].name);
                printf("math:%d,",students[i].math);
                printf("english:%d,",students[i].english);
                printf("computer:%d,",students[i].computer);
                printf("average:%.2Lf
    ",students[i].average);
            }
            else
                printf("Not Found!");
    }
    /* 输出学生信息 */
    void output_student(struct student students[])
    {
        int i;
        if(Count==0){
            printf("Count of students is zero!
    ");
            return ;
        }
        printf("num	 name	 math	 english	 computer	 average
    ");
        for(i=0;i<Count;i++){
            printf("%d	",students[i].num);
                printf("%d	",students[i].name);
                printf("%d	",students[i].math);
                printf("%d	",students[i].english);
                printf("%d	",students[i].computer);
                printf("%.2Lf
    ",students[i].average);
        }
    }

    三、预习检查:选择正确答案,并简要说明为什么?

    1)  下面定义结构变量的语句中错误的是 _D__,为什么? 

    A.struct student{ int num; char name[20]; } s;

    B.struct { int num; char name[20]; } stu ;                                               根据结构变量的定义方式:1.先定义结构体类型,再定义变量    

    C.struct student{ int num; char name[20]; }; struct student s;                                                    2.定义结构体类型的同时定义变量

    D.struct student{ int num; char name[20]; }; struct stu s;                                                          3.直接定义结构体变量,忽略类型

    2) struct { int x, y; } s[2] = { { 1, 3 }, { 2, 7 } }; 则语句:printf(“%d ”, s[0].y/s[1].x ); 输出结果是 __B_, 为什么? 

              A.0        B.1          C.2         D.3

         3) 分析下面的程序片段,能打印出字母 M 的语句是 __C_, 为什么?。         

    struct person{char name[10];int age; } c[10] = { “John”, 17, “Paul”, 19, “Mary”, 18, “Adam”, 16 };

    A.printf(“%c”, c[3].name);

    B.printf(“%c”, c[3].name[1]);

    C.printf(“%c”, c[2].name[0]);                                            根据数组

    D.printf(“%c”, c[2].name[1]);

          4) 设有如下定义,则对 data 中的 a 成员的正确引用是 __B_, 为什么?           

               struct sk{ int a; float b; } data, *p=&data;           

               A.(*p).data.a       B.(*p).a    C.p->data.a        D.p.data.a  

           根据3种访问结构体成员的方式

    • 结构体变量名.成员名
    • (*指针变量名).成员名
    • 指针变量名->成员名

     

    四、预习检查:填空,并注释"每空"填充的依据

  • 相关阅读:
    吴裕雄--天生自然C++语言学习笔记:C++ 引用
    吴裕雄--天生自然C++语言学习笔记:C++ 指针
    吴裕雄--天生自然C++语言学习笔记:C++ 字符串
    吴裕雄--天生自然C++语言学习笔记:C++ 数组
    吴裕雄--天生自然C++语言学习笔记:C++ 数字
    吴裕雄--天生自然C++语言学习笔记:C++ 函数
    吴裕雄--天生自然C++语言学习笔记:C++ 判断
    HiHoCoder1671 : 反转子串([Offer收割]编程练习赛41)(占位)
    HihoCoder1670 : 比赛日程安排([Offer收割]编程练习赛41)(模拟)
    POJ3417Network(LCA+树上查分||树剖+线段树)
  • 原文地址:https://www.cnblogs.com/simple9495/p/3434677.html
Copyright © 2011-2022 走看看