C语言模拟练习及答案5.docx
- 文档编号:13525406
- 上传时间:2023-06-15
- 格式:DOCX
- 页数:33
- 大小:26.56KB
C语言模拟练习及答案5.docx
《C语言模拟练习及答案5.docx》由会员分享,可在线阅读,更多相关《C语言模拟练习及答案5.docx(33页珍藏版)》请在冰点文库上搜索。
C语言模拟练习及答案5
C语言模拟练习及答案5
一、判断对错(6分,每题1分,对:
√,错:
×)
1.函数的定义可以嵌套,但函数的调用不可以嵌套。
()
2.C语言程序总是从main函数第一条可执行语句开始执行,在main函数结束。
()
3.若用数组名作为函数的实参,传递给形参的是数组第一个元素的值。
()
4.C语言中,函数调用时,只有当实参与其对应的形参同名时,才共占同一个存储单元。
( )
5.凡是函数中未指定存储类别的局部变量其隐含的存储类别是自动(auto)变量。
()
6.结构体类型所占用的内存字节数是所有成员变量占用的内存字节数的总和。
()
二、单项选择题:
(10分,每题2分)
1.若有定义int(*p)[4],则标识符p是一个。
A)指向整型变量的指针变量
B)指向函数的指针变量
C)指向有四个整型元素的一维数组的指针变量
D)指针数组名,有四个元素,每个元素均为一个指向整型变量的指针
2.下列对字符串的定义中,错误的是:
。
A)charstr[7]="FORTRAN";
B)charstr[]="FORTRAN";
C)char*str="FORTRAN";
D)charstr[]={'F','O','R','T','R','A','N',0};
3.针对下面程序段,下面哪些说法是正确的?
#include
voidSwap(int*x,int*y);
main()
{
inta,b;
a=5;
b=9;
Swap(&a,&b);
printf("a=%d,b=%d",a,b);
}
voidSwap(int*x,int*y)
{
int*pTemp;
*pTemp=*x;
*x=*y;
*y=*pTemp;
}
A)程序运行结果为乱码;
B)程序运行后将导致程序崩溃;
C)程序编译时出错导致程序无法运行;
D)程序执行了危险的操作;
4.已知学生记录描述为:
structstudent
{
intno;
charname[20];
charsex;
struct
{
intyear;
charmonth[15];
intday;
}birth;
};
structstudents;
设变量s中的生日是1984年11月11日,下列对生日的正确赋值方式是_____
A)s.birth.year=1984;
s.birth.month="11";
s.birth.day=11;
B)s.birth.year=1984;
s.birth.month=11;
s.birth.day=11;
C)s.birth.year=1984;
strcpy(s.birth.month,"11");
s.birth.day=11;
D)s.birth.year=1984;
strcpy(s.birth.month,11);
s.birth.day=11;
5.要使下面程序输出1,2,34,则从键盘输入的数据格式应为。
#include
main()
{
chara,b;
intc;
scanf("%c%c%d",&a,&b,&c);
printf("%c,%c,%d\n",a,b,c);
}
A)1234B)1,2,34
C)’1’,’2’,34D)1234
三、写出下列程序的运行结果。
(10分,每题2分)
1.
#include
main()
{
inti,j,x=0;
for(i=0;i<2;i++)
{
x++;
for(j=0;j<3;j++)
{
if(j%2)break;
x++;
}
x++;
}
printf("x=%d\n",x);
}
程序运行结果是:
2.
#include
structdate
{
intyear;
intmonth;
intday;
};
structdatefunc(structdatep)
{
p.year=2000;
p.month=5;
p.day=22;
returnp;
}
main()
{
structdated;
d.year=1999;
d.month=4;
d.day=23;
printf("%d/%d/%d\n",d.year,d.month,d.day);
d=func(d);
printf("%d/%d/%d\n",d.year,d.month,d.day);
}
程序运行结果是:
3.
#include
main()
{
chara[]="Hello";
char*p=NULL;
for(p=a;p { printf("%s\n",p); } } 程序运行结果是: 4. #include intfun(intx,inty) { returnx>y? x: y; } main() { inta=2,b=5,c=8; printf("%d\n",fun(fun(a+c,b),a-c)); } 程序运行结果是: 5. #include voidFunc(void); main() { inti; for(i=0;i<5;i++) { Func(); } } voidFunc(void) { staticinta=1; intb=2,c; c=a+b; a++; b++; printf("%d\n",c); } 程序运行结果是: 四、阅读程序,在标有下划线的空白处填入适当的表达式或语句,使程序完整并符合题目要求。 (10分,每空1分) 1.先按学号由小到大的顺序从键盘输入学生的学号和成绩,然后从键盘任意输入一个学生的学号,查找并打印具有该学号的学生的成绩。 #include #defineARR_SIZE40 ; main() { floatscore[ARR_SIZE]; intn,i,pos; longnum[ARR_SIZE],x; printf("Pleaseentertotalnumber: "); scanf("%d",&n); printf("Pleaseenterthenumberandscore: \n"); for(i=0;i { scanf("%ld%f",&num[i],&score[i]); } printf("Pleaseenterthesearchingnumber: "); scanf("%ld",&x); pos=BinSearch(); if() { printf("score=%4.0f\n",score[pos]); } else { printf("Notfound! \n"); } } intBinSearch(longa[],intn,longx) { intlow,high,mid; low=0; high=n-1; while(low<=high) { mid=(high+low)/2; if(x>a[mid]) { low=; } elseif(x { high=; } else { return(); } } return(-1); } 2.输入一行字符,统计其中的英文字符、数字字符、空格及其它字符的个数。 #include #include #defineARR_SIZE80 main() { charstr[ARR_SIZE]; intlen,i,letter=0,digit=0,space=0,others=0; gets(str); for(i=0;;i++) { if() letter++; elseif() digit++; elseif() space++; else others++; } printf("Englishcharacter: %d\n",letter); printf("digitcharacter: %d\n",digit); printf("space: %d\n",space); printf("othercharacter: %d\n",others); } 五、在下面给出的4个程序中,共有18处错误(包括语法错误和逻辑错误),请找出其中的错误,并改正之。 (34分,每找对1个错误,加1分,每修改正确1个错误,再加1分。 只要找对17处错误即可,多找不加分。 ) 1.计算组合数 (m≥n) #include unsignedlongFactorial(unsignedintnumber); main() { unsignedintm,n; doublep; do{ printf("Pleaseinputm,n: "); scanf("%d,%d",&m,&n); }while(m>=n); p=(double)Factorial(m)/Factorial(n)*Factorial(m-n); printf("p=%lf\n",p); } unsignedlongFactorial(unsignedintx) { unsignedlongi,result; for(i=2;i<=x;i++) result*=i; returnresult; } 2.利用下面性质计算最大公约数: g(a,b)=g(a-b,b)a>b g(a,b)=g(a,b-a)a g(a,b)=a=ba=b #include intMaxCommonFactor(inta,intb); main() { inta,b,x; printf("Inputa,b: "); scanf("%d,%d",&a,&b); x=MaxCommonFactor(a,b); printf("MinCommonMultiple=%d\n",x); } intMaxCommonFactor(inta,intb); { if(a<=0&&b<=0) return-1; while(a! =b) { if(a>b) { returna; } elseif(b>a) { returnb; } } } 3.输入一个字符数小于100的不带空格的字符串string,然后在string所保存字符串中的每个字符间加一个空格。 #include #defineMAX=100; voidCopyString(charc[],chars[]); voidInsert(chars[]); main() { charstring[MAX]; scanf("%s",string); Insert(charstring[]); printf("%s",string); } voidCopyString(charc[],chars[]) { inti; for(i=0;s[i]! ='\0';i++) { c[i]=s[i]; } c[i]='\0'; } voidInsert(charsrcStr[]) { charstrTemp[MAX]; inti=0,j=0; CopyString(strTemp,srcStr); while(srcStr[i]! ='\0') { srcStr[i]=strTemp[j]; i++; j++; srcStr[i]=""; i++; } } 4.下面函数实现两个字符串大小的比较,函数将两个字符串中第一个出现的不相同字符的ASII码值之差作为比较的结果返回,当两个字符串完全一样时,返回值为0。 intMyStrcmp(char*p1,char*p2) { for(;*p1=*p2;p1++;p2++) { if(*p1='\0')return0; } returnp1-p2; } 六、编程(30分) 1.编程计算1! +2! +3! +……+10! 的值。 (12分) 2.编程先输入某班30个学生某门课的成绩,对全班30个学生成绩进行由高到低排序,并打印输出排序结果,要求按照如下函数原型,用函数编程实现排序功能。 (18分) voidSort(floatscore[],intn); 参考答案与评分标准 一、判断对错(6分,每题1分) 1.函数的定义可以嵌套,但函数的调用不可以嵌套。 (错) 2.C语言程序总是从main函数第一条可执行语句开始执行,在main函数结束。 (正确) 3.若用数组名作为函数的实参,传递给形参的是数组第一个元素的值。 (错) 4.C语言中,函数调用时,只有当实参与其对应的形参同名时,才共占同一个存储单元。 ( 错 ) 5.凡是函数中未指定存储类别的局部变量其隐含的存储类别是自动(auto)变量。 (正确) 6.结构体类型所占用的内存字节数是所有成员变量占用的内存字节数的总和。 (错误) 二、单项选择题: (10分,每题2分,评分标准: 正确得2分,错误扣2分) 1.若有定义int(*p)[4],则标识符p是一个。 A)指向整型变量的指针变量 B)指向函数的指针变量 √C)指向有四个整型元素的一维数组的指针变量 D)指针数组名,有四个元素,每个元素均为一个指向整型变量的指针 2.下列对字符串的定义中,错误的是: 。 √A)charstr[7]="FORTRAN"; B)charstr[]="FORTRAN"; C)char*str="FORTRAN"; D)charstr[]={'F','O','R','T','R','A','N',0}; 3.针对下面程序段,下面哪些说法是正确的? #include voidSwap(int*x,int*y); main() { inta,b; a=5; b=9; Swap(&a,&b); printf("a=%d,b=%d",a,b); } voidSwap(int*x,int*y) { int*pTemp; *pTemp=*x; *x=*y; *y=*pTemp; } A)程序运行结果为乱码; B)程序运行后将导致程序崩溃; C)程序编译时出错导致程序无法运行; √D)程序执行了危险的操作; 4.已知学生记录描述为: structstudent { intno; charname[20]; charsex; struct { intyear; charmonth[15]; intday; }birth; }; structstudents; 设变量s中的生日是1984年11月11日,下列对生日的正确赋值方式是_____ A)s.birth.year=1984;s.birth.month="11";s.birth.day=11; B)s.birth.year=1984;s.birth.month=11;s.birth.day=11; √C)s.birth.year=1984;strcpy(s.birth.month,"11");s.birth.day=11; D)s.birth.year=1984;strcpy(s.birth.month,11);s.birth.day=11; 5.要使下面程序的输出1,2,34,则从键盘输入的数据格式应为。 #include main() { chara,b; intc; scanf("%c%c%d",&a,&b,&c); printf("%c,%c,%d\n",a,b,c); } A)1234B)1,2,34 C)’1’,’2’,34√D)1234 三、写出下列程序的运行结果。 (10分,评分标准: 正确得2分,完全错误扣2分,如果部分错误,少一行或者多了一行,或者其他提示信息,则扣1分) 1. main() { inti,j,x=0; for(i=0;i<2;i++) { x++; for(j=0;j<3;j++) { if(j%2)continue; x++; } x++; } printf("x=%d\n",x); } 程序运行结果是: x=6 2. #include structdate { intyear; intmonth; intday; }; structdatefunc(structdatep) { p.year=2000; p.month=5; p.day=22; returnp; } main() { structdated; d.year=1999; d.month=4; d.day=23; printf("%d/%d/%d\n",d.year,d.month,d.day); d=func(d); printf("%d/%d/%d\n",d.year,d.month,d.day); } 程序的运行结果是: 1999/4/23 2000/5/22 3. #include main() { chara[]="Hello"; char*p=NULL;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 模拟 练习 答案