二级c语言
二级C语言是全国计算机等级考试中的一项,是很多非计算机专业朋友需要获得的证书,这里收集大量的二级C语言相关的教程和题目
-
给定程序中,函数fun的功能是:将形参n中,各位上为偶数的数取出,并按原来从低位到高位相反的顺序组成一个新的数,并作为函数值返回。例如,输入一个整数:27638496,函数返回值为:64862。请在程序的下划线处填入正确的内容并把下划线删除.便程序得出正确的结果。注意:源程序存放在考生文件夹下的BLANK1.C中。不得增行或删行,也不得更改程序的结构!/**********code.c**********/ #include <stdio.h> unsigned long fun(unsigned long n) { unsigned long x=0; int t; while(n) { t=n%10; if(t%2==①______) x=②______+t; n=③______; }return x; } main() { unsigned long n=-1; while(n>99999999||n<0) { printf("Please input(0<n<100000000): "); scanf("%ld",&n); } printf("\nThe result is: %ld\n",fun(n)); } /**********-code.c**********/
-
给定程序MODI1.C中函数fun的功能是:输出M行M列整数方阵,然后求两条对角线上元素之和,返回此和数。请改正程序中的错误,使它能得出正确的结果。注意:不要改动main函数,不得增行或删行,也不得更己改程序的结构!/**********code.c**********/ #include <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++) /**********found**********/ printf("%f ", xx[i][j]); printf("\n"); }for(i = 0;i < n;i++) sum += xx[i][i]+xx[i][n-i-1]; return(sum); }void main() { int aa[M][M]={{1,2,3,4,5},{4,3,2,1,0},{6,7,8,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)); } /**********-code.c**********/
-
给定程序中,函数fun的功能是:将形参n中,各位上为偶数的数取出,并按原来从高位到低位的顺序组成一个新的数,并作为函数值返回。例如,从主函数输入一个整数:27638496,函数返回值为:26846。请在程序的下划线处埴入正确的内容并把下划线删除.使程序得出正确的结果。 注意:源程序存放在考生文件夹下的BLANK1.C中。不得增行或删行,也不得更改程序的结构!/**********code.c**********/ #include <stdio.h> unsigned long fun(unsigned long n) { unsigned long x=0, s, i; int t; s=n; i=①______; while(②______) { t=s%10; if(t%2==0) { x=x+t*i; i=③______; } s=s/10; }return x; }void main() { unsigned long n=-1; while(n>99999999||n<0) { printf("Please input(0<n<100000000): "); scanf("%ld",&n); } printf("\nThe result is: %ld\n",fun(n)); } /**********-code.c**********/
-
学生的记录由学号和成绩组成,N名学生的数据已在主函数中敲入结构体数组s中,请编写函数fun,它的功能是:按分数的高低排列学生的记录,高分在前。注意:部分源程序给出如下。请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。/**********code.c**********/ #include <stdio.h> #define N 16 typedef struct { char num[10]; int s; }STREC; void fun(STREC a[]) { STREC tmp; int i,j; for(i = 1; i < N; i++) for(j = 0; j < N-1; j++) { /* 请按题目要求,完成一下代码*/ } }void main() { STREC s[N] = {{"GA005",85}, {"GA003",76}, {"GA002",69}, {"GA004",85}, {"GA001",91}, {"GA007",72}, {"GA008",64}, {"GA006",87}, {"GA015",85},{"GA013",91}, {"GA012",64}, {"GA014",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 ",s[i].num,s[i].s); } printf("\n"); out = fopen("out.dat","w"); for(i=0;i<N;i++) { if((i)%4==0&&i) fprintf(out,"\n"); fprintf(out,"%4d ",s[i].s); } fprintf(out,"\n"); fclose(out); }
-
绐定程序MODI1.C中函数fun的功能是:从低位开始取出长整型变量s中偶数位上的数,依次构成一个新数放在t中。高位仍在高位,低位仍在低位。例如,当s中的数为:7654321时,t中的数为:642。请改正程序中的错误,使它自能得出正确的结果。注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!/**********code.c**********/ #include <stdio.h> /**********found**********/ void fun (long s, long t) { long sl=10; s /= 10; *t = s % 10; /**********found**********/ while (s < 0) { s = s/100; *t = s%10*sl + *t; sl = sl * 10; } } main() { long s, t; printf("\nPlease enter s:"); scanf("%ld", &s); fun(s, &t); printf("The result is: %ld\n", t); } /**********-code.c**********/
-
给定程序中,函数fun功能是:找出100~999之间(含100和999)所有整数中各位上数字之和为x(x为一正整数)的整数.然后输出;符合条件的整数个数作为函数值返回。例如,当x值为5时,l00~999之间各位上数字之和为5的整数有:104、113、122、131、140、203、212、221、230、302、311、320、401、410、500。共有15个。当x值为27时,各位数字之和为27的整数是:999。只有1个。请在程序的下划线处填入正确的内容并把下划线删除.使程序得出正确的结果。注意:源程序存放在考生文件夹下的BLAIIK1.C中。不得增行或删行.也不得更改程序的结构!/**********code.c**********/ #include <stdio.h> int fun(int x) { int n, s1, s2, s3, t; n=0; t=100; while(t<=①______) { s1=t%10; s2=(②______)%10; s3=t/100; if(s1+s2+s3==③______) { printf("%d ",t); n++; } t++; }return n; } void main() { int x=-1; while(x<0) { printf("Please input(x>0): "); scanf("%d",&x); } printf("\nThe result is: %d\n",fun(x)); } /**********-code.c**********/
-
给定程序MODI1.C中函数fun的功能:先将s所指字符串中的字符按逆序存放到t所指字符中,然后把s所指串中的字符按正序连接到t所指串的后面。例如:当s所指的字符串为:"ABCDE"时,则t所指的字符串应为:"EDCBAABCDE"。请改正程序中的错误,使它能得出正确的结果。注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!/**********code.c**********/ #include<stdio.h> #include<string.h> void fun (char *s, char *t) { /**********found**********/ int i; i=0; sl = strlen(s); for(; i<sl; i++) /**********found**********/ t[i] = s[sl-i]; for(i=0; i<sl; i++) t[sl+i] = s[i]; t[2*sl] = '\0'; } main() { char s[100],t[100]; printf("\nPlease enter string s:"); scanf("%s", s); fun(s,t); printf("The result is: %s\n", t); } /**********-code.c**********/
-
给定程序中,函数fun的功能是:找出100至x(x≤999)之间各位上的数字之和为15的所有整数,然后输出;符合条件的整数个数作为函数值返回。例如,当n值为500时,各位数字之和为15的整数有:159、168、177、186、195、249、258、267、276、285、294、339、348、357、366、375、384、393、429、438、447、456、465、474、483、492。共有26个。请在程序的下划线处填入正确的内容并把下划线删除,是程序得出正确的结果。注意:源程序存放在考生文件夹下的BLANK1.C中。不得增行或删行,也不得更改程序的结构!/**********code.c**********/ #include <stdio.h> int fun(int x) { int n, s1, s2, s3, t; /**********found**********/ n=①______; t=100; /**********found**********/ while(t<=②______) { s1=t%10; s2=(t/10)%10; s3=t/100; if(s1+s2+s3==15) { printf("%d ",t); n++; } /**********found**********/ ③______; }return n; }void main() { int x=-1; while(x>999||x<0) { printf("Please input(0<x<=999): "); scanf("%d",&x); } printf("\nThe result is: %d\n",fun(x)); } /**********-code.c**********/
-
请编写函数fun(),该函数的功能是:移动一维数组中的内容,若数 组中有n个整数,要求把下标从p~n-1(p 例如,一维数组中的原 始内容为1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,p的值为6。移动后,一维数组的内容应为7,8,9,10,11,12,13,14,15,1,2,3,4,5,6。 注意:部分源程序给出如下。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花 括号中填入所编写的若干语句。/**********code.c**********/ #include <stdio.h> #define N 80 void fun(int*w,int p,int n) { } void main() { int a[N]={ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; int i,p,n=15; printf("The original data:\n"); for(i=0;i<n;i++) printf("%3d",a[i]); printf("\n\nEnter p:"); scanf("%d",&p); fun(a,p,n); printf("\nThe data after moving:\n"); for(i="0;i<n;i++)" printf("\n\n");}
-
给定程序中函数fun的功能是:根据整型形参m,计算如下公式的 值。例如,若m中的值为:5,则应输出:1.463611。请改正程序中的错 误,使它能得到正确结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的结 构。/**********code.c**********/ #include double fun(int m) { double y=1.0; int i; /**********found**********/ for(i=2;i<m;i++) /**********found**********/ y+=1/(i*i); return(y); } main() { int n=5; printf("%\nThe result is%1f\n",fun(n)); /**********-code.c**********/
-
下列给定的程序中,函数fun()的功能是:求出以下分数序列的前n项和。2/1,3/2,5/3,8/5,13/8,21/13,……和值通过函数值返回main()函数。例如,若输入n=5,则应输出8.391667。 注意:部分源程序给出如下。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的横 线上填入所编写的若干表达式或语句。/**********code.c**********/ #include <stdio.h> #include <conio.h> double fun(int n){ int a=2,b=1,c,k; double①______; for(k=1;k<=n;k++) { s=s+1.0*a/b; c=a; a+=②______; b=c; } return(s); } main() { int n=5; /**********found**********/ printf("\nThe value of function is:%1f\n",③______); } /**********-code.c**********/
-
请编写函数fun(),该函数的功能是:计算并输出给定整数n的所有因子(不包括1和自身)之和。规定n的值不大于1000。例如,在主函数中从键盘给n输入的值为856,则输出为:sum=763。注意:部分源程序给出如下。请勿改动主函数main()和其他函数中的任何内容,仅在fun()函数的花括号中填入所编写的若干语句。试题程序如下:/**********code.c**********/ #include <stdio.h> int fun(int n) { } void main() { int n,sum; printf("Input n: "); scanf("%d",&n); sum=fun(n); printf("sum=%d\n",sum); }
-
下列给定程序中,是建立一个带头结点的单向链表,并用随机函数为各结点数据域赋值。函数fun的作用是求出单向链表结点(不包括头结点)数据域中的最大值,并且作为函数值返回。请改正程序指定部位的错误,使它能得到正确结果。注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。/**********code.c**********/ #include<stdio.h> #include<stdlib.h> typedef struct aa { int data; struct aa *next; } NODE; fun( NODE *h) { int max=-1; NODE *p; /**********found**********/ p=h; while(p) { if(p->data>max) max=p->data; /**********found**********/ p=h->next; }return max; }outresult(int s,FILE *pf) { fprintf(pf,"\nThe max in link:%d\n",s); } NODE *creatlink(int n,int m) { NODE *h,*p,*s,*q; int i,x; h=p=(NODE *)malloc(sizeof(NODE)); h->data=9999; for(i=1;i<=n;i++) { s=(NODE *)malloc(sizeof(NODE)); s->data=rand()%m; s->next=p->next; p->next=s; p=p->next; } p->next=NULL; return h; } outlink(NODE *h, FILE *pf) { NODE *p; p=h->next; fprintf(pf,"\nTHE LIST:\n\n HEAD"); while(p) { fprintf(pf,"->%d",p->data); p=p->next; } fprintf(pf,"\n"); } main() { NODE *head;int m; head=creatlink(12,100); outlink(head,stdout); m=fun(head); printf("\nTHE RESULT:\n"); outresult(m,stdout); } /**********-code.c**********/
-
给定程序的功能是:从键盘输入若干行文本(每行不超过80个字符),写到文件myfile4.txt中,用-1作为字符串输入结束的标志。然后将文件的内容读出显示在屏幕上。文件的读写分别由自定义函数ReadText和WriteText实现。注意:部分源程序给出如下。请勿改动主函数main和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式或语句。/**********code.c**********/ #include <stdio.h> #include <string.h> #include <stdlib.h> void WriteText(FILE*); void ReadText(FILE*); void main() { FILE*fp; if((fp=fopen("myfile4.txt","w"))==NULL) { printf("open fail!!\n"); exit(0); } WriteText(fp); fclose(fp); if((fp=fopen("myfile4.txt","r"))==NULL) { printf("open fail!!\n"); exit(0); } ReadText(fp); fclose(fp); } /**********found**********/ void WriteText(FILE ①______) { char str[81]; printf("\nEnter string writh -1 to end:\n"); gets(str); while(strcmp(str,"-1")!=0) { /**********found**********/ fputs(②______,fw); fputs("\n",fw); gets(str); } }void ReadText(FILE*fr) { char str[81]; printf("\nRead file and output to screen:\n"); fgets(str,81,fr); while(!feof(fr)); { /**********found**********/ printf("%s",③______); fgets(str,81,fr); } } /**********-code.c**********/
-
学生的记录由学号和成绩组成,N名学生的数据已在主函数中敲入 结构体数组s中,请编写函数fun,它的功能是:按分数的高低排列学 生的记录,高分在前。 注意:部分源程序给出如下。请勿改动主函数main和其它函数中 的任何内容,仅在函数fun的花括号中填入你编写的若干语句。/**********code.c**********/ #include <stdio.h> #define N 16 typedef struct { char num[10]; int s; }STREC; void fun( STREC a[] ) { STREC tmp; int i,j; for(i = 1; i < N; i++) for(j = 0; j < N-1; j++) { /*请按题目要求,完成一下代码*/ } } void main() { STREC s[N] = {{"GA005",85}, {"GA003",76}, {"GA002",69},{"GA004",85}, {"GA001",91}, {"GA007",72}, {"GA008",64},{"GA006",87},{"GA015",85}, {"GA013",91}, {"GA012",64}, {"GA014",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 ",s[i].num,s[i].s); } printf("\n"); out=fopen("out.dat","w"); for(i=0;i<N;i++) { if((i)%4==0&&i)fprintf(out, "\n"); fprintf(out, "%4d ",s[i].s); } fprintf(out,"\n"); fclose(out); }
-
下列给定程序中,函数fun()的功能是:用选择法对数组中的n个元 素按从小到大的顺序进行排序。 请改正程序中的错误,使它能得到正确结果。 注意:不要改动main函数,不得增行或删行,也不得更改程序的 结构。/**********code.c**********/ #include <stdio.h> #define N 20 void fun(int a[],int n) { int i,j,t,p; for(j=0;j<n-1;j++) { /********found********/ p=j; for(i=j;i<n;i++) if(a[i]<a[p]) /********found********/ t=a[p]; a[p]=a[j]; a[j]=t; } main() { int a[N]={9,6,8,3,-1},i,m=5; printf("排序前的数据:"); for(i=0;i<m;i++) printf("%d",a[i]); printf("\n"); fun(a,m); printf("排序后的数据:"); for(i=0;i<m;i++) printf("%d",a[i]); printf("\n"); } **********-code.c**********
-
请补充fun函数,该函数的功能是:按‘0’到‘9’统计一个字符串中的 奇数数字字符各自出现的次数,结果保存在数组num中。注意:不能 使用字符串库函数。 例如,输入“x=1123.456+0.909*bc”,结果为:1=2,3=1,5=1,7=0,9=2。注意:部分源程序给出如下。 请勿改动主函数main和其他函数中的任何内容,仅在函数fun的横 线上填入所编写的若干表达式或语句。/**********code.c**********/ #include <conio.h> #include <stdio.h> #define N 20 fun(char *tt,int num[]) { int i,j; int bb[10]; char *p=tt; for(i=0;i<10;i++) { num[i]=0; bb[i]=0; } /**********found**********/ while(①______) { if(*p>='0'&&*p<='9') /**********found**********/ ②______; p++; } for(i=1,j=0;i<10;i=i+2,j++) /**********found**********/ ③______; } main() { char str[N]; int num[10],k; printf("\nPlease enter a string:"); gets(str); printf("\n******** The original string ********\n"); puts(str); fun(str,num); printf("\n******** The number of letter ********\n"); for(k=0;k<5;k++) { printf("\n"); printf("%d=%d",2*k+1,num[k]); } printf("\n"); return; } /**********-code.c**********/
-
请编写函数fun,其功能是:计算并输出3到n之间(含3和n)所有素数的平方根之和。例如,在主函数中从键盘给n输入100后,输出为:sum=148.874270。注意:要求n的值大于2但不大于100。部分源程序给出如下。请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。/**********code.c**********/ #include <math.h> #include <stdio.h> double fun(int n) { } main() { int n; double sum; printf("\n\nInput n: "); scanf("%d",&n); sum=fun(n); printf("\n\nsum=%f\n\n",sum); }
-
下列给定程序中,函数fun()的功能是计算并输出high以内的素数之和。high由主函数传给fun()函数。例如:若high的值为100,则函数的解为1060。请改正程序中的错误,使它能得到正确结果。注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。/**********code.c**********/ #include<conio.h> #include<stdio.h> #include<math.h> int fun(int high) { int sum=0,n=0,j,yes; while(high>=2) { yes=1; for(j=2;j<=high/2;j++) /**********found**********/ if high%j==0 { yes=0; break; } /**********found**********/ if(yes==0) { sum+=high; n++; } high--; }return sum; } main() { printf("%d\n",fun(100)); } /**********-code.c**********/
-
N个有序整数数列已放在一维数组中,给定下列程序中,函数fun()的功能是:利用折半查找法查找整数m在数组中的位置。若找到,则返回其下标值;反之,则返回“Not be found!”。折半查找法的基本算法是:每次查找前先确定数组中待确定的范围:low和high(low<high),然后把m与中间位置(mid)中元素的值进行比较。如果m的值大于中间位置元素中的值,则下一次的查找范围放在中间位置之后的元素中;反之,下次查找范围落在中间位置之前的元素中。直到low>high,查找结束。注意:部分源程序给出如下。请勿改动主函数main和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式或语句。/**********code.c**********/ #include<stdio.h> #define N 10 int fun(int a[],int m) { int low=0,high=N-1,mid; while(low<=high) { /**********found**********/ mid= ①______; if(m<a[mid]) /**********found**********/ high= ②______; else if(m>a[mid]) low=mid+1; else return(mid); } /**********found**********/ ③______(-1); } main() { int i,a[N]={-3,4,7,9,13,24,67,89,100,180},k,m; printf("a数组中的数据如下:"); for(i=0;i<N;i++)printf("%d",a[i]); printf("Enter m:"); scanf("%d",&m); k=fun(a,m); if(k>=0) printf("m=%d,index=%d\n",m,k); else printf("Not be found\n"); } /**********-code.c**********/