您的位置: 网站首页 > 程序开发 > C语言程序设计案例教程 > 第九章 结构体、共用体与枚举类型 > 【9.4 思考与练习】

9.4 思考与练习

 

思考与练习

1.选择题

(1)设有以下程序片段:

struct st

{

    int n;

    struct st *next;

};

static struct st a[3]={5,&a[1],7,&a[2],9,'\0'},*p;

p=&a[0];

则表达式      的值是6。

A.p++->n                  B.p->n + +                C.(*p).n++          D.++p->n

(2)以下程序的输出结果为      。

struct st

{

    int x;

    int *y;

} *p;

int dt[4]={10,20,30,40};

struct st aa[4]={50,&dt[0],60,&dt[1],70,&dt[2],80,&dt[3]};

main()

{

    p=aa;

    printf("%d\n",++p->x);

    printf("%d\n",(++p)->x);

    printf("%d\n",++( *p->y));

}

A.10                          B.50                          C.51                   D.60

        20                  60                   60               70

        20                  21                   21               31

(3)根据以下定义,能输出字母M的语句是      。

struct person

{

    char name[9];

    int age;

};

struct person class[10]={"John",17,"Paul",19,"Mary",18,"Adam",16};

A.printf("%c\n",class[3].name);                     

B.printf("%c\n",class[3].name[1]);

C.printf("%c\n",class[2].name[1]);                 

D.printf("%c\n",class[2].name[0]);

(4)设有如下定义:

typedef union { long i; int k[5]; char c;} DATE;

struct date { int cat; DATE cow; double dog;} too;

DATE max;

则以下语句的输出结果是      。

printf("%d",sizeof(struct date)+sizeof(max));

A.25                          B.30                          C.18                   D.8

(5)若程序中有以下程序片段:

struct abc

{

    int x;

    char y;

}

struct abc s1,s2;

则会发生的情况是      。

A.编译出错                                                  B.能顺利编译、连接,且能执行

C.能顺利通过编译、连接,但不能执行        D.能顺利通过编译,但连接出错

(6)若有以下程序片段,则sizeof(struct aa)的值是      。

struct aa

{

    int r1;

    double r2;

    float r3;

    union uu { char u1[5]; long u2[2]; } ua;

} mya;

A.30                          B.29                          C.24                   D.22

(7)设有如下定义:

struct sk

{

    int a;

    float b;

} data,*p;

若有定义语句p=&data;,则对data中的a域的正确引用是      。

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

(8)以下程序的输出结果是      。

struct stu

{

      int num;

      char name[10];

      int age;

};

void fun(struct stu *p)

{

    printf("%s\n",(*p).name);

}

main()

{

    struct stu students[3]={{9801,"Zhang",20},{9802,"Wang",19},{9803,"Zhao",18}};

    fun(students+2);

}

A.Zhang                    B.Zhao                      C.Wang              D.18

(9)设有如下程序片段:

typedef union

        {

            long i;

            int k[5];

            char c; 

        } DATE;

 

struct date

{

    int cat;

    DATE cow;

    double dog;

} too;

 

DATE max;

则以下语句的执行结果是      。

printf ("%d",sizeof(struct date)+sizeof(max));

A.26                         B.30                          C.18                   D.8

(10)以下选项中,能定义s为合法的结构体变量的是      。

A.typedef  struct   abc                             B.struct

         {                                        {

            double a;                                double a;

            char b[10];                              char b[10];

         } s;                                     }s;

C.struct    ABC                                           D.typedef ABC

         {                                        {

            double   a;                              double a;

            char   b[10];                            char b[10];

         }                                        }

         ABC s;                                   ABC s;

(11)设有如下程序片段,则变量a所占的内存字节数是       。

union U

{  char st[4];

   int i;

   long l;

};

struct A

{  int c;

   union U u;

}a;

A.4                           B.5                            C.6                     D.8

(12)根据以下定义,能输出字母M的语句是      。

