C理论自测题及参考答案第4次范文.docx

上传人:b****5 文档编号:3525896 上传时间:2022-11-23 格式:DOCX 页数:15 大小:27.16KB
下载 相关 举报
C理论自测题及参考答案第4次范文.docx_第1页
第1页 / 共15页
C理论自测题及参考答案第4次范文.docx_第2页
第2页 / 共15页
C理论自测题及参考答案第4次范文.docx_第3页
第3页 / 共15页
C理论自测题及参考答案第4次范文.docx_第4页
第4页 / 共15页
C理论自测题及参考答案第4次范文.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

C理论自测题及参考答案第4次范文.docx

《C理论自测题及参考答案第4次范文.docx》由会员分享,可在线阅读,更多相关《C理论自测题及参考答案第4次范文.docx(15页珍藏版)》请在冰豆网上搜索。

C理论自测题及参考答案第4次范文.docx

C理论自测题及参考答案第4次范文

一、单选题

1.已知数据类型定义和变量声明如下:

structsk

{inta;floatb;}data[2],*p;

若有p=data,则以下对data[0]中成员a的引用中错误的是。

A.data[0]->aB.data->aC.p->aD.(*p).a

2.已有数据类型定义和变量声明如下:

structperson

{ intnum;

charname[20],sex;

struct{intclass;charprof[20];}in;

}a={20,"Lining",’M’,{5,"computer"}},*p=&a;

下列语句中正确的是。

A.printf("%s",a->name);B.printf("%s",p->in.prof);

C.printf("%s",*p.name);D.printf("%c",p->in->prof);

3.已知有如下的结构类型定义和变量声明:

structstudent

{intnum;

charname[10];

}stu={1,”Mary”},*p=&stu;

则下列语句中错误的是。

A.printf(“%d”,stu.num);B.printf(“%d”,(&stu)->num);

C.printf(“%d”,&stu->num);D.printf(“%d”,p->num);

4.若main函数中有以下定义、声明和语句,则不能输出字符串“England”的语句是。

A.puts(x[1].b);B.puts((x+1)->b);

C.puts((++x)->b);D.puts((++p)->b);

structtest

{inta;

char*b;

};

charx0[]="UnitedstatesofAmerican",x1[]="England";

structtestx[2],*p=x;

x[0].a=300;x[0].b=x0;

x[1].a=400;x[1].b=x1;

5.若要使表达式“P++”无语法错误,则变量P不能声明为。

A.intP;B.doubleP;C.int*P;D.struct{intx;}P;

6.若有定义和声明typedefenum{green,red,yellow,blue,black}color;colorflower;,则下列语句中正确的是。

A.green=red;B.flower=red;C.color=red;D.enum=red;

7.以下程序运行时输出结果是。

A.10B.25C.15D.27

#include

#definePI3.14

#defineF(y)((y)*(y))

#defineP(a)printf("%d",a)

main()

{intx=PI;P(F(1+2)*x);}

参考答案:

1~5ABCCD6~10:

BD

二、填空题

1.在用fopen函数打开一个已经存在的数据文件abc时,若要求既可以读出abc文件中原来的内容,也可以用新的数据覆盖文件原来的数据,则打开模式应当是。

2.设函数a的定义如下:

voida()

{intx=12,y=345;FILE*fp=fopen("my.dat","w");

fprintf(fp,"%d%d",x,y);

fclose(fp);

}

已知main函数中有声明intx,y;FILE*fp=fopen("my.dat","r");,若需要从文件my.dat中正确地读出由函数a写入的两个数据并分别保存到变量x和y中,则在main函数中使用的读数据语句应当是(要求写出语句的完整格式)。

3.执行以下程序段时输出结果是。

#include

main()

{FILE*fp=fopen("a.dat","w+");

chars[11]={0};

floatx=-1.2345;fprintf(fp,"%6.3f",x);

rewind(fp);

fgets(s,10,fp);

puts(s);

fclose(fp);

}

4.若有宏定义如下:

#defineN2

#defineY(n)((N+1)*n)

则执行语句“z=2*(N+Y(N+2));”后z的值是。

5.以下程序输出结果是。

#defineT(x,y,z)x*y*z/4

main()

{inta=1,b=3,c=5;

printf("%d",T(b+a,a*++b,a+b+c));

}

6.以下程序输出结果是。

main()

{enumcolor{RED,BLUE,WHITE=0,BLACK};

char*colorname[]={"red","blue","white","black"};

printf("%s",colorname[BLACK]);

}

7.以下程序运行时,输出结果的第一行是,第二行是,

第三行是。

#include

typedefstructs

{intindex;

intvalue;

}M;

main()

