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

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

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

    1.结构是c语言中的一种新的结构类型,它能够把有内在联系的不同类型的数据汇聚成一个整体

    使他们相互关联,同时,结构也是一个变量的集合,可以按照对基本类型的操作方法单独使用

    其成员变量。 2.各项数据类型不同但之间有内在联系的变量

    • 什么是结构定义和结构变量?

    struct 结构名{

    类型名 结构成员名1;

    类型名 结构成员名2;

    类型名 结构成员名n;

    };

    结构变量:结构类型的变量,通过结构成员操作符.对其成员变量引用,与普通变量作为

    函数参数相似,实参变量将其结构值传递给对应的形参变量    

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

    1.结构中的成员。2.初始化列表是用花括号括起来的一些常量,这些常量依次赋给结构体变量中的各成员

    使用结构成员操作符“.”进行操作,格式为:结构变量名.结构成员名

     

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

     

    • 什么是结构数组,如何定义和使用结构数组?
    • 什么是结构指针,如何利用结构指针操作结构成员?

    二、运行和理解教材例题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 ;

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

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

     

    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  

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

          1) 完成下列程序,该程序计算 10 名学生的平均成绩          

    复制代码
    #include <stdio.h>
    #include <string.h>
    struct student {
        int num;
        char name[20];
        int score;
    }; 
    struct student stud[10];
    int main(void)
    {
        int   i , sum = 0 ;
        for(i = 0; i < 10; i++){
            scanf("%d%s%d ",&stud[i].num,stud[i].name, &stud[i].score);
            sum += stud[i].score;
        }
        printf("aver = %d 
    ", sum/10);
        return 0;
    }
    复制代码

     

     2) 下列程序读入时间数值,将其加 1 秒后输出,时间格式为:hh: mm: ss,即小时:分钟:秒,当小时等于 24 小时,置为 0。          

     

    复制代码
    #include<stdio.h>
    struct { int hour, minute, second;} time;
    int main(void)
    {
        scanf("%d: %d: %d",    &time.hour, &time.minute, &time.second );
        time.second++;
        if( time.second == 60){
            time.minute++ ; 
            time.second = 0;
            if(time.minute == 60){
                time.hour++; 
                time.minute = 0;
                if( time.hour == 24 )
                    time.hour = 0; 
            }
        }
        printf ("%d: %d: %d 
    ", time.hour, time.minute, time.second );
        return 0;
    } 
    复制代码

     

      3) 写出下面程序的运行结果,并简要描述原因。       

     

    复制代码
    struct s1{
        char c1, c2;
        int   n;
    };
    struct s2{
        int n; 
        struct s1 m;
    } m = {1, {‘A’, ’B’, 2} };
    int main(void)
    { 
        printf(“%d	%d	%c	%c
    ”, m.n, m.m.n, m.m.c1, m.m.c2);
        return 0;
    }

    1 2 A B

     

     4) 写出下面程序的运行结果,并简要描述原因。          

     

    复制代码
    struct abc{    int a;    float b;    char *c; };
    int main(void)
    {
        struct abc x = {23,98.5,"wang"};
        struct abc *px = &x;
        printf("%d, %s, %.1f, %s 
    ", x.a, x.c, (*px).b, px->c );
        return 0;
    }
    复制代码

     

  • 相关阅读:
    python3中的文件操作
    python3网络爬虫实现有道词典翻译功能
    C++11 thread condition_variable mutex 综合使用
    goland scope pattern 设置
    Go 1.11 Module 介绍
    hustOJ 添加 golang 支持
    docker 搭建 hustoj
    最长重复字符串题解 golang
    golang 结构体中的匿名接口
    使用aliyun cli工具快速创建云主机
  • 原文地址:https://www.cnblogs.com/laurenliu1994/p/3428515.html
Copyright © 2011-2022 走看看