福建省C语言精选习题集.docx

上传人:b****8 文档编号:30551832 上传时间:2023-08-16 格式:DOCX 页数:25 大小:36.33KB
下载 相关 举报
福建省C语言精选习题集.docx_第1页
第1页 / 共25页
福建省C语言精选习题集.docx_第2页
第2页 / 共25页
福建省C语言精选习题集.docx_第3页
第3页 / 共25页
福建省C语言精选习题集.docx_第4页
第4页 / 共25页
福建省C语言精选习题集.docx_第5页
第5页 / 共25页
点击查看更多>>
下载资源
资源描述

福建省C语言精选习题集.docx

《福建省C语言精选习题集.docx》由会员分享,可在线阅读,更多相关《福建省C语言精选习题集.docx(25页珍藏版)》请在冰豆网上搜索。

福建省C语言精选习题集.docx

福建省C语言精选习题集

第一部分:

选择题

1.以下程序段的运行结果是(A)。

 inta=1;

printf("%d,%d,%d\n",a,++a,a++);从右往左运算

A.3,3,1B.1,2,2C.1,2,3D.3,2,1

2.以下程序段执行后p的值是(A)。

inta[3][3]={3,2,1,3,2,1,3,2,1};

intj,k,p=1;

for(j=0;j<2;j++)

for(k=j;k<4;k++)p*=a[j][k];

A.108B.18C.12D.2

3.用数组名作为函数的实参时,错误的说法是(B)。

A.定义形参数组时,元素的个数必须与实参相同

B.可以使用数组名作为形参

C.实参传递给形参的值是数组的首地址

D.可以使用指针变量作为形参

4.以下程序段的运行结果是(D)。

union

{intn;

charstr[2];

}t;

t.n=80;

t.str[0]='a';

t.str[1]=0;

printf("%d\n",t.n);

A.80

B.a

C.0

D.97

5.定义结构体类型变量teach1,不正确的是(D)。

A.structteacher

{intnum;

intage;

};

structteacherteach1;

B.structteacher

{intnum;

intage;

}teach1;

C.struct

{intnum;

intage;

}teach1;

D.struct

{intnum;

intage;

}teacher;

structteacherteach1;

6.若有定义:

structstuent

{intnum;

charsex;

intage;

}stu1;

下列叙述不正确的是(A)。

A.student是结构体类型名

B.structstudent是结构体类型名

C.stu1是用户定义的结构体类型变量名

D.num,sex,age都是结构体变量stu1的成员

7.设有如下语句:

structstu

{intnum;

intage;

};

structstus[3]={{101,18},{102,21},{103,19}};

structstu*p=s;

则下面表达式的值为102的是(B)。

A.(p++)->num

B.(*++p).num

C.(*p++).num

D.*(++p)->num

8.若有下面定义,对结构体变量成员不正确引用的语句是(B)。

structpup

{charname[20];

intage;

intsex;

}p[3],*q;

q=p;

A.scanf("%s",p[0].name);

B.scanf("%d",q->age);

C.scanf("%d",&(q->sex));

D.scanf("%d",&p[0].age);

9.错误的枚举类型定义语句是(A)。

A.enumcar{A,B,C};

B.enumcar{1,2,3};

C.enumcar{X=0,Y=5,Z=9};

D.enumcar{D=3,E,F};

10.以下程序的功能是(C)。

#include

main()

{FILE*fp;

longintn;

fp=fopen("wj.txt","rb");

fseek(fp,0,SEEK_END);

n=ftell(fp);

fclose(fp);

printf("%ld",n);

}

A.计算文件wj.txt的起始地址

B.计算文件wj.txt的终止地址

C.计算文件wj.txt的长度

D.将文件指针定位到文件末尾

11.当顺利执行了文件关闭操作时,fclose函数的返回值是(B)。

A.1

B.0

C.-1

D.一个非0值

第二部分:

改错题

1.程序Cmody051.C,其功能是统计输入字符串中大写英文字母的个数。

如输入:

abcDEFGH123

输出:

5

#include

#include

main( )

