[IT认证]计算机二级c++上机考试题库80题答案.doc

上传人:音乐台 文档编号:1964358 上传时间:2019-01-27 格式:DOC 页数:200 大小:524.50KB
返回 下载 相关 举报
[IT认证]计算机二级c++上机考试题库80题答案.doc_第1页
第1页 / 共200页
[IT认证]计算机二级c++上机考试题库80题答案.doc_第2页
第2页 / 共200页
[IT认证]计算机二级c++上机考试题库80题答案.doc_第3页
第3页 / 共200页
亲,该文档总共200页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《[IT认证]计算机二级c++上机考试题库80题答案.doc》由会员分享,可在线阅读,更多相关《[IT认证]计算机二级c++上机考试题库80题答案.doc(200页珍藏版)》请在三一文库上搜索。

1、第三分册 上机考试题库第1套 一、填空题给定程序的功能是求1/4的圆周长。函数通过形参得到圆的直径,函数返回1/4的圆周长(圆周长公式为:L=d,在程序中定义的变量名要与公式的变量相同)。例如:输入圆的直径值:19.527,输出为:15.336457请勿改动主函数main与其他函数中的任何内容,仅在横线上填写所需的若干表达式或语句。#includedouble fun(double d) return 3.14159*d/4.0;main() double z; printf(Input the d of the round:); scanf(%lf,&z); printf(L=%lfn,fu

2、n(z);二、改错题下列给定程序中函数fun的功能是:计算正整数m的各位上的数字之积。例如,若输入202,则输出应该是0。请修改程序中的错误,得出正确的结果注意:不要改动main函数,不能增行或删行,也不能更改程序的结构#include#includelong fun(long n) long r=1; do r*=n%10;n/=10; while(n); return(r);main() long m; printf(nplease enter a number:); scanf(%ld,&m); printf(n%ldn,fun(m);三、编程题请编写一个函数fun,它的功能是:求出1到

3、m之内(含m)能被7或11整初的所有整数放在数组b中,通过n返回这些数的个数。例如,若传送给m的值为20,则程序输出7 11 14。请勿改动主函数main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。#include#include#define N 100void fun(int m,int*b,int*n) int i,j=0;*n=0; for(i=1;i=m;i+)if(i%7=0|i%11=0) bj=i; j+; *n=j;main() int aN,n,i; FILE*out; fun(20,a,&n); for(i=0;in;i+)if(i+1)%20=

4、0) printf(%4dn,ai);else printf(%4d,ai); printf(n); out=fopen(outfile.dat,w); fun(100,a,&n); for(i=0;in;i+) if(i+1)%10=0) fprintf(out,%4dn,ai);else fprintf(out,%4d,ai); fclose(out);第2套一、填空题函数fun的功能是:统计长整数test的各位上出现数字5、6、7的次数,并通过外部(全局)变量sum5、sum6、sum7返回主函数。例如:当test=89431676时,结果应该为:sum5=0 sum6=2 sum7=1

5、。请勿改动主函数main与其它函数中的任何内容,仅在fun函数的横线上填写所需的若干表达式或语句。#includeint sum5,sum6,sum7;void fun(long test) sum5=sum6=sum7=0; while(test) switch(test%10)case 5:sum5+;break;case 6:sum6+;break;case 7:sum7+;test/=10; main() long test=89431676L; fun(test); printf(nThe count result:n); printf(test=%ld sum5=%d sum6=%

6、d sum7=%dn,test,sum5,sum6,sum7);二、改错题下列给定程序中,函数fun的功能是:将字符串str中的小写字母都改为对应的大写字母,其它字符不变。例如,若输入“asAS”,则输出“ASAS”。请修改程序中的错误,使它能统计出正确的结果。注意:不要改动main函数,不能增行或删行,也不能更改程序的结构#include#include#includechar*fun(char str) int i; for(i=0;stri;i+) if(stri=a&stri=z) stri-=32; return(str);main() char str81; printf(nple