struct person {char name[9];int age;};

strict person class[10]={"Johu",17,"Paul",19,"Mary",18,"Adam",6 };

A.prinft("%c\n",class[3].mane);                      B.pfintf("%c\n",class[3].name[1]);

C.prinft("%c\n",class[2].name[1]);                  D.printf("%c\n",class[2].name[0]);

(13)以下对结构体类型变量的定义中,不正确的是      。

A.typedef struct aa                                  B.#define AA    struct aa

         {                                        AA

            int n;                                {

            float m;                                  int n;

            } AA;                                     float m;

         AA td1;                                  } td1;

C.struct                                                      D.struct

         {                                        {   

            int n;                                    int n;

            float m;                                  float m;

         } aa;                                    } td1;

(14)以下对枚举类型名的定义中,正确的是      。

A.enum a={one,two,three};                                   B.enum a {one=9,two=-1,three};

C.enum a={"one","two","three"};                  D.enum a {"one","two","three"};

(15)设有如下程序片段:

struct test

{

   int ml; char m2; float m3;

   union uu{char ul[5]; int  u2[2];} ua;

}myaa;

则sizeof(struct test)的值是      。

A.12                                                             B.16                         

C.14                                                             D.9

(16)设有如下程序片段:

typedef  struct 

   {

      int  n;

      char  ch[8];

   } PER;

则以下叙述中,正确的是      。

A.PER是结构体变量名                                B.PER是结构体类型名

C.typedef  struct是结构体类型                            D.struct是结构体类型名

(17)以下选项中,不能正确将c1定义成结构体变量的是      。

A.typedef  struct                                      B.struct  color  cl

         {                                        {

            int red;                                 int  red;

            int green;                               int  green;

            int blue;                                int  blue;

            } COLOR;                              };

         COLOR  cl;

C.struct  color                                          D.struct

         {                                        {

            int red;                                  int red;

            int green;                                int green;

            int blue;                                 int blue;

         }cl;                                     }cl ;

(18)以下程序的输出结果是      。

struct s

{

   int x;

   int y;

} data[4]={10,100,20,200};

main()

{

   struct s *p=data;

   printf("%d\n",++(p->x));

}

A.10                          B.11                          C.20                   D.21

(19)设建立了以下链表结构,如图9-3所示。指针p、q分别指向如图9-3所示的节点,则以下选项中,可以将q所指节点从链表中删除并释放该节点的语句组是      。

A.free(q); p->next=q->next;

B.(*p).next=(*q).next; free(q);

C.q=(*q).next; (*p).next=q; free(q);

D.q=q->next; p->next=q; p=p->next; free(p);

图9-3  链表结构图

(20)有以下结构体说明和变量定义,指针p、q、r分别指向一个链表中的三个连续节点,如图9-4所示。

struct node

{

    int data

    struct node *next ;

} *p,*q,*r;

图9-4  链表结构图

现要将q和r所指节点的先后位置交换,同时要保持链表的连续,以下程序片段中,错误的是      。

A.r->next=q; q->next=r->next; p->next=r;

B.q->next=r->next; p->next=r; r->next=q;

C.p->next=r; q->next=r->next; r->next=q;

D.q->next=r->next; r->next=q; p->next=r;

2.填空题

(1)若有以下程序片段,则可用a.day引用结构体成员day,写出引用结构体成员a.day的其他两种形式      、      。

struct

{

    int day;

    char mouth;

    int year;

} a,*b;

b=&a;

(2)若有以下程序片段,则sizeof(a)的值是      ,而sizeof(b)的值是      。

struct

{

    int day;

    char mouth;

    int year;

} a,*b;

b=&a;

(3)若有以下程序片段,则sizeof(a)的值是      ,而sizeof(a.share)的值是      。

struct date

{ 

    int day;

    int month;

    int year;

    union

    {

        int share1

        float share2;

    } share;

} a;

