计算机二级c语言程序——上机考试题库80套(附答案)
第三分册 上机考试题库计算机二级c语言程序上机考试题库80套第1套一、填空题给定程序的功能是求1/4的圆周长。函数通过形参得到圆的直径,函数返回1/4的圆周长(圆周长公式为:L=d,在程序中定义的变量名要与公式的变量相同)。例如:输入圆的直径值:19.527,输出为:15.336457请勿改动主函数main与其他函数中的任何内容,仅在横线上填写所需的若干表达式或语句。#include<stdio.h>double 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",fun(z);二、改错题下列给定程序中函数fun的功能是:计算正整数m的各位上的数字之积。例如,若输入202,则输出应该是0。请修改程序中的错误,得出正确的结果注意:不要改动main函数,不能增行或删行,也不能更改程序的结构#include<stdio.h>#include<conio.h>long 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到m之内(含m)能被7或11整初的所有整数放在数组b中,通过n返回这些数的个数。例如,若传送给m的值为20,则程序输出7 11 14。请勿改动主函数main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。#include<conio.h>#include<stdio.h>#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;i<n;i+)if(i+1)%20=0) printf("%4dn",ai);else printf("%4d",ai); printf("n"); out=fopen("outfile.dat","w"); fun(100,a,&n); for(i=0;i<n;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。请勿改动主函数main与其它函数中的任何内容,仅在fun函数的横线上填写所需的若干表达式或语句。#include<stdio.h>int 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=%d sum7=%dn",test,sum5,sum6,sum7);二、改错题下列给定程序中,函数fun的功能是:将字符串str中的小写字母都改为对应的大写字母,其它字符不变。例如,若输入“asAS”,则输出“ASAS”。请修改程序中的错误,使它能统计出正确的结果。注意:不要改动 main函数,不能增行或删行,也不能更改程序的结构#include<conio.h>#include<stdio.h>#include<string.h>char*fun(char str) int i; for(i=0;stri;i+) if(stri>='a'&&stri<='z') stri-=32; return(str);main() char str81; printf("nplease enter a string:"); gets(str); printf("nThe result string is:n%s",fun(str);三、编程题请编写一个函数fun,它的功能是:找出一维整型数组元素中最小的值和它所在的下标,最小的值和它所在的下标通过形参传回。数组元素中的值已在主函数中赋予。主函数中a是数组名,n是a中的数据个数,min存放最小值,flag存放最小值所在元素的下标请勿改动主函数main与其它函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。#include<stdlib.h>#include<stdio.h>#include<string.h>void fun(int b,int n,int*min,int*d) int i; *min=b0; *d=0; for(i=0;i<n;i+) if(bi<*min) *min=bi; *d=i; main() int i,a20,min,flag,n=10; FILE*out; for(i=0;i<n;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(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<stdio.h>#include<conio.h>#define NUM 100main() int nNUM; int i=-1; int MAX=-1; printf("nInsert integer with the '-1' as end:n"); do i+;printf("n%d=",i);scanf("%d",&ni);MAX=MAX<ni?ni:MAX; while(ni!=-1); printf("The MAX=%dn",MAX);二、改错题下列给定程序中,函数fun的功能是:将一个由八进制数字字符组成的字符串转换为与其数值相等的十进制整数。例如,若输入11111,则输出将是4681。请修改程序中的错误,使它能得出正确结果。注意:不要改动main函数,不能增行或删行,也不能更改程序的结构#include<stdio.h>#include<stdlib.h>#include<string.h>int fun(char*s) 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(stri<'0'|stri>'7') printf("Error:%c not is octal digits!nn",stri); exit(0); printf("The original string:"); puts(str); n=fun(str); printf("n%s is convered to intege number:%dnn",str,n);三、编程题下列程序定义了N×N的二维数组,并在主函数中赋值。请编写函数fun,函数的功能是:求出数组周边元素的平均值并作为函数值返回给主函数中的r。例如:若c数组中的值为:C=197452383则返回主程序后r的值应为4.625000请勿改动主函数main与其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句#include<stdio.h>#include<conio.h>#include<stdlib.h>#define N 3double fun(int bN) int i,j,k=0; double r=0.0; for(j=0;j<N;j+) r+=b0j;k+; for(j=0;j<N;j+) r+=bN-1j;k+; for(i=1;i<=N-2;i+) r+=bi0;k+; for(i=1;i<=N