大一c语言期末试题及参考答案.docx

上传人:b****7 文档编号:11434956 上传时间:2023-03-01 格式:DOCX 页数:19 大小:20.03KB
下载 相关 举报
大一c语言期末试题及参考答案.docx_第1页
第1页 / 共19页
大一c语言期末试题及参考答案.docx_第2页
第2页 / 共19页
大一c语言期末试题及参考答案.docx_第3页
第3页 / 共19页
大一c语言期末试题及参考答案.docx_第4页
第4页 / 共19页
大一c语言期末试题及参考答案.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

大一c语言期末试题及参考答案.docx

《大一c语言期末试题及参考答案.docx》由会员分享,可在线阅读,更多相关《大一c语言期末试题及参考答案.docx(19页珍藏版)》请在冰豆网上搜索。

大一c语言期末试题及参考答案.docx

大一c语言期末试题及参考答案

2004级信息学院《C语言设计》考试试题

一、判断下列语句或程序的对错。

10分√

1intx=y=z=’0’;(×)y,z没有定义

2#include;(×)不能有分号,#开头的结尾均不能有分号;

3printf(“%s\n”,”clanguage”);(√)

4floata[100];

int*p=a;(×)数据类型不匹配

5charstr[20];

6intdata[4]={0,1,2,3,4};(×)五个元素,但是只有四个单元

7floatx=1.45e+310L;(×)数值越界

8intxyz-1=2;(×)

9intx=‘\xae’;(√)

10int*p,a[2][3];

p=a;(×)数据类型不匹配

 

二计算下列表达式的值10分

设unsignedinta=10,b=17,c=5,d=3;

floatf;

(1)f=b/c(3.0)

(2)!

(a+b)+c-1&&b+c/2

(1)

(3)(a^b)+(c>>1+d)(0x1b)

(4)a+=b%=a=b(17)

(5)a=2,b=a*++b

(2)

三程序改错10分

(1)求两个浮点数的平方和及平方差

#include

floatcalculate(floatx,floaty,float*sub);添加函数原型声明

main()

{

floata,b;

floatadd_reasult,sub_result;

scanf(“%f,%f”,a,b);

add_result=calculate(a,b,&sub_result);

printf(“a*a+b*b=%d,a*a-b*b=%d\n”,add_result,sub_result);

}

floatcalculate(floatx,floaty,float*sub)添加函数类型

{

float*temp;应该直接定义为变量floattemp;

sub=a*a-b*b;*sub=a*a-b*b;

temp=a*a+b*b;

return*temp;returntemp

}

(2)统计N个字符中大写字母和数字字符的个数

#include

#defineN5

Count(char*str,int*result);添加函数声明

main()