7、ase enter a string:); gets(str); printf(nThe result string is:n%s,fun(str);三、编程题请编写一个函数fun,它的功能是:找出一维整型数组元素中最小的值和它所在的下标,最小的值和它所在的下标通过形参传回。数组元素中的值已在主函数中赋予。主函数中a是数组名,n是a中的数据个数,min存放最小值,flag存放最小值所在元素的下标请勿改动主函数main与其它函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。#include#include#includevoid fun(int b,int n,int*min,in

8、t*d) int i; *min=b0; *d=0; for(i=0;in;i+) if(bi*min) *min=bi; *d=i; main() int i,a20,min,flag,n=10; FILE*out; for(i=0;in;i+) ai=rand()%50;printf(%4d,ai); printf(n); fun(a,n,&min,&flag); printf(min=%5d,Index=%4dn,min,flag); out=fopen(outflie.dat,w); memcpy(a,3.141592653589793238462643383279,32); fun(

9、a,8,&min,&flag); fprintf(out,min=%5d,Index=%4d,min,flag); fclose(out);第3套 上机考试试题一、填空题请补充main函数,该函数的功能是:从键盘输入一组整数,使用条件表达式找出最大的整数。当输入的整数为-1时结束。例如:输入96 121 23 343 232 54 89 365 89 -1时,最大的数为365。仅在横线上填写所需的若干表达式或语句,请勿改动函数中的其它任何内容#include#include#define NUM 100main() int nNUM; int i=-1; int MAX=-1; printf(

10、nInsert integer with the -1 as end:n); do i+;printf(n%d=,i);scanf(%d,&ni);MAX=MAXni?ni:MAX; while(ni!=-1); printf(The MAX=%dn,MAX);二、改错题下列给定程序中,函数fun的功能是:将一个由八进制数字字符组成的字符串转换为与其数值相等的十进制整数。例如,若输入11111,则输出将是4681。请修改程序中的错误,使它能得出正确结果。注意:不要改动main函数,不能增行或删行,也不能更改程序的结构#include#include#includeint fun(char*s)

11、 int n; n=*s-0; s+; while(*s!=0) n=n*8+*s-0;s+; return n;main() char str6; int i; int n; printf(Enter a string(octal digits):); gets(str); if(strlen(str)5) printf(Error:string too longer!nn);exit(0); for(i=0;stri;i+) if(stri7) printf(Error:%c not is octal digits!nn,stri); exit(0); printf(The origina

12、l string:); puts(str); n=fun(str); printf(n%s is convered to intege number:%dnn,str,n);三、编程题下列程序定义了NN的二维数组,并在主函数中赋值。请编写函数fun,函数的功能是:求出数组周边元素的平均值并作为函数值返回给主函数中的r。例如:若c数组中的值为:C=197452383则返回主程序后r的值应为4.625000请勿改动主函数main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句#include#include#include#define N 3double fun(int bN

13、) int i,j,k=0; double r=0.0; for(j=0;jN;j+) r+=b0j;k+; for(j=0;jN;j+) r+=bN-1j;k+; for(i=1;i=N-2;i+) r+=bi0;k+; for(i=1;i=N-2;i+) r+=biN-1;k+; return r/=k;main() int cNN=1,9,7,4,5,2,3,8,3; int i,j; FILE*out; double r; printf(*n); for(i=0;iN;i+) for(j=0;jN;j+) printf(%4d,cij);printf(n); r=fun(c); pri

14、ntf(THE RESULTn); printf(The r is %lfn,r); out=fopen(outfile.dat,w); fprintf(out,%lf,r); fclose(out);第4套 上机考试试题一、填空题请补充fun函数,该函数的功能是将字符串str中的小写字母都改为对应的大写字母,其它字符不变。例如:若输入”Welcome!”,程序输出结果是”WELCOME!”。请勿改动主函数main与其它函数中的任何内容,仅在fun函数的横线上填写所需的若干表达式或语句。#include#include#includechar*fun(char str) int j; for(