{

/**/charstr1/**/;charstr1[50]数组长度足够就行

inti,len,sum=0;

printf("Pleaseinputastring:

\n");

scanf("%s",str1);

len=strlen(str1);

for(i=0;i

{

if(str1[i]>='A'&&str1[i]<='Z')

/**/sum--;/**/sum++;

}

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

getch( );

}

2.程序Cmody062.C,其功能是将程序中的两个字符串“ABC”、“xyz”连接在一起,并输出“ABCxyz”。

#include

#include

voidmain( )

{

chars1[12]="ABC",s2[]="xyz";

char*ps1=s1,*ps2;

/**/ps2=NULL;/**/ps2=s2;

/**/while(*ps1==NULL)/**/while(*ps1!

=NULL)

ps1++;

while(*ps2)*(ps1++)=*(ps2++);

printf("%s\n",s1);

getch( );

}

3.程序Cmody122.C中函数voidchg(char*s),实现将字符串中所有偶数位置上的小写

英文字母转换为大写英文字母(不是英文字母不转换)。

#include

voidchg(char*s)

{inti,n=0;

char*p=s;

while(*p)

{n++;

p++;

}

for(i=0;i

/**/if((s[i]>='a'&&s[i]<='z')||i%2==0)/**/if((s[i]>='a'&&s[i]<='z')&&i%2==0)

s[i]=s[i]-32;

/**/s[i]='0';/**/s[i]='\0';

}

voidmain()

{

charss[100];

printf("Enterstring:

\n");

gets(ss);

chg(ss);

printf("\nNowstringis:

\n");

puts(ss);

getch();

}

第三部分:

填空题

1.补充程序Ccon013.C,程序实现从10个整数中找出最大值和最小值。

#include

#include

intmax,min;

voidfind_max_min(int*p,intn)

{

int*q;

max=min=*p;

for(q=p;q

if(/**/max<*q/**/)max=*q;

elseif(min>*q)min=*q;

}

voidmain( )

{

inti,num[10];

printf("Input10numbers:

");

for(i=0;i<10;i++)scanf("%d",&num[i]);

find_max_min(/**/num/**/,10);

printf("max=%d,num=%d\n",max,min);

}

2.补充程序Ccon023.C,该程序中可测试歌德巴赫猜想:

从键盘上输入一个大于6的偶数,总能找到两个素数,使得这两个素数之和正好等于该偶数。

#include

#include

intprime(intn)

{intk,flag=1;

for(k=2;k<=n/2+1;k++)

if(n%k==0){flag=/**/0/**/;break;}

returnflag;

}

voidmain( )

{intnum,a;

clrscr( );

do

{printf("Pleaseinputanevennumber:

");

scanf("%d",&num);

}while(num<=6||num%2==1);

for(a=2;a<=num/2+1;a++)

if(prime(a)&&prime(/**/num-a/**/))

printf("\n%d=%d+%d",num,a,num-a);

}

3.补充程序Ccon033.C,其中main函数通过调用average函数计算数组元素的平均值。

#include

floataverage(int*pa,intn)

{

intk;

/**/floatavg=0.0;/**/

for(k=0;k

avg=avg+/**/pa[k]/**/;

avg=avg/n;

returnavg;

}

voidmain( )

{inta[5]={20,30,45,64,23};

floatm;

m=average(/**/a/**/,5);

printf("Average=%f\n",m);

}

4.补充程序Ccon052.C,使程序中的sort()函数用选择法对数组a中的m个元素从小到大排序。

#include

#include

voidsort(inta[],intm)

{inti,j,k,t;

for(i=0;i

{k=i;

for(j=i+1;j

if(a[j]

if(k!

=i)

{t=a[k];

a[k]=a[i];

a[i]=/**/t/**/;

}

}

}

voidmain( )

{

inta[]={72,25,58,32,2,15,7,64};

inti,m=sizeof(a)/sizeof(int);

sort(a,m);

for(i=0;i

printf("%d",a[i]);

printf("\n");

getch( );

}

5.补充程序Ccon083.C,实现将结构体数组mystudent中存储的各学生信息按其学号的升序排列。

#include

#include

typedefstruct{

intnum;

charname[20],sex[2];

intage,score;

}STU;

STUmystudent[]={

{1111,"Zhangqiang","m",20,80},

{2104,"Liminghong","w",18,82},

{3121,"Wangxingda","m",21,78},

{4118,"Liushaotao","m",20,90},

{1456,"Wuminghong","w",35,86}

};

voidsort(STU*ps,intsize)

{

inti,flag,pass;

/**/STUtemp;/**/

for(pass=1;pass

{

flag=0;

for(i=0;i

if(/**/ps[i].num>ps[i+1].num/**/)

{

flag+=1;

temp=ps[i];ps[i]=ps[i+1];ps[i+1]=temp;

}

if(/**/flag==0/**/)break;

}

}

voidmain( )

{

inti,size=sizeof(mystudent)/sizeof(STU);

clrscr( );

printf("Students\'informationbeforesort:

\n\n");

printf("NumberNameageSexscore\n\n");

for(i=0;i

printf("%-7d%s%10d\t%s%8d\n",(mystudent+i)->num,

(mystudent+i)->name,(mystudent+i)->age,

(mystudent+i)->sex,(mystudent+i)->score);

sort(mystudent,size);

printf("\nStudents\'informationaftersort:

\n\n");

printf("NumberNameageSexscore\n\n");

for(i=0;i

printf("%-7d%s%5d\t%s%7d\n",(mystudent+i)->num,

(mystudent+i)->name,(mystudent+i)->age,

(mystudent+i)->sex,(mystudent+i)->score);

getch( );

}

6.补充程序Ccon0310.c,使其实现用递归算法求平方根。

求平方根的迭代公式如下:

#include

main( )

{

doublex,y;

/**/doublemysqrt(double,double);/**/

printf("Pleaseinputx:

\n");

scanf("%lf",&x);

y=mysqrt(x,1.0);

printf("Thesqrtof%f=%f\n",x,y);

getch( );

}

doublemysqrt(doublea,doublex0)

{

doublex1,y;

x1=/**/(x0+a/x0)/2.0;/**/

if(fabs(x1-x0)>0.00001)

y=mysqrt(/**/a,x1/**/);

else

y=x1;

return(y);

}

7.将程序Ccon123.c填写完整,函数fun()返回公式

的值,其中m、n

为整数,且

#include

doublefun(unsignedm,unsignedn)

{

unsignedi;

doubley;

/**/y=1.0;/**/

for(i=1;i<=m;i++)

y=y*i;

for(i=1;i<=n;i++)

/**/y=y/i;/**/

for(i=1;i<=m-n;i++)

y=y/i;

return/**/y/**/;

}

voidmain()

{

clrscr();

printf("fun(13,8)=%f\n",fun(13,8));

getch();

}

第四部分:

编程题

1.打开考生文件夹中的Cprog012.C,完成其中的函数fun2(inta[],intn,intb[],intc[]),实现:

(1)将数组a中大于-20的元素,依次存放到数组b中;

(2)将数组b中的元素按照从小到大的顺序存放到数组c中;

(3)函数返回数组b中的元素个数。

#include

#include

#include

#include

intfun2(inta[],intn,intb[],intc[])

{

/**/intj=0;

inti,t,k;

for(i=0;i

if(a[i]>-20){c[j]=b[j]=a[i];j++;}

for(i=0;i

for(k=i+1;k

if(c[i]>c[k])

{t=c[i];c[i]=c[k];c[k]=t;}

returnj;/**/

}

voidmain( )

{intn=10,i,nb;

intaa[10]={12,-10,-31,-18,-15,50,17,15,-20,20};

intbb[10],cc[10];

clrscr( );

printf("Thereare%2delementsinaa.\n",n);

printf("Theyare:

");

for(i=0;i

printf("\n");

nb=fun2(aa,n,bb,cc);

printf("Elementsinbbare:

");

for(i=0;i

printf("\n");

printf("Elementsinccare:

");

for(i=0;i

printf("\n");

printf("Thereare%2delementsinbb.\n",nb);

}

2.打开考生文件夹中的Cprog022.C,完成其中的函数fun2(chara[],charb[],charc[]),实现:

将三个字符串a、b、c从小到大排序后输出。

注意:

字符串比较函数为strcmp(str1,str2),字符串赋值函数为strcpy(str1,str2)。

#include

#include

#include

#include

voidfun2(chara[],charb[],charc[])

{

/**/charstr[100];

if(strcmp(a,b)>0)

{strcpy(str,a);strcpy(a,b);strcpy(b,str);}

if(strcmp(b,c)>0)

{strcpy(str,b);strcpy(b,c);strcpy(c,str);}

if(strcmp(a,b)>0)

{strcpy(str,a);strcpy(a,b);strcpy(b,str);}/**/

}

voidmain( )

{charstr1[15]="Fuzhou",str2[15]="Fujian",str3[15]="China";

clrscr( );

fun2(str1,str2,str3);

printf("Theorderedstringsis:

%s,%s,%s\n",str1,str2,str3);

getch( );

}

3.打开程序Cprog072.C,完成其中的fun( )函数,该函数将4阶矩阵A的各行中0之前的所有正数依次存放到数组b中,并返回这些正数之和。

如矩阵A为

则调用函数fun( )后,b[0]为1,b[1]为2,b[2]为23,b[3]为32,函数返回58。

#include

#defineROW4

#defineCOL4

intfun(inta[][COL],introw,intb[])

{

/**/intx=0,i,j,k=0;

for(i=0;i

for(j=0;j

if(a[i][j]>0)b[k++]=a[i][j];

elseif(a[i][j]==0)break;

for(i=0;i

x=x+b[i];

returnx;/**/

}

voidmain( )

{

intsss=0,b[16]={0};

inta[ROW][COL]={{1,2,-3,-4},{0,-12,-13,14},{-21,23,0,-24},{-31,32,-33,0}};

clrscr( );

sss=fun(a,ROW,b);

printf("Sumofpositiveelementsis%d\n",sss);

getch( );

}

4.打开程序Cprog082.C,完成其中的fun( )函数,该函数将以指针数组的形式存放的n个串升序排序。

(提示:

字符串复制函数是strcpy(char*,char*),字符串比较函数是strcmp(char*,char*))

#include

#include

voidf(charp[][20],intn);

voidmain( )

{

inti;

charp[][20]={"abc","xabdfg","abbd","dcdbe","cd"};

f(p,5);

clrscr( );

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

printf("%s\n",p[i]);

getch( );

}

voidf(charp[][20],intn)

{

/**/chart[20];

inti,j;

for(i=1;i

for(j=0;j

if(strcmp(p[j],p[j+1])>0)

{strcpy(t,p[j]);strcpy(p[j],p[j+1]);strcpy(p[j+1],t);}

/**/

}

5.打开程序Cprog092.C,完成其中的strcmp1( )函数,该函数实现判别两字符串str1和str2的大小。

#include

intstrcmp1(const

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

当前位置:首页 > 经管营销 > 人力资源管理

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

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