(4)以下语句的功能是使指针p指向一个double型的动态存储单元。

p=      malloc(sizeof(double));

(5)设有以下结构类型说明和变量定义,则变量a在内存中所占的字节数是      。

struct stud

{

    char num[6];

    int s[4];

    double  ave;

} a,*p;

(6)以下程序片段用于构成一个简单的单向链表。

struct STRU

{

   int  x, y ;

   float rate;

         p;

} a,b;

a.x=0; a.y=0; a.rate=0; a.p=&b;

b.x=0; b.y=0; b.rate=0; b.p=NULL;

(7)若有如下结构体说明:

struct STRU

{

    int a,b;

    char c;

    double  d;

    struct STRU p1,p2;

};

则应对t数组的定义为       t[20],其中t数组的每个元素的为该结构体类型。

(8)以下定义的结构体类型包含两个成员:成员变量info用来存入整形数据;成员变量link是指向自身结构体的指针。

struct node

{

    int info;

          link;

}

(9)若有如下定义:

struct node

{

    int data;

    struct node *next;

} *p;

以下语句的功能是调用malloc( )函数,使指针p指向一个struct node型的动态存储空间。

p=(struct node *)malloc(      );

(10)若有以下程序体,则变量w在内存中所占的字节数是      。

union aa

{

    float x,y;

    char c[6];

};

struct st

{

    union aa v;

    float w[5];

    double ave;

} w;

(11)以下程序建立了一个带有头节点的单向链表,链表节点中的数据通过键盘输入,当输入的数据为-1时,表示输入结束(链表头节点的data域不存放数据,表空的条件是ph->next == NULL)。

#include <stdio.h>

 

struct list

{

   int data;

   struct list *next;

};

 

       creatlist()

{

    struct list *p,*q,*ph;  int a;

    ph=(struct list *)malloc(sizeof(struct list));

    p=q=ph;

    printf("Input an integer number,enter –1 to end:\n");

    scanf("%d",&a);

    while(a!=-1)

    {

        p=(struct list *)malloc(sizeof(struct list));

        p->data=a;

        q->next=p; 

              =p;

        scanf("%d",&a);

    }

    p->next='\0';

    return(ph);

}

main()

{

    struct list *head;

    head=creatlist();

}

(12)现有如图9-5所示的存储结构,每个节点含有两个域:data是指向字符串的指针域;next是指向节点的指针域。填空完成此结构的类型定义和说明。

图9-5  存储结构图

struct link

{

        ;

        ;

} *head;

(13)变量root有如图9-6所示的存储结构,其中sp是指向字符串的指针域,next是指向该结构的指针域,data用以存放整型数。完成此结构的类型说明和变量root的定义。

         sp next data

        ┌─┬─┬─┐

    root│  │  │  │

        └─┴─┴─┘

     图9-6  存储结构图

struct list

{

    char  *sp;

            ;

            ;

} root;

(14)以下min3函数的功能是:计算单向循环链表first中每三个相邻节点数据域中值的和,返回其中最小的值。

图9-7  存储结构图

struct node

{

    int data;

    struct node *link;

};

int min3(       *first)

{

    struct node *p=first;

    int m,m3;

 

    m3=p->data + p->link->data + p->link->link->data;

    for(p=p->link; p!= first; p=       )

    {

        m=p->data + p->link->data + p->link->link->data;

        if(      ) m3=m;

    }

    return (m3);

}

3.上机操作题

(1)定义一个结构体类型,用来描述日期(年、月、日),并计算该日期在这一年中是第几天。

(2)编写一个函数,用来输出5条学生信息,每条学生信息由学号、姓名、成绩组成。要求在主函数中输入学生信息,利用所编写的函数输出学生信息。

(3)编写一个程序,实现一个链表,链表中存储5条学生信息,每条学生信息包括学号、成绩。

(4)编写一个程序,实现输入星期号时,输出该星期的英文名。要求使用枚举类型。