15、j=0;strj;j+) if(strj=a)&(strj=z)strj-=32; return(str);main() char str100; printf(nplease enter a string:); gets(str); printf(nThe result string is:n%s,fun(str);二、改错题下列给定程序中,函数fun的功能是:计算并输出n以内最大的10个能被11或19整除的自然数之和。n的值由主函数传入,若n的值为300,则函数值为2646。请修改程序中的错误或在横线处填上适当的内容并把横线删除,使程序能得出正确的结果。注意:不要改动main函数,不能增行

16、或删行,也不能更改程序的结构。#include#includeint fun(int n) int m=0,mix=0; while(n=2)&(mix10) if(n%11=0)|(n%19)=0) m=m+n; mix+;n-; return m;main() printf(%dn,fun(300);三、编程题请编写函数fun,其功能是:将str所指字符串中除了下标为偶数、同时ASCII值也为偶数的字符外,其余的全部删除;串中剩余字符所形成的一个新串放在s所指的数组中。例如,若str所指字符串中的内容为ABCDEFG123456,其中字符A的ASCII码值为奇数,因此应当删除;其中字符B的

17、ASCII值为偶数,但在数组中的下标为奇数,因此也应当删除;而字符2的ASCII码值为偶数,所在数组中的下标也为偶数,因此不应当删除,其它以此类推。最后s所指的数组中的内容应是246。请勿改动主函数main与其它函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。注意:部分源程序给出如下。#include#include#includevoid fun(char*str,char s) int i,j=0,n; n=strlen(str); for(i=0;in;i+) if(i%2=0&stri%2=0) sj=stri; j+; sj=0;main() char str100,

18、s100; FILE*out; printf(nplease enter string:); scanf(%s,str); fun(str,s); printf(nThe result is:%sn,s); out=fopen(outfile.dat,w); strcpy(str,please enter string:); fun(str,s); fprintf(out,%s,s); fclose(out);第5套 上机考试试题一、填空题请补充fun函数,该函数的功能是:依次取出字符串中所有大写字母,形成新的字符串,并取代原字符串。例如,输入sdfASDsd,则输出ASD。请勿改动主函数ma

19、in与其它函数中的任何内容,仅在fun函数的横线上填写所需的若干表达式或语句。注意:部分源程序给出如下。#include#includevoid fun(char*s) int j=0; char*p=s; while(*p) if(*p=A&*p=Z) sj=*p; j+;p+; sj=0;main() char str100; printf(nPlease Input a string:); gets(str); printf(nnThe original string is:%sn,str); fun(str); printf(nnThe string of changing is:%s

20、n,str);二、改错题下列给定程序中,函数fun的功能是:先从键盘上输入一个3行3列矩阵的各个元素的值,然后输出主对角线元素之和。请修改函数fun中的错误或在横线处填上适当的内容并把横线删除,得出正确结果。注意:不要改动main函数,不能增行或删行,也不能更改程序的结构。#includevoid fun() int aa33,sum; int i,j; sum=0; for(i=0;i3;i+) for(j=0;j3;j+) scanf(%d,&aaij); for(i=0;i3;i+)sum=sum+aaii; printf(sum=%dn,sum);main() fun();三、编程题请

21、编写一个函数void fun(int*s,int t,int*result),用来求出数组的最小元素在数组中的下标,并存放在result所指的存储单元中。例如,输入如下整数:564,165,567,121,948,324,329,454,5345,783,434,124,561,985,555则输出结果为:3,121。请勿改动主函数main与其它函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。注意:部分源程序给出如下。#include#includevoid fun(int*s,int t,int*result) int temp,min; min=s0; for(temp=0

22、;tempt;temp+) if(stempmin) min=stemp; *result=temp; main() int store15=564,165,567,121,948,324,329,454,5345,783,434,124,561,985,555,result; FILE*out; fun(store,10,&result); printf(%d,%dn,result,storeresult); out=fopen(outfile.dat,w); fprintf(out,%dn%d,result,storeresult); fclose(out);第6套 上机考试试题一、填空题