{

charstring[N][80];

chari;

intCapital_Count=0,Num_Count=0;需要初始化为0

for(i=0;i

scanf(“%s”,&string[i]);去掉&符

for(I=0;I

Capital_Count+=Count(string[I],&Num_Count);

Printf(“Capitalcount:

=%d,numbercount=%d\n”

Capital_Count,Num_Count);

}

Count(char*str,int*result)

{

inttemp,I;inttemp=0,i;temp应该初始化为0

for(I=0;I<80;I++)

{

If(str[I]>=’A’&&str[I]<=’Z’)

Temp++;

If(str[I]>’0’||str[I]<’9’)

*result++;

}

returntemp;

}

四程序填空10分

(答案参考书中p85~86)

(1)利用公式sinx=x-x

/3!

+x

x=0.5,n=20

#include

main()

{

floaty,s,x,d,t;

intn,I,j;

scanf(“%d%f”,&n,&x);

s=1.0;

____________________________;

for(I=2;I

{

d=t=__________________________;

for(j=1;_______________;j++)

{

d=________________;

t=________________;

}

s=(-1)*s;

y+=_____________________;

}

(2)利用库函数char*strstr(char*sl,char*s2)在给定字符串中查找子串最后(最右)一次出现的位置。

如果S2并没有出现在S1的任何地方,函数返回一个NULL

指针。

如果第二个参数是一个空字符串,函数就返回S1;

注:

库函数charstrstr(char*s1,char*s2),这个函数在S1中查找子字符串S2第一次出现的起始位置,并返回一个指向该位置的指针。

如果S2并没有出现在S1的任何地方,函数返回一个NULL指针。

如果第二个参数是一个空字符串,函数返回S1;

(答案见书中p196~197)

#include

#include

voidmain(void)

{

charstr[80]=”ABCdabcdfgabc”;

char*p;

p=my_strrstr(str,”abc”);

printf(“%s\n”,p);

p=my_strrstr(str,”“);

printf(“%s\n”,p);

}

char*my_strrstr(char*s1,char*s2)

{

char*last;

char*current;

_________________________;

if(________________________)

{

last=current=_____________;

While(______);

{

last=current;

current=_______;

}

}

returnlast;

}

五.写输出结果(20分)

(1)

#include

voidfun(int*,int);

voidmain()

{

inta[]={5,6,7,8},i;

fun(a,4);

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

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

}

voidfun(int*b,intn)

{

inti;

for(i=0;i

b[i]=i*2;

return;

}

0

2

4

6

(2)

#include

voidmain()

{

inti,j,max;

introw=0,column=0;

inta[3][3]={{1,2,3},{2,-3,4},{9,4,7}};

max=a[0][0];

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

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

{

if(a[i][j]>max)

{

max=a[i][j];

row=i+1;

column=j+1;

}

}

printf("max=%d,row=%d,column=%d\n",max,row,column);

}

(书中例题5.5,p123)

max=9,row=3,column=1

(3)

#include

intn=1;

voidfunc();

voidmain()

{

staticintx=5;

inty;

y=n;

printf("main:

x=%d,y=%d,n=%d\n",x,y,n);

func();

printf("main:

x=%d,y=%d,n=%d\n",x,y,n);

}

voidfunc()

{

staticintx=4;

inty=10;

x=x+2;

n=n+2;

y=y+n;

printf("func:

x=%d,y=%d,n=%d\n",x,y,n);

}

main:

x=5,y=1,n=1

func:

x=6,y=13,n=3

main:

x=5,y=1,n=3

(4)

#include

#include

structperson

{

charname[20];

intcount;

};

voidmain()

{

structpersonleader[3]={{"li",0},{"zhang",0},{"wang",0}};

charname[20],m,n;

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

{

scanf("%s",name);

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

if(!

strcmp(name,leader[n].name))

{

leader[n].count++;

break;

}

}

printf("\n");

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

printf("%s:

%d\n",leader[m].name,leader[m].count);

}

当程序运行时,键入情况如下:

Li

Wang

Lei

Li

Wang

Zhang

写出程序的输出结果。

(基本上是书中例题)

Li:

2

Zhang:

1

Wang:

2

(5)

#include

#include

voidmain()

{

char*name[]={"capital","index","large","small"};

inta,b,n=4;

char*temp;

for(a=0;a

for(b=a+1;b

{

if(strcmp(name[a],name[b])>0)

{

temp=name[a];

name[a]=name[b];

name[b]=temp;

}

}在此之前是书中的例题7.19

for(a=0;a

printf("%s\n",name[a]+a);输出时应该能够识别指针及偏移情况

}

capital

ndex

rge

ll

 

六、编写程序(35分)

(1)求一元二次方程ax

+bx+c=0的根,实系数a,b,c从终端输入,只考虑两个不同实根和两个相同的实根(9分)

(书中例题3.4,p66`67)

#include

#include

voidmain()

{

floata,b,c;

floatx1,x2;

floatx3,x4;

floatm;

printf("inputthenumbers:

a,b,c");

scanf("%f%f%f",&a,&b,&c);

if(a==0)

{

printf("theinputiserror!

\n");

return;

}

m=b*b-4*a*c;

if(m>0)

{

x1=(-b+sqrt(m))/(2*a);

x2=(-b-sqrt(m))/(2*a);

printf("x1:

%.2fx2:

%.2f\n",x1,x2);

}

elseif(m==0)

{

x1=x2=(-b+sqrt(m))/(2*a);

printf("x1=x2=%.2f\n",x1);

}

else

{

x3=-b/(2*a);

x4=sqrt(m)/(2*a);

printf("x1=%.2f+%.2fi\n",x3,x4);

printf("x2=%.2f-%.2fi\n",x3,x4);

}

}

(2)编写一个函数,求s=a+aa+aaa+--------+aaaaaaaaa-----a,其中a是一个数字,例如2+22+222+2222(此时n=4)。

主函数a和n的输入,调用所函数和输出所求的累加和;编写的函数完成计算。

(9分)

注意:

不得使用全局变量,注意程序结构

(书中习题3:

4。

16)

#include

#include

longCal(longa,longn);

main()

{

longsn=0;

longa,n;

printf("pleaseinputan:

");

scanf("%d%d",&a,&n);

sn=Cal(a,n);

printf("a+aa+aaa+...+aa...a=%ld\n\n",sn);

}

longintCal(longa,longn)

{

inti;

longsn=0,m=0;

for(i=0;i

{

m=m*10+a;

sn+=m;

}

returnsn;

}

(3)从十个字符串中找出所要的某个字符串,若能找到则把他删除,然后输出新字符串;若未找到则输出“”cannotfond”.(9分)

#include

#include

main()

{

chars[10][80];

chars2[80];

inti,j;

intnum=0;

printf("pleaseenter10string:

\n");

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

gets(s[i]);

printf("pleaseenters2string:

");

gets(s2);

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

{

if(strcmp(s[i],s2)==0)

{

for(j=i;j<9-num;j++)

{

strcpy(s[j],s[j+1]);

}

i--;

num++;

}

}

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

puts(s[i]);

}

 

(4)一个班有N个同学,修5门课从键盘输入他们的性名、学号、性别和成绩。

1)按平均成绩从高到底打印全班的成绩单。

2)求第三门课的平均分

3)找出平均分在90以上或全部功课在85以上的女生。

要求:

输入、输出、计算、排序和查找分别用函数实现,主函数只是调用这些函数。

不得使用全局变量。

#include

#defineN5

structStudent

{

charname[20];

intnumber;

intsex;

floatscore[5];

floataver;

};

voidInput(structStudent*stu);

voidAverage(structStudent*stu,float*aver);

voidSort(structStudent*stu);

voidSearch(structStudent*stu,floatscore);

main()

{

structStudentstu[N];

floatscore=85.0;

floataver3;

Input(stu);

Sort(stu);

Average(stu,&aver3);

printf("average3is%.2f\n",aver3);

Search(stu,score);

}

voidInput(structStudent*stu)

{

inti,j;

floataver=0;

for(i=0;i

{

aver=0;

printf("pleaseentername:

");

gets(stu[i].name);

printf("pleaseenternumbersex:

");

scanf("%d%d",&stu[i].number,&stu[i].sex);

printf("pleaseenterscore(5):

");

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

{

scanf("%f",&stu[i].score[j]);

aver+=stu[i].score[j];

}

stu[i].aver=aver/5;

getchar();

}

}

voidSort(structStudent*stu)

{

inti,j;

structStudenttemp;

for(i=0;i

{

for(j=i+1;j

{

if(stu[i].aver

{

temp=stu[i];

stu[i]=stu[j];

stu[j]=temp;

}

}

}

printf("NameNumbSexscore(5)aver\n");

for(i=0;i

{

printf("%8s%d%d%.2f%.2f%.2f%.2f%.2f%.2f\n",

stu[i].name,

stu[i].number,

stu[i].sex,

stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],stu[i].score[4],

stu[i].aver);

}

}

voidAverage(structStudent*stu,float*average3)

{

floatsum3=0;

inti;

for(i=0;i

sum3+=stu[i].score[2];

*average3=sum3/N;

}

voidSearch(structStudent*stu,floatscore)

{

inti,j;

intflag=1;

printf("\naver>90score>85\n");

printf("NameNumbSexscore(5)aver\n");

for(i=0;i

{

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

flag=flag&&(stu[i].score[j]>score);

if(stu[i].aver>90.0&&flag)

{

printf("%8s%d%d%.2f%.2f%.2f%.2f%.2f%.2f\n",

stu[i].name,

stu[i].number,

stu[i].sex,

stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],stu[i].score[4],

stu[i].aver);

}

}

printf("\n\n");

}

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

当前位置:首页 > 求职职场 > 简历

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

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