资源描述
第九章 习题及答案
精品资料
第九章 习题
一、选择题
1.以下选项中不能正确把cl定义成结构体变量的是( )
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;
2.有以下说明和定义语句
struct student
{ int age; char num[8];};
struct student stu[3]={{20,"200401"},{21,"200402"},{10\9,"200403"}};
struct student *p=stu;
以下选项中引用结构体变量成员的表达式错误的是( )
A) (p++)->num B)p->num C)(*p).num D)stu[3].age
3.有以下结构体说明、变量定义和赋值语句
struct STD
{char name[10];
int age;
char sex;
}s[5],*ps;
ps=&s[0];
则以下scanf函数调用语句中错误引用结构体变量成员的是( )。
A)scanf(“%s”,s[0].name); B)scanf(“%d”,&s[0].age);
C)scanf(“%c”,&(ps->sex)); D)scanf(“%d”,ps->age);
4.以下叙述中错误的是( )
A)可以通过typedef增加新的类型
B)可以用typedef将已存在的类型用一个新的名字来代表
C)用typedef定义新的类型名后,原有类型名仍有效
D)用typedef可以为各种类型起别名,但不能为变量起别名
5.有以下程序段( )
typedef struct node { int data; struct node *next; } *NODE;
NODE p;
以下叙述正确的是(C)
A)p是指向struct node结构变量的指针的指针
B)NODE p;语句出错
C)p是指向struct node结构变量的指针
D)p是struct node结构变量
6.若有以下定义和语句
union data
{ int i; char c; float f;}x;
int y;
则以下语句正确的是( )。
A)x=10.5; B)x.c=101; C)y=x; D)printf(“%d\n”,x);
7.有以下程序
main()
{ union { unsigned int n;
unsigned char c;
}u1;
ul.c=`A`;
printf("%c\n",u1.n);
}
执行后输出结果是()
A) 产生语法错 B) 随机值 C) A D) 65
8.有以程序
#include <stdio.h>
#include <string.h>
typedef struct { char name[9]; char sex; float score[2]; } STU;
void f( STU a)
{ STU b={“Zhao” ,’m’,85.0,90.0} ; int i;
strcpy(a.name,b.name);
a.sex=b.sex;
for(i=0;i<2;i++) a.score[i]=b.score[i];
}
main()
{ STU c={“Qian”,’p’,95.0,92.0};
f(c); printf(“%s,%c,%2.0f,%2.0f\n”,c.name,c.sex,c.score[0],c.score[1]);
}
程序的运行结果是
A)Qian,p,95,92 B) Qian,m,85,90
C)Zhao,p,95,92 D) Zhao,m,85,90
9.现有以下结构体说明和变量定义,如图所示,指针p,q,r分别指向一个链表中连续的三个结点。
struct node
{
char data;
struct node *next;
}*p,*q,*r;
现要将q和r所指结点交换前后位置,同时要保持链表的连续,以下不能完成此操作的语句是
A)q->next=r->next; p->next=r; r->next=q;
B) p->next=r; q->next=r->next; r->next=q;
C) q->next=r->next; r->next=q; p->next=r;
D) r->next=q; p->next=r; q-next=r->next;
10.有以下程序段
struct st
{ int x; int *y;}*pt:
int a[]={1,2},b[]={3,4};
struct st c[2]={10,a,20,b};
pt=c;
以下选项中表达式的值为11的是( )
A) *pt->y B) pt->x C) ++pt->x D) (pt++)->x
二、填空题
1.设有说明
struct DATE{int year;int month; int day;};
请写出一条定义语句,该语句定义d为上述结构体变量,并同时为其成员year、month、day 依次赋初值2006、10、1。
2.已有定义如下:
struct node
{ int data;
struct node *next;
} *p;
以下语句调用malloc函数,使指针p指向一个具有struct node类型的动态存储空间。请填空。
p = (struct node *)malloc( );
3.以下程序中函数fun的功能是:统计person所指结构体数组中所有性别(sex)为M的记录的个数,存入变量n中,并做为函数值返回。请填空:
#include<stdio.h>
#define N 3
typedef struct
{int num;char nam[10]; char sex;}SS;
int fun(SS person[])
{int i,n=0;
for(i=0;i<N;i++)
if( ==’M’ ) n++;
return n;
}
main()
{SS W[N]={{1,”AA”,’F’},{2,”BB”,’M’},{3,”CC”,’M’}}; int n;
n=fun(W); printf(“n=%d\n”,n);
}
4.以下程序运行后的输出结果是( ) 。
struct NODE
{ int k;
struct NODE *link;
};
main()
{ struct NODE m[5],*p=m,*q=m+4;
int i=0;
while(p!=q){
p->k=++i; p++;
q->k=i++; q--;
}
q->k=i;
for(i=0;i<5;i++) printf("%d",m[i].k);
printf("\n");
}
5.以下程序的功能是:建立一个带有头结点的单向链表,并将存储在数组中的 字符依次转储到链表的各个结点中,请从与下划线处号码对应的一组选若中选择出正确的选项。
#include
stuct node
{ char data; struct node *next;};
(1) CreatList(char *s)
{ struct node *h,*p,*q);
h=(struct node *) malloc(sizeof(struct node));
p=q=h;
while(*s!='\0')
{ p=(struct node *) malloc(sizeof(struct node));
p->data= (2) ;
q->next=p;
q= (3) ;
s++;
}
p->next='\0';
return h;
}
main()
{ char str[]="link list";
struct node *head;
head=CreatList(str);
...
}
三、编程题
1. 定义一个能正常反映教师情况的结构体teacher,包含教师姓名、性别、年龄、所在部门和薪水;定义一个能存放两人数据的结构体数组tea,并用如下数据初始化:{{“Mary “, ‘W’,40, ‘Computer’ , 1234 },{“Andy “, ‘M’,55, ‘English’ , 1834}};要求:分别用结构体数组tea和指针p输出各位教师的信息,写出完整定义、初始化、输出过程。
2.定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。
3.构建简单的手机通讯录,手机通讯录包括信息(姓名、年龄、联系电话),要求实现新建、查询功能。假设通信录最多容纳50名联系人信息。
4.建立一个教师链表,每个结点包括学号(no),姓名(name[8]),工资(wage),写出动态创建函数creat和输出函数print。
5.在上一题基础上,假如已经按学号升序排列,写出插入一个新教师的结点的函数Insert。第9章 习题答案
一、选择题
1-5 B D D A C 6-10 B C A D C
二、填空题
1. struct DATA d={2006,10,1};
2. sizeof(struct node)
3. person[i].sex
4. 13431
5. (1)struct node* (2)*s (3)p
三、编程题
1. 定义一个能正常反映教师情况的结构体teacher,包含教师姓名、性别、年龄、所在部门和薪水;定义一个能存放两人数据的结构体数组tea,并用如下数据初始化:{{“Mary “, ‘W’,40, ‘Computer’ , 1234 },{“Andy “, ‘M’,55, ‘English’ , 1834}};要求:分别用结构体数组tea和指针p输出各位教师的信息,写出完整定义、初始化、输出过程。
#include<stdio.h>
struct teacher
{ char name[8];
char sex;
int age;
char department[20];
float salary;
} ;
struct teacher tea[2]= {{"Mary ", 'W',40, "Computer" , 1234 },
{"Andy ", 'M',55, "English" , 1834}} ;
main()
{ int i;
struct teacher *p;
for( i=0;i<2;i++)
printf("%s,\t%c,\t%d,\t%s,\t%f", tea[i].name,tea[i].sex,tea[i].age,tea[i].department,tea[i].salary);
for(p=tea;p<tea+2;p++)
printf("%s,\t%c,\t%d,\t%s,\t%f", p->name, p->sex, p->age, p->department, p->salary);
}
2. 定义一个结构体变量(包括年、月、日)。计算该日在本年中是第几天,注意闰年问题。
#include<stdio.h>
struct
{int year;
int month;
int day;
}date;
main()
{int days;
printf(“Input year,month,day:”);
scanf(“%d,%D,%d”,&date.year,&date.month,&date.day);
switch(date.month)
{case 1: days=date.day; break;
case 2: days=date.day+31; break;
case 3: days=date.day+59; break;
case 4: days=date.day+90; break;
case 5: days=date.day+120; break;
case 6: days=date.day+31; break;
case 7: days=date.day+181; break;
case 8: days=date.day+212; break;
case 9: days=date.day+243; break;
case 10: days=date.day+273; break;
case 11: days=date.day+304; break;
case 12: days=date.day+334; break;
}
if((date.year%4==0&&date.year%100!=0||date.year%400==0)&&date.month>=3)
days+=1;
printf(“\n%d/%d is the %dth day in%d.”,date.month,date.day,days,date.year);
}
3.构建简单的手机通讯录,手机通讯录包括信息(姓名、年龄、联系电话),要求实现新建、查询功能。假设通信录最多容纳50名联系人信息。
#include<stdio.h>
#include<string.h>
/*手机通讯录结构定义*/
struct friends_list{
char name[10]; /* 姓名 */
int age; /* 年龄 */
char telephone[13]; /* 联系电话 */
};
int Count = 0; /* 定义全局变量Count,记录当前联系人总数 */
void new_friend(struct friends_list friends[ ] );
void search_friend(struct friends_list friends[ ], char *name);
int main(void)
{
int choice;
char name[10];
struct friends_list friends[50]; /* 包含50个人的通讯录 */
do{
printf("手机通讯录功能选项:1:新建 2:查询 0:退出\n");
printf("请选择功能:");
scanf("%d", &choice);
switch(choice){
case 1:
new_friend(friends);
break;
case 2:
printf("请输入要查找的联系人名:");
scanf("%s", name);
search_friend(friends, name);
break;
case 0: break;
}
}while(choice != 0);
printf("谢谢使用通讯录功能!\n");
return 0;
}
/*新建联系人*/
void new_friend(struct friends_list friends[ ])
{
struct friends_list f;
if(Count == 50){
printf("通讯录已满!\n");
return;
}
printf("请输入新联系人的姓名:");
scanf("%s", f.name);
printf("请输入新联系人的年龄:");
scanf("%d", &f.age);
printf("请输入新联系人的联系电话:");
scanf("%s", f.telephone);
friends[Count] = f;
Count++;
}
/*查询联系人*/
void search_friend(struct friends_list friends[ ], char *name)
{
int i, flag = 0;
if(Count == 0){
printf("通讯录是空的!\n");
return;
}
for(i = 0; i < Count; i++)
if(strcmp(name,friends[i].name) == 0){ /* 找到联系人*/
flag=1;
break;
}
if(flag){
printf("姓名: %s\t", friends[i].name);
printf("年龄: %d\t", friends[i].age);
printf("电话: %s\n", friends[i].telephone);
}
else
printf("无此联系人!");
}
4. 建立一个教师链表,每个结点包括学号(no),姓名(name[8]),工资(wage),写出动态创建函数creat和输出函数print。
#include<stdio.h>
#include<stdlib.h>
#define NULL 0
#define LEN sizeof(struct teacher)
struct teacher
{int no;
char name[8];
float wage;
struct teacher * next;
};
int n;
struct teacher *creat(void)
{ struct teacher *head;
struct teacher *p1,*p2;
n=0;
p1=p2= (struct teacher *)malloc(LEN);
scanf(“%d%s%f”,&p1->no,p1->name, &p1->wage);
head=NULL;
while(p1->no!=0)
{ n=n+1;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
p1=( struct teacher *)malloc(LEN);
scanf(“%d%s%f”,&p1->no,p1->name, &p1->wage);
}
p2->next=NULL;
return(head);
}
void print(struct teacher *head)
{ struct teacher *p;
p=head;
if (head!=NULL)
do{
printf(“%d\t%s\t%f\n”, p->no, p->name, p->wage);
p=p->next;
} while(p!=NULL);
}
5.在上一题基础上,假如已经按学号升序排列,写出插入一个新教师的结点的函数insert。
struct teacher insert(struct teacher *head,struct teacher *tea)
{ struct teacher *p0,*p1,*p2;
p1=head;
p0=tea;
if(head=NULL)
{head=p0; p0->next=NULL;}
else
while((p0->no>p1->no)&&(p1->next!=NULL))
{ p2=p1;
p1=p1->next;}
if(p0->no<=p1->no)
{ if (head==p1)
head=p0;
else
{ p2->next=p0;
p0->next=p1;
}
else
{ p1->next=p0;p0->next=NULL;}
n=n+1;
return(head);
}
仅供学习与交流,如有侵权请联系网站删除 谢谢12
展开阅读全文