23、给定程序的功能是判断字符串s中的某个字符是否与字符ch相同,若相同什么也不做,若不同则插在字符串的最后。例如,输入test,如果输入e,输出的结果不变,但如果输入a,结果testa。请勿改动主函数main与其它函数中的任何内容,仅在横线上填写所需的若干表达式或语句。注意:部分源程序给出如下。#include#includevoid fun(char*s,char ch) while(*s&*s!=ch) s+; if(*s!=ch) s0=ch;s1=0; main() char str81,c; printf(nPlease input a string:n); gets(str); pri

24、ntf(n Please enter the character to search:); c=getchar(); fun(str,c); printf(nThe result is %sn,str);二、改错题下列给定程序中,函数fun的功能是:按顺序给t所指数组中的元素赋予从2开始的偶数。然后再按顺序对每5个元素求一个平均值,并将这些值依次存放在r所指的数组中。若t所指数组中元素的个数不是5的倍数,多余部分忽略不计。例如,t所指数组有14个元素,则只对前10个元素进行处理,不对最后的4个元素求平均值。请修改程序中的错误,得出正确的结果。注意:不要改动main函数,不能增行或删行,也不能更

25、改程序的结构。#include#define NUM 20int fun(double*t,double*r) int j,i; double sum; for(j=2,i=0;iNUM;i+) ti=j;j+=2; sum=0.0; for(j=0,i=0;iNUM;i+) sum+=ti;if(i+1)%5=0) rj=sum/5; sum=0; j+; return j;main() double aNUM,bNUM/5; int i,j; j=fun(a,b); printf(The original data:n); for(i=0;iNUM;i+) if(i%5=0)printf(

26、n);printf(%4.0f,ai); printf(nnThe result:n); for(i=0;ij;i+) printf(%6.2f,bi); printf(nn);三、编程题请编写一个函数void fun(int x,int sum,int select),该函数的功能是:将大于整数x且紧靠x的sum个素数存入select所指的数组中。例如,输入:121 8,则应输出:127 131 137 139 149 151 157 163。请勿改动主函数main与其它函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。注意:部分源程序给出如下。#include#include

27、void fun(int x,int sum,int select) int r=0,temp,p,sign=1; for(temp=x+1;tempx*x;temp+) for(p=2;p=temp) if(sum=0) selectr+=temp;sum-; else break; main() int x,y,a500; FILE*out; printf(nInput two number:); scanf(%d,%d,&x,&y); fun(x,y,a); for(x=0;xy;x+) printf(%d ,ax); printf(n); fun(121,10,a); out=fope

28、n(outfile.dat,w); for(x=0;x10;x+) fprintf(out,%dn,ax); fclose(out);第7套 上机考试试题一、填空题给定程序的功能是计算score中n个人的平均成绩aver,将高于aver的成绩放在high中,通过函数名返回人数。例如,score=88,75,50,60,80,90,n=6时,函数返回的人数应该是4,high=88,75,80,90。请勿改动主函数main与其他函数中的任何内容,仅在横线上填写所需的若干表达式或语句。注意:部分源程序给出如下。#include#includeint fun(int score,int m,int h

29、igh) int i,j=0; float aver=0.0; for(i=0;im;i+) aver+=scorei; aver/=(float)m; for(i=0;iaver) highj+=_; return j;main() int i,n,high6; int score6=88,75,50,60,80,90; n=fun(score,6,_); printf(nThe high of average score are:); for(i=0;in;i+) printf(%d ,_); printf(n);二、改错题已知一个数列从第0项开始的前三项分别为0、0、1,以后的各项都是其

30、相邻的前三项之和。下列给定程序中,函数fun的功能是:计算并输出该数列前n项的平方根之和sum。n的值通过形参传入。例如,当n=4时,程序输出结果应为2.000000。请修改程序中的错误,使程序能得出正确的结果。注意:不要改动main函数,不能增行或删行,也不能更改程序的结构。#include#include#includedouble fun(int n) double sum,a0,a1,a2,a; int i; sum=1.0; if(n=2) sum=0.0; a0=0.0; a1=0.0; a2=1.0; for(i=4;i=n;i+) a=a0+a1+a2;sum+=sqrt(a)

