• C语言程序设计教程(第三版)李凤霞 第十章课后习题答案


    习题十
    一、 单选题
    1、下面的四个运算符中,优先级最低的是( )。
    A)( ) B). C)-> D)++
    2、已知: struct
    {int i;
    char c;
    float a;
    }test;
    则sizeof(test)的值是( )。
    A)4 B)5 C)6 D)7
    3、选择出错误的函数定义( )。
    A)struct tree funa(s) B)int *funb(s) C)struct tree *func(s) **D)**int *fund(s)
    Struct tree s[ ]; char s[ ]; char **s; char *s[10][ ];
    { … {… {… {…
    } } } }
    4、以下对C语言中联合类型数据的正确叙述是( )。
    A) 一旦定义了一个联合变量后,即可引用该变量或该变量中的任意成员
    B) 一个联合变量中可以同时存放其所有成员
    C) 一个联合变量中不能同时存放其所有成员
    D) 联合类型数据可以出现在结构类体型定义中,但结构体类型数据不能出现在联合类型定义中
    5、已知函数原型为:
    struct tree *f(int x1,int x2,struct tree x3,struct tree *x4)
    其中tree为已定义过的结构,且有下列变量定义:
    struct tree pt,*p;int i;
    请选择正确的的函数调用语句( )。
    A)&pt=f(10,&i,pt,p); B)p=f(i++,(int *)p,pt,&pt);
    *C)p=f(i+1,&(i+2),p,p); D)f(i+1,&i,p,p);
    6、当说明一个结构变量时系统分配给它的内存是( )。
    A)各成员所需内存量的总和 B)结构中第一个成员所需内存量
    C)成员中占内存量最大者所需内存量 D)结构中最后一个成员所需内存量
    7、C语言结构类型变量在程序执行期间( )。
    A)所有成员一直驻留在内存中 B)只有一个成员驻留在内存中
    C)部分成员驻留在内存中 D)没有成员驻留在内存中
    8、已知:
    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
    9、若有以下定义和语句:
    struct student
    {int num,age;
    };
    struct student stu[3]={{1001,20},{1001,19},{1003,21}};
    struct student *p=stu;
    则以下错误的引用是( )。
    A)(p++)->num B)p++ C)(*p).num D)p=&stu.age
    10、以下对C语言中联合类型数据的叙述正确的是( )。
    A) 可以对联合变量直接赋值
    B) 使用联合变量的目的是节省内存
    C) 对一个联合变量,可以同时引用联合中的不同成员
    D) 联合类型定义中不能出现结构类型成员
    11、已知函数定义的形式如下:
    struct data *f( void)
    {……}
    则函数f( )。
    A) 没有参数,返回值是一个结构
    B) 有一个参数void,返回值是一个结构
    C) 没有参数,返回值是一个结构指针
    D) 有一个参数void,返回值是一个结构指针
    12、在对typedef的叙述中错误的是( )。用typedef可以定义各种类型名,但不能用来定义变量
    A) 用typedef可以增加新类型
    B) 用typedef只能是将已存在的类型用一个新的标识符来代替
    C) 使用typedef有利于程序的通用和移植
    13、设有以下语句:
    struct st
    {int n;
    struct st *next;
    };
    static struct st a[3]={5,&a[1],7,&a[2],9,NULL},*p;
    p=&a[0];
    则以下表达式的值为6的是( )。
    A)p++ ->n B)p->n++ C)(*p).n++ D)++p->n
    14、若已建立下面的链表结构,指针p、q分别指向图中所是结点,则不能将q所值的结点插入到链表末尾的一组语句是( )。
    A)q->next=NULL;p=p->next;p->next=q;
    B)p=p->next;q->next=p->next;p->next=q;
    C)p=p->next;q->next=p;p->next=q;
    D)p=(*p).next;(*q).next=(*p).next;(*p).next=q;

    二、填空题
    1、已知:

    union{int x;
          struct
           {char c1,c2;
            }b;
         }a;
    
    • 1
    • 2
    • 3
    • 4
    • 5

    执行语句a.x=0x1234之后,a.b.c1的值为(用16 进制表示),a.b.c2的值为(用16进制表示)__。(34 12)
    2、用typedef定义整型一维数组:

    typedef int ARRAY[10];
    
    • 1

    则对整型数组a[10],b[10],c[10]可定义为_________。(ARRAY a,b,c;)
    3、 已知:

    struct{
    iny x,y;
    }s[2]={{1,2},{3,4}},*p=s;
    
    • 1
    • 2
    • 3

    则:表达式++p->x的值为________。表达式(++p)->x的值________。(1 3)
    4、已知:

       struct {
              int x;
              char *y;
               }tab[2]={{1,"ab"},{2,"cd"}},*p=tab;
    
    • 1
    • 2
    • 3
    • 4

    则:表达式p->y的结果为________。表达式(++p)->y的结果为________。(a c)
    5、 已知:struct {int day;char month;intyear;}a,*b; b=&a;
    可用a.day引用结构中的成员day,请写出通过指针变量b引用成员a.day的其它两种形式,它们是__________和__________。((*b).day=? b->day=?)

    6、 分析下列程序执行结果。

    #include “stdio.h”
    main()
    {static struct s1
      {char c[4],*s;
       s1={“abc”,”def”};
     static struct s2
      {char *cp;struct s1 ss1;
       }s2={“ghi”,{“jkl”,”mno”}};
     printf(%c%c\n”,s1.c[0],*s1.s);  /*output ab    */
     printf(%s%s\n”,s1.c,s1.s);     /*output abcdef  */
     printf(%s%s\n”,s2.cp,s2.ss1.s);  /*output ghimno */
     printf(%s%s\n”,++s2.cp,++s2.ss1.s);  /* output hino */
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    7、 以下程序的功能是:读入一行字符(如:a,…,y,z),按输入时的逆序建立一个链式的结点序列,即先输入的位于链表尾(如下图),然后再按输入的相反顺序输出,并释放全部结点。

    #include 
    #define getnode(type)_________malloc(sizeof(type)) ( (struct node *)) 
    main()
    {struct node
      {char info;
       struct node *link;
       }*top,*p;
     char c;
     top=NULL;
     while((c=getchar())______________)!='\n'{p=getnode(struct node);
       p->info=c;
       p->link=top;
       top=p;
     }
    while(top)
     {_________________;  (p=top)
      top=top->link;
      putchar(p->info);
      free(p);
      }
     }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    8、 下面的函数将指针p2所指向的线性链表链接到p1所指向的的链表的末端。假定p1所指向的链表非空。

    #define NULL 0
    struct link
     {float a;
      struct link *next;
      };
    concatenate(p1,p2)
     struct list *p1,*p2;
     {if(p1->next==NULL)
         p1->next=p2;
      else
    concatenate(___________,p2);  (p->next)
          }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    9、 以下函数create用来建立一个带头结点的单项链表。新产生的结点总是插入再链表的末尾。单向链表的头指针作为函数的返回值。

    #include 
    struct list
    {char data;
     struct list *list;
    };
    struct list *cteate()
    {struct list *h,*p,*q;
     char ch;
     h=___________malloc(sizeof(struct list));struct list *)
     p=q=h;
     ch=getchar();
     while(ch!='\n')
      {p=____________malloc(sizeof(struct list));  (struct list *)
       p->date=ch;
       q->next=p;
       q=p;
       ch=getchar();
       }
       p->next='\0';
       ______________; (rerturn h)
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    三、编程题
    1、 成绩排序。按学生的序号输入学生的成绩,按照分数由低到高的顺序输出学生的名次、该名次的分数、相同名次的人数和学号;同名次的学号输出再同一行中,一行最多输出10 个学号。

    #include "stdio.h"
    struct student
    {int n;
     int mk;
     }
    main( )
    {int i,j,k,count=0,no;
     struct student stu[100],*s[100],*p;
     printf("\nPleasse enter mark(if mark<0 is end) \n");
     for(i=0;i<100;i++)
     { printf("No.%4d= =',i+1);
       scanf("%d",&stu[i].mk);
       s[i]=&stu[i];
       stu[i].n=i+1;
       if(stu[i].mk<=0)break;
       for(j=0;j<I;j++)
         for(k=j+1;k<=I;k++)
           if(s[j]->mk<s[k]->mk)
            {p=s[j];s[j]=s[k];s[k]=p;}
         }
      for(no=1,count=1,j=0;j<I;j++)
       {if(s[j]->mk>s[k+1]->mk)
    {printf("\nNo.%3d= =%4d%4d:",no,s[j]->mk,count);
     for(k=j-count+1;k<=j;k++)
      {ptintf("%3d",s[k]->n);
       if((k-(j-count))%10= =0&&k!=j)
         printf("\n      ");
       }
      count=1;
      no++;
     }
     else count++;
    }
          }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    2、 现在有教师(姓名、单位、住址、职称)和学生(姓名、班级、住址、入学成绩)的信息。请在输入10名教师和学生的信息后,按姓名进行排序,最后按排序后的顺序进行输出,对于教师要输出姓名、单位、住址和职称,对学生要输出姓名、班级、住址和入学成绩。请编程实现。

    /*p332_2.c*/
    #include "string.h"
    union dwbj
    {char dw[20];
     char bj[10];
     };
    union zcrxcj
     {char zhich[10];
      float rxcj;
      };
    struct inf
     {int fl;/*0:teacher;1:student*/
      char name[20];
      union dwbj db;
      char addr[30];
      union zcrxcj zs;
      };
    main()
    {int i,j;
     struct inf info[10];
     for(i=0;i<10;i++)
       {printf("teacher:input 0;student:input 1\n");
        scanf("%d",&info[i].fl);
        printf("Input name of teacher or student:\n");
        scanf("%s",info[i].name);
        printf("Input dept of teacher or class of student:\n");
        if(info[i].fl==0)scanf("%s",info[i].db.dw);
        else scanf("%s",info[i].db.bj);
        printf("Input address:\n");
        scanf("%s",info[i].addr);
        printf("job of teacher or suc of student\n");
        if(info[i].fl==0)scanf("%s",info[i].zs.zhich);
        else scanf("%f",&info[i].zs.rxcj);
        }
     for(i=0;i<10;i++)
       {printf("%s,",info[i].name);
        if(info[i].fl==0)printf("%s,",info[i].db.dw);
        else printf("%s,",info[i].db.bj);
        printf("%s,",info[i].addr);
        if(info[i].fl==0)printf("%s\n",info[i].zs.zhich);
        else printf("%f\n",info[i].zs.rxcj);
        }
     } 
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
  • 相关阅读:
    Vue基础
    LeetCode54题:螺旋矩阵(python3)
    在 Maui 中自绘组件1:绘制
    SpringMVC中文件的上传与下载
    MATLAB数字图像处理 实验三:空域频域图像去噪与锐化
    qt中d指针
    快速删除MySQL服务 。
    【Hadoop学习笔记】(三)——Sqoop
    信息化发展30
    makefile 使用学习,小白入门~
  • 原文地址:https://blog.csdn.net/weixin_44071904/article/details/126828798