《二级C语言题库2.docx》由会员分享,可在线阅读,更多相关《二级C语言题库2.docx(69页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、二级C语言题库第21套题一、程序填空题#include <stdio.h>int fun(int x) int n, si, s2, s3, t;/*lound*/n=_1_;t=100;/* * *found */whUe(t<=_2_) sl=t%10; s2=(t/10)%10; s3=t/100; if(sl+s2+s3=15) printf("%d ",t);n+;/*found*/_3_;)return n;)main() int x=-l;while(x>999llx<0) printf("Please input(0&
2、lt;x<=999): "); scanf("%d",&x); printf("nThe result is: %dn",fun(x);二、程序改错题#include <string.h>void fun (char *s, char *t) /* * * found */int i;si = strlen(s);for (i=0; i<sl; i+)/* * *nd */ti = ssl-i;for (i=0; i<sl; i+)tsl+i = si;t2*sl = 40 main() chars100,
3、t100;printf("nPlease enter string s:"); scanf("%s", s); fun(s, t); printf("The result is: %sn", t);三、程序编写题#include <stdio.h>void fun(int a, int b, long *c) main() /* 主函数 */ int a,b; long c;void NONO ();printf("Input a b:");scanf("%d%d", &a,
4、 &b);fun(a, b, &c);printf("The result is: %ldn", c);NONO();void NONO ()/*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。*/ FILE *rf,*wf; int i, a,b ; long c ;rf = fopen("K:k0124010001in.dat","r");wf = fopen("K:kO 1W24010001 out.dat","w");for(i = 0 ; i < 10
5、 ; i+) fscanf(rf, "%d,%d", &a, &b);fun(a, b, &c);fprintf(wf, "a=%d,b=%d,c=%ldn", a, b, c);)fclose(rf);fclose(wf);第22套题一、程序填空题#include <stdio.h> int fun(int x) int n, si, s2, s3, t;n=0;t=100;/*found*不/while(t<=I)/*found* */sl=t%l 0; s2=(_2_)% 10; s3=t/100;/*fo
6、und*/ if(sl+s2+s3=_3_) printf("%d ",t); n+;t+;return n;main() int x=-l;while(x<0) printf("Please input(x>O): "); scanf("%d",&x); printf("nThe result is: %dn",fun(x);二、程序改错题#include <stdio.h>/* * *found */ void fun (long s, long t) long sl=10;s/
7、= 10;*t = s % 10;/*ound*/while ( s < 0) s = s/100;*t = s%10*sl + *t;si = si * 10;main() long s, t;printf("nPlease enter s:"); scanf("%ld", &s); fun(s, &t); printf("The result is: %ldn", t);三、程序编写题#include <stdio.h>#define N 16 typedef struct char num10;
8、 int s;STREC;void fun( STREC a)main()STRECsN= "GA005",85, "GA003",76, "GA002",69," GA004",85,"GA001",91,"GA007",72,"GA008",64,"GA006 ",87),"GA015",85,"GA013",91,"GA012",64,"GA014 "
9、,91),"GA011 ",66, "GA017",64,"GA018",64, "GA016",72; int i;FILE *out;fun( s);printf("The data after sorted :n");for(i=0;i<N; i+) if( (i)%4=0 )printf("n");printf("%s %4d ",si.num,si.s);printf("n");out = fopen("K:k
10、O 1W24010001 out.dat","w");for(i=0;i<N; i+) if( (i)%4=0 && i) fprintf(out, "n");fprintf(out, "%4d ",si.s);fprintf(out,"n");fclose(out);第23套题一、程序填空题#include <stdio.h>unsigned long fun(unsigned long n) unsigned long x=0, s, i; int t;s=n;/*
11、* * found * 不/i=_l_;/*found*/while(_2_) t=s%10;if(t%2=0)/* * found */x=x+l*i; i=_3_; s=s/10; return x;main() unsigned long n=-l;while(n>99999999lln<0)printf("Pleaseinput(O<n<l 00000000):");scanf("%ld",&n); printf("nThe result is: %ldn",fun(n);1二、程序改错题#in
12、clude <stdio.h>#define M 5/* * found */int fun(int n, int xx) int i, j, sum=0;printf( "nThe %d x %d matrix:n", M, M );for( i = 0; i < M; i+ ) for(j = 0;j< M;j+)/* * *fkiund * */printf( "%f ", xxij);printf("n");for( i = 0 ; i < n ; i+ ) sum += xxii+xxi n-i
13、-1 ;retum( sum);)main() int aaMM= 1,2,3,4,5),4,3,2,1,0), 678,9,0,9,8,7,6,5,3,4,5,6,7;printf ( "nThe sum of all elements on 2 diagnals is %d.",fun( M, aa ); 三、程序编写题#include <stdio.h>void fun(int a, int b, long *c) main() int a,b; long c;void NONO ();printf("Input a b:");scan
14、f("%d%d", &a, &b);fun(a, b, &c);printf("The result is: %ldn", c);NONO();void NONO ()/*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。*/ FILE *rf, *wf; int i, a,b ; long c ;rf = fopen("K:k0124010001in.dat","r");wf = fopen("K:kO 1W24010001 out.dat","w&
15、quot;);for(i = 0 ; i < 10 ; i+) fscanf(rf, "%d,%d", &a, &b);fun(a, b, &c);fprintf(wf, "a=%d,b=%d,c=%ldn", a, b, c);fclose(rf);fclose(wf);第24套题一、程序填空题#include <stdio.h> unsigned long fun(unsigned long n) unsigned long x=0; int t; while(n) t=n%10;/*found*/ if(t
16、%2=_l_) /* * *found */ x=2+t;/* * * found * 不/ n=_3_;I return x; ) main() unsigned long n=-l; while(n>99999999lln<0) printf("Pleaseinput(O<n<l 00000000):");scanf("%ld",&n); printf("nThe result is: %ldn",fun(n);)二、程序改错题#include <stdio.h>void fun (lo
17、ng s, long *t) int d;long sl=l;/*,0und*/t = 0;while ( s > 0) d = s%10;/*tound*/if(d%2 = 0) *t = d * si + *t;si *= 10;) s/= 10;) main() long s, t; printf("nPlease enter s:"); scanf("%ld", &s); fun(s, &t);printf("The result is: %ldn", t);三、程序编写题#include <std
18、io.h>void fun(char pl, char p2)main() char si80, s240 ;void NONO ();printf("Enter si and s2:n");scanf("%s%s" s 1, s2);printf("s 1 =%sn", si);printf("s2=%sn", s2);printf("Invoke fun(s 1 ,s2):n");fun(sl, s2);printf("After invoking:n");prin
19、tf("%sn", si);NONO();void NONO ()/*本函数用于打开文件,输入测试数据,调用fun函数,输出数据,关闭文件。*/ intFILE *rf, *wf;charsl8O, s240;rf = fopen("K:kO 1W24010001 in.dat","r");wf = fopen("K:kO 1W24010001 out.dat","w");for(i = 0;i < 10 ; i+) fscanf(rf, "%s" si);fscanf
20、(rf, "%s", s2);fun(sl, s2);fprintf(wf, "%sn" si);fclose(rf);fclose(wf);第25套题一、程序填空题#include <stdio.h>void fun(unsigned long *n) unsigned long x=0, i; int i=l;while(*n)/*nd */ t=*n % _1_;/*found*/ if(t%2!= _2_) x=x+t*i; i=i*10; *n =*n /10;/*found* 不/*n=_3_;main() unsigned lo
21、ng n=-l;while(n>99999999lln<0) printf("Please input(O<n< 100000000): "); scanf("%ld",&n); fun(&n);printf("nThe result is: %ldn",n);二、程序改错题#include <stdio.h>double fun (int n ) double result = 1.0 ;/* *found */jf n = = 0return 1.0;while( n >l
22、 && n < 170 )/* */ result return result;main () int n ;printf("Input N:");scanf("%d", &n);printf("nn%d! =%lfnn", n, fun(n); 三、程序编写题#include <stdio.h>#include <string.h> long fun ( char *p)/*以下代码仅供参考*/int i, len, t; /*len 为串长,t 为正负标识 */ long x=
23、0;len=strlen(p);if(pO=-) t=-l; len-; p+; else t= 1;/*以下完成数字字符串转换为一个数字*/return x*t; main() /* 主函数 */ char s6;void NONO ();long n;printf("Enter a string:n");gets(s);n = fun(s);printf("%ldn",n);NONO (); )void NONO ()/*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。FILE *fp, *wf; inti;char s20;long n
24、 ;fp = fopen("K:kO 1W24010001 in.dat","r");wf = fopen("K:kO 1W24010001 out.dat","w");for(i = 0;i < 10 ; i+) fscanf(fp, "%s", s);n = fun(s);fprintf(wf, "%ldn", n);fclose(fp);fclose(wf); 第26套题一、程序填空题*/#include <stdio.h> double fun(in
25、t n) intdouble s, t;/*found*/s=_1;/* * *found */fbr(i=l; i<=_2_; i+) t=2.0*i;/*tound*/s=s+(2.0*i-l)*(2.0*i+1)/_3_;)return s;main() int n=l;while(n<O) printf("Please input(n>O): "); scanf("%d",&n); printf("nThe result is: %fn",fun(n);二、程序改错题#include <stdi
26、o.h>int fun (char *str,char *substr) int i,j,k,num=0;/*found*/for(i = 0, strij, i+)for(j=i,k=0;substrk=strj;k+,j+) /*found*/If(substrk+l=40,) num+;break;return num;main()char str80,substr80;printf("Input a string:");gets(str);printf("Input a substring:");gets(substr);printf(&q
27、uot;%dn",fun(str,substr);三、程序编写题#include <stdio.h>#include <math.h>double fun (double eps)main() double x;void NONO ();printf("Input eps:");scanf("%lf",&x); printf("neps = %lf, PI=%lfn", x, fun(x); NONO();)void NONO ()/*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件
28、。*/FILE *fp, *wf;inti;double x ;fp =fopen("K:kO 1W24010001 in.dat","r");wf =fopen("K:kO 1 24010001 out.dat","w");for(i = 0 ; i < 10 ; i+) fscanf(fp, "%lf", &x);fprintf(wf, "%lfn", fun(x);fclose(fp);fclose(wf);第27套题一、程序填空题#include <
29、;stdio.h> double fun(int n) int i, k; double s, t;s=0;/* * *found */ k=_l_;fbr(i=l; i<=n; i+) /*ound * */ t=_2_;s=s+k*(2*i-1 )*(2*i+ l)/(t*t);/*found*/ k=k*_3_;returnmain() int n=-l;while(n<O) printf("Please input(n>O): "); scanf("%d",&n); printf("nThe result
30、 is: %fn",fun(n);)二、程序改错题#include <stdio.h>int fun (int m ) intk = 2;while ( k <= m && (m%k)/*found*/k+/*tound*/if (m = k )return 1;else return 0;main() int n;printf( "nPlease enter n: " ); scanf( "%d", &n ); if ( fun n ) printf( "YESn");else p
31、rintf( "NO!n");三、程序编写题#include <stdlib.h>#include <stdio.h>void fun(int a, int n, int *max, int *d) main() int i, x20, max , index, n = 10;void NONO ();for (i=0;i < n;i+) xij = rand()%50; printf("%4d", xij); printf("n");fun( x, n , &max, &index);p
32、rintf("Max =%5d , Index =%4dn",max, index );NONO();void NONO ()FILE *fp, *wf;/*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 int i, x20, max , index, n = 10, j;fp = fopen("K:k012401(XM)lin.dat","r");wf = fopen("K:k0124010001out.dat","w");for(i = 0;i < 10 ; i+) f
33、or(j = 0 ; j < n ; j+) fscanf(fp, "%d,", &xj);fun( x, n , &max, &index);fprintf(wf, "Max=%d,Index=%dn", max, index);)fclose(fp);fclose(wf);第28套题一、程序填空题*/s=0; k=l; i=2;x=_1_/4;while(x 2_ e)#include <stdio.h> double fun(double e) int i, k; double s, t, x;/*foun
34、d*/ /* 东东 * * found * */ s=s+k*x;k=k* (-1);t=2*i;/*found*/ i+;)return s;)main() double e=le-3;printf("nThe result is: %fkn",fun(e);)二、程序改错题#include <stdio.h>/*found*/ void fun ( int n ) int a, b, c, k; double s;s = 0.0; a = 2; b = 1;for ( k = l;k<= n; k+) /* * *nd */s = s + (Doubl
35、e)a / b;c = a; a = a + b; b = c;return s;main() int n = 5;printf( "nThe value of function is: %lfn", fun ( n );程序编写题#include <stdio.h>#define M 4 int fun (int aM) main() int arr2M=5,8,3,45,76,-4,12,82) ;void NONO ();printf("max =%dn", fun(arr);NONO();void NONO 0/*本函数用于打开文件,
36、输入数据,调用函数,输出数据,关闭文件。*/ FILE *wf; int arrM=5,8,3,90,76,412,82;wf = fopen("K:kO 1W24010001 out.dat"," w") ; fprintf(wf, "max=%dn", fun(arr);fclose(wf);)第29套题一、程序填空题#include <stdio.h>double fun(double e) int i; double s, x;/* ou nd * */s=0; i=_1 x=1.0;while(x>e)/*
37、tound*/ _2_;/*found*/ x=(2.0*i-1)/(_3_)*(2.0*i);s=s+x;return s;main() double e=le-3;printf("nThe result is: %fn",fun(e); 二、程序改错题#include <stdio.h>#include <string.h>void fun (char s, char t)(int i, d;d = strlen(s);for (i = 0; i<d; i+) ti = si;for (i = 0; i<d; i+) td+i = s
38、d-l-i; /*found*/ t2*d-l = 40main()(char s100, t100;printf("nPlease enter string S:"); scanf("%s", s); fun(s, t); printf("nThe result is: %sn", t);三、程序编写题#include <stdio.h>#include <string.h>void fun(char *s, char tJ)main()(char s100,t100;void NONO ();printf(
39、"nPlease enter string S:"); scanf("%s", s);fun(s, t);printf("nThe result is: %sn", t);NONO();void NONO ()/*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。char s100,t100;HLE *rf, *wf;int i;rf = fopen("K:kO 1W24010001 in.dat","r");wf = fopen("K:kO 1W24010001 out.d
40、at","w");for(i = 0;i < 10 ; i+) fscanf(rf, "%s", s);fun(s, t);fprintf(wf, "%sn", t);)fclose(rf);fclose(wf);第30套题一、程序填空题*/#include <stdio.h>#include <stdlib.h>#include <string.h>char *fun(char *s) int i, j, k, n; char *p, *t;n=strlen(s)+1;t=(cha
41、r*)malloc(n*sizeof(char);p=(char*)malloc(n*sizeof(char);j=0; k=0;for(i=0; i<n; i+)if(si>=ta,)&&(si<=z,)ll(si>=4A,)&&(si<=*Z,) /*fund* */tj=_l_;j+;else pk=si; k+; /*found*/for(i=0; i<_2_; i+) tj+i=pi;/*found* 不/tj+k= _3_;return t;main() char s80;printf("Please input: "); scanf("%s",s); printf("nThe result is: %sn",fun(s);)二、程序改错题#include <stdio.h>#include <string.h>void fun (char *s, char *tl, char *t
限制150内