31、;a0=a1;a1=a2;a2=a; return sum;main() int n; printf(Input N=); scanf(%d,&n); printf(%lfn,fun(n);三、编程题学生的记录由学号和成绩组成,M名学生的数据已在主函数中放入结构体数组score中,请编写函数fun,它的功能是把分数最低的学生数据放在low所指的数组中,注意:分数最低的学生可能不只一个,函数返回分数最低的学生的人数。请勿改动主函数main与其它函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。注意:部分源程序给出如下。#include#define M 10typedef stru

32、ct char num10; int s;SCORE;int fun(SCORE*a,SCORE*b) int i,j=0,n=0,min; min=a0.s; for(i=0;iM;i+) if(ai.smin) min=ai.s; for(i=0;iM;i+) if(ai.s=min) *(b+j)=ai;j+;n+; return n;main() SCORE stuM=GA03,76,GA04,85,GA01,91,GA08,64,GA06,87,GA014,91,GA011,77,GA017,64,GA018,64,GA016,72; SCORE lowM; int i,n; FIL

33、E*out; n=fun(stu,low); printf(The %d lowest score:n,n); for(i=0;in;i+) printf(%s %4dn,lowi.num,lowi.s); printf(n); out=fopen(outfile.dat,w); fprintf(out,%dn,n); for(i=0;in;i+) fprintf(out,%4dn,lowi.s); fclose(out);第8套 上机考试试题一、填空题请补充main函数,该函数的功能是:从键盘输入3个整数,然后找出最小的数并输出。例如:输入78,53,123,则输出为53。请勿改动主函数ma

34、in与其它函数中的任何内容,仅在横线上填写所需的若干表达式或语句。注意:部分源程序给出如下。#include#includemain() int x,y,z,min; printf(nInput three number:n); scanf(%d,%d,%d,&x,&y,&z); printf(The three numbers are:%d,%d,%dn,x,y,z); if(xz) min=z; printf(min=%dn,min);二、改错题下列给定程序中,fun函数的功能是:分别统计字符串中小写字母和大写字母的个数。例如,给字符串t输入:adfsFFssefSCGSDew,则应输出结

35、果:big=7,small=10。请修改程序中的错误,使它能计算出正确的结果。注意:不要改动main函数,不能增行或删行,也不能更改程序的结构。#include#includevoid fun(char*t,int*c,int*d) while(*t) if(*t=A&*t=a&*t=z)(*d)+;t+; main() char t200; int big=0,small=0; printf(nplease input a string:); gets(t); fun(t,&big,&small); printf(n big=%d small=%dn,big,small);三、编程题请编一个

36、函数void fun(int aMN,int bN),c指向一个M行N列的二维数组,求出二维数组每列中最大元素,并依次放入b所指一维数组中。二维数组中的数己在主函数中赋予。请勿改动主函数main与其它函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。注意:部分源程序给出如下。#include#include#define M 3#define N 4void fun(int aMN,int bN) int i,j,max; for(j=0;jN;j+) max=a0j;for(i=0;imax) max=aij;bj=max; main() int cMN=10,22,15,30

37、,19,33,45,38,20,22,66,40; int pN,i,j,k; FILE*out; printf(The original data is:n); for(i=0;iM;i+) for(j=0;jN;j+)printf(%6d,cij); printf(n); fun(c,p); printf(nThe result is:n); for(k=0;kN;k+) printf(%4d,pk); printf(n); out=fopen(outfile.dat,w); for(k=0;kN;k+) fprintf(out,%dn,pk); fclose(out);第9套 上机考试试题一、填空题请补充fun函数,该函

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

当前位置:首页 > 其他


经营许可证编号:宁ICP备18001539号-1