{staticinti,j,k,c[4][4];

Ma[10]={{0,1},{3,2},{5,3},{6,4},{9,5},{15,6},{-1,0}},*p=a,

b[10]={{1,1},{3,2},{4,3},{6,4},{10,5},{13,6},{-1,0}},*q=b;

while(p->index!

=-1)

{i=p->index/4;

j=p->index%4;

c[i][j]=p->value;

p++;

}

while(q->index!

=-1)

{i=q->index/4;

j=q->index%4;

c[i][j]+=q->value;

q++;

}

for(i=0;i<4;i++)

{for(j=0;j<4;j++)

printf("%d",c[i][j]);

printf("\n");

}

}

8.以下程序输出结果为。

#include

structs

{inta;

structs*next;

};

main()

{inti;

staticstructsx[2]={5,&x[1],7,&x[0]},*ptr;

ptr=&x[0];

for(i=0;i<3;i++)

{printf("%d",ptr->a);ptr=ptr->next;}

}

9.以下程序运行时,输出结果第一行为,第二行为,第三行为。

#include

#include

typedefstructnode

{intd;

structnode*next;

}NODE;

NODE*insert(NODE*head,intx,intkey)

{NODE*s,*p,*q;

s=(NODE*)malloc(sizeof(NODE));

s->d=key;s->next=NULL;

if(head==NULL)

{head=s;returnhead;}

if(head->d==x)

{s->next=head;head=s;returnhead;}

else

{q=head;p=q->next;

while((p->d!

=x)&&(p->next!

=NULL))

{q=p;p=p->next;}

if(p->d==x)

{s->next=p;q->next=s;}

else

{s->next=NULL;p->next=s;}

returnhead;

}

}

voidprint(NODE*head)

{if(head==NULL)return;

while(head->next!

=NULL)

{printf("%d,",head->d);head=head->next;}

printf("%d\n",head->d);

}

main()

{NODE*head=NULL;

head=insert(head,0,3);print(head);

head=insert(head,3,1);print(head);

head=insert(head,4,5);print(head);

}

10.以下程序运行时,输出结果第一行为,第二行为,第三行为。

#include

#include

structnode

{intd;

structnode*next;

};

structnode*create(void)

{structnode*head=NULL,*p,*q=NULL;

inti;

for(i=2;i<=9;i++)

{p=(structnode*)malloc(sizeof(structnode));

p->d=i;p->next=NULL;

if(head==NULL)head=p;

elseq->next=p;

q=p;

}

return(head);

}

voidprint(structnode*head)

{if(head==NULL)return;

while(head->next!

=NULL)

{printf("%d,",head->d);

head=head->next;

}

printf("%d\n",head->d);

}

structnode*delst(structnode*head,int*n)

{intcount=0;

structnode*p,*q,*r;

p=r=head;

while(p!

=NULL)

{q=p->next;

while(q!

=NULL)

{if((q->d)%(p->d)==0)

{r->next=q->next;

free(q);count++;

q=r->next;

}

else

{r=q;q=q->next;}

}

p=p->next;

}

*n=count;return(head);

}

voidmain()

{inty;

structnode*head;

head=create();

print(head);

head=delst(head,&y);

print(head);

printf("%d",y);

}

11.以下程序运行时输出到屏幕的结果是(6)。

#include

voidmain()

{FILE*fp;

intk,n,a[6]={1,2,3,4,5,6};

fp=fopen("d2.dat","w");

fprintf(fp,"%d%d%d\n",a[0],a[1],a[2]);

fprintf(fp,"%d%d%d\n",a[3],a[4],a[5]);

fclose(fp);

fp=fopen("d2.dat","r");

fscanf(fp,"%d%d",&k,&n);

printf("%d,%d\n",k,n);

fclose(fp);

}

12.以下程序运行时输出到屏幕的结果中第一行是,第二行是。

#include

#definef(x,y)y=x*x

voidg(intx,inty)

{y=x*x;}

voidmain()

{inta=2,b=0,c=2,d=0;

f(a,b);

g(c,d);

printf("%d\n%d",b,d);

}

13..以下程序运行时输出到屏幕的结果第一行是,第二行是。

#include

typedefstructfact

{intm,z;

}FACT;

FACTfun1(FACTt1,FACTt2)

{FACTt3;

t3.m=t1.m*t2.m;

t3.z=t1.z*t2.m+t2.z*t1.m;

returnt3;

}

FACTfun2(FACTt)

{intm,n,k;

m=t.m;

n=t.z;

while(k=m%n)

{m=n;n=k;}

t.m=t.m/n;

t.z=t.z/n;

returnt;

}

voidmain()

{FACTs,s1={8,4},s2={6,5};

s=fun1(s1,s2);

printf("%d,%d\n",s.z,s.m);

s=fun2(s);

printf("%d,%d",s.z,s.m);

}

参考答案:

1.r+2.fscanf(fp,”%d%d”,&x,&y);3.-1.2354.20

5.136.Blue

7.第一行是1104,第二行是3380,第三行是0550

8.575

9.第一行为3,第二行为1,3,第三行为1,3,5

10.第一行为2,3,4,5,6,7,8,9,第二行为2,3,5,7,第三行为4

11.123,456

12.第一行是4,第二行是0

13.第一行是64,48,第二行是4,3

三、完善程序

1.以下程序统计数组pu中grade的每一个分值档次的人数,若程序正确,输出结果应为:

gradenembersofperson

22

12

31

源程序:

#include

typedefstruct{

intid;

intgrade;

}STUD;

voidmain()

{STUDpu[5]={{18,2},{19,2},{17,1},{21,1},{22,3}},*p;

inti,gd[5],ct[5]={0},tmp,count;

count=1;

gd[0]=

(1);

ct[0]=1;

for(p=pu+1;p

{tmp=

(2);

for(i=0;i

if(gd[i]==tmp)break;

if(i>=count)

{gd[count]=tmp;

ct[count]=1;

count++;

}

else

(3);

}

printf("gradenembersofperson");

for(i=0;i

printf("\n%2d%10d",gd[i],ct[i]);

printf("\n");

}

2.以下程序按结构成员grade的值从大到小对结构数组pu的全部元素进行排序,并输出经过排序后的pu数组全部元素的值。

#include

typedefstruct{

intid;

intgrade;

}STUD;

voidmain()

{STUDpu[10]={{1,4},{2,9},{3,1},{4,5},{5,3},{6,2},{7,8},

{8,6},{9,5},{10,2}};

(1)temp;

inti,j,k;

for(i=0;i<9;i++)

{k=i;

for(j=i+1;j<10;j++)

if(

(2))k=j;

if((3))

{temp=pu[i];pu[i]=pu[k];pu[k]=temp;}

}

for(i=0;i<10;i++)

printf("\n%2d:

%d",pu[i].id,pu[i].grade);

printf("\n");

}

3.设有一个单链表的结点定义如下:

structnode

{intd;

structnode*next;

};

函数intcopy_dellist(structnode*head,intx[])的功能为:

将head指向的单链表中存储的所有整数从小到大依次复制到x指向的整型数组中并撤销该链表;函数返回复制到x数组中的整数个数。

算法:

找出链表中数值最小的结点,将其值存储到x数组中,再将该结点从链表中删除,重复以上操作直到链表为空为止。

intcopy_dellist(structnode*head,intx[])

{structnode*pk,*pm,*pn,*pj;

intdata,k=0;

while(head!

=0)

{pk=head;data=pk->d;pn=pk;

while(

(1)!

=0)

{pj=pk->next;

if(

(2)

{data=pj->d;pm=pk;pn=pj;}

pk=pj;

}

x[k++]=pn->d;

if((3))pm->next=pn->next;

elsehead=pn->next;

free(pn);

}

;

}

4.设某链表上每个结点的数据结构为:

typedefstructnode

{intd;

structnode*next;

}NODE;

函数NODE*invert(NODE*head)的功能为:

将head指向的单链表逆置,即原链表最后一个结点变为第一个结点,原来倒数第二个结点变成第二个结点,以此类推。

在逆置过程中不建立新的链表。

NODE*invert(NODE*head)

{NODE*p,*q,*r;

if(head==0||

(1))

returnhead;

p=head;

q=p->next;

while(q!

=0)

{r=

(2);

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

}

(3)=0;

head=(4);

returnhead;

}

5.fun函数的功能是删除s指向的链表中满足以下条件的结点:

该结点的编号值是奇数且存放的字母ASCII编码值也为奇数(提示:

a的ASCII编码是97);将删除的结点添加到t所指向的链表尾部。

试完善fun函数以达到要求的功能。

例如,若删除前的s链表为:

则删除后的s链表为:

#include

structnode

{inti;/*存放结点的编号*/

charc;/*存放一个字母的ASCII编码*/

structnode*next;

};

structnode*t=NULL;

structnode*fun(structnode*s)

{structnode*p,*q;structnode*r;

p=q=s;

while(p!

=NULL)

{if(((p->i)%2)&&((p->c)%2))

{if(s==p)

s=q=

(1);

else

{

(2);

q=p->next;

}

if(t==NULL)

t=r=p;

else

{r->next=p;r=r->next;}

}

q=p;p=(3);

}

if(t!

=NULL)

(4);

returns;

}

参考答案:

1.

(1)pu[0].grade

(2)p->grade(3)ct[i]++

2.

(1)STUD

(2)pu[k].grade

=i

3.

(1)pk->next

(2)pj->d(3)pn!

=head(4)returnk

4.

(1)head->next==0

(2)q->next(3)head->next(4)p

5.

(1)p->next

(2)q->next=p->next(3)p->next(4)r->next=NULL

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 初中教育 > 数学

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1