计算机二级C语言上机试题及答案.doc

上传人:yyf 文档编号:5027231 上传时间:2020-01-29 格式:DOC 页数:19 大小:232.50KB
返回 下载 相关 举报
计算机二级C语言上机试题及答案.doc_第1页
第1页 / 共19页
计算机二级C语言上机试题及答案.doc_第2页
第2页 / 共19页
计算机二级C语言上机试题及答案.doc_第3页
第3页 / 共19页
计算机二级C语言上机试题及答案.doc_第4页
第4页 / 共19页
计算机二级C语言上机试题及答案.doc_第5页
第5页 / 共19页
点击查看更多>>
资源描述

《计算机二级C语言上机试题及答案.doc》由会员分享,可在线阅读,更多相关《计算机二级C语言上机试题及答案.doc(19页珍藏版)》请在三一文库上搜索。

1、2.给定程序MODI1.C中函数fun的功能是:利用插入排序法对字符串中的字符按从小到大的顺序进行排序。插入法的基本算法是:先对字符串中的头两个元素进行排序。然后把第三个字符插入到前两个字符中,插入后面三个字符依然有序;再把第四个字符插入到前三个字符中,。待排序的字符串已在主函数中赋予。#include #include #define N 80void insert(char *aa) int i,j,n; char ch;/*found*/ n=strlen aa ; for( i=1; i=0) & ( chaaj ) aaj+1=aaj; j-; aaj+1=ch; main( ) c

2、har aN=QWERTYUIOPASDFGHJKLMNBVCXZ; printf (The original string : %sn, a); insert(a) ; printf(The string after sorting : %snn,a ); 答案:(1) n=strlen(aa); (2) ch=aai;3. N名学生的成绩已在主函数中放入一个带头节点的链表结构中,h指向链表的头节点。请编写函数fun,它的功能是:找出学生的最高分,由函数值返回。#include #include #define N 8struct slist double s; struct slist *

3、next;typedef struct slist STREC;double fun( STREC *h )STREC * creat( double *s) STREC *h,*p,*q; int i=0; h=p=(STREC*)malloc(sizeof(STREC);p-s=0; while(is=si; i+; p-next=q; p=q; p-next=0; return h;outlist( STREC *h) STREC *p; p=h-next; printf(head); do printf(-%2.0f,p-s);p=p-next; while(p!=0); printf

4、(nn);main() double sN=85,76,69,85,91,72,64,87, max;void NONO (); STREC *h; h=creat( s ); outlist(h); max=fun( h ); printf(max=%6.1fn,max); NONO();void NONO ()/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ FILE *in, *out ; int i,j ; double sN,max; STREC *h ; in = fopen(c:testin.dat,r) ; out = fopen(c:testout.d

5、at,w) ; for(i = 0 ; i 10 ; i+) for(j=0 ; j next;max=p-s;while(p) if(p-smax) max=p-s; p=p-next;return max;第32套1.给定程序中,函数fun的功能是:在形参s所指字符串中的每个数字字符之后插入一个*号。例如,形参s所指的字符串为:def35adh3kjsdf7。执行结果为:def3*5*adh3*kjsdf7*。#include void fun(char *s) int i, j, n; for(i=0; si!=0; i+)/*found*/ if(si=0 _1_ sii; j-)/*

6、found*/ sj+1= _3_; sj+1=*; i=i+1; main() char s80=ba3a54cd23a; printf(nThe original string is : %sn,s); fun(s); printf(nThe result is : %sn,s);答案:(1) & (2) 0 (3) sj2.给定程序MODI1.C中函数fun的功能是:根据整型形参m,计算如下公式的值。#include double fun ( int m ) double y = 1.0 ; int i;/*found*/ for(i = 2 ; i m ; i+)/*found*/ y

7、 += 1 / (i * i) ; return( y ) ;main( ) int n = 5 ; printf( nThe result is %lfn, fun ( n ) ) ; 答案:(1) for(i=2;i=m;i+) (2) y+=1.0/i/i;3. 请编写函数fun,函数的功能是:实现B=A+A,即把矩阵A加上A的转置,存放在矩阵B中。计算结果在main函数中输出。#include void fun ( int a33, int b33)main( ) /* 主程序 */ int a33 = 1, 2, 3, 4, 5, 6, 7, 8, 9, t33 ; int i, j

8、 ; void NONO ( ); fun(a, t) ; for (i = 0 ; i 3 ; i+) for (j = 0 ; j 3 ; j+) printf(%7d, tij) ; printf(n) ; NONO () ;void NONO ( )/* 本函数用于打开文件,输入测试数据,调用fun函数,输出数据,关闭文件。*/ int i, j, k, a33, t33 ; FILE *rf, *wf ; rf = fopen(c:testin.dat,r) ; wf = fopen(c:testout.dat,w) ; for(k = 0 ; k 5 ; k+) for(i = 0

9、 ; i 3 ; i+) fscanf(rf, %d %d %d, &ai0, &ai1, &ai2) ; fun(a, t) ; for(i = 0 ; i 3 ; i+) for(j = 0 ; j 3 ; j+) fprintf(wf, %7d, tij) ; fprintf(wf, n) ; fclose(rf) ; fclose(wf) ;答案:81. void fun ( int a33, int b33)int i,j;for(i=0;i3;i+)for(j=0;j3;j+)bij=0;for(i=0;i3;i+)for(j=0;j3;j+) bij=aij+aji;第33套1.

10、程序通过定义学生结构体变量,存储了学生的学号、姓名和3门课的成绩。函数fun的功能是将形参a中的数据进行修改,把修改后的数据作为函数值返回主函数进行输出。#include #include struct student long sno; char name10; float score3;/*found*/_1_ fun(struct student a) int i; a.sno = 10002;/*found*/ strcpy(_2_, LiSi);/*found*/ for (i=0; i3; i+) _3_+= 1; return a;main() struct student s=

11、10001,ZhangSan, 95, 80, 88, t; int i; printf(nnThe original data :n); printf(nNo: %ld Name: %snScores: ,s.sno, s.name); for (i=0; i3; i+) printf(%6.2f , s.scorei); printf(n); t = fun(s); printf(nThe data after modified :n); printf(nNo: %ld Name: %snScores: ,t.sno, t.name); for (i=0; i3; i+) printf(%

12、6.2f , t.scorei); printf(n);答案:(1) stuct student (2) a.name (3) a.scorei2.假定整数数列中的数步重复,并存放在数组中。给定程序MODI1.C中函数fun的功能是:删除数列中值为x的元素。n中存放的是数列中元素的个数。#include #define N 20int fun(int *a,int n,int x) int p=0,i; an=x; while( x!=ap )p=p+1;/*found*/ if(P=n) return -1; else for(i=p;in-1;i+)/*found*/ ai+1=ai; r

13、eturn n-1; main() int wN=-3,0,1,5,7,99,10,15,30,90,x,n,i; n=10; printf(The original data :n); for(i=0;in;i+) printf(%5d,wi); printf(nInput x (to delete): ); scanf(%d,&x); printf(Delete : %dn,x); n=fun(w,n,x); if ( n=-1 ) printf(*Not be found!*nn); else printf(The data after deleted:n); for(i=0;in;i+

14、) printf(%5d,wi);printf(nn); 答案:(1) if(p= =n)return-1; (2) ai=ai+1; 3. 学生的记录由学号和成绩组成,N名学生的数据已在主函数中放入结构体数组s中。请编写函数fun,它的功能是:把分数最低的学生数据放在b所指的数组中。注意:分数最低的学生可能不止一个。函数返回分数最低的学生的人数。 #include #define N 16typedef struct char num10; int s; STREC;int fun( STREC *a, STREC *b )main() STREC sN=GA05,85,GA03,76,GA

15、02,69,GA04,85,GA01,91,GA07,72,GA08,64,GA06,87,GA015,85,GA013,91,GA012,64,GA014,91,GA011,91,GA017,64,GA018,64,GA016,72; STREC hN; int i,n;FILE *out ; n=fun( s,h ); printf(The %d lowest score :n,n); for(i=0;in; i+) printf(%s %4dn,hi.num,hi.s); printf(n); out = fopen(c:testout.dat,w) ; fprintf(out, %dn

16、,n); for(i=0;in; i+) fprintf(out, %4dn,hi.s); fclose(out);答案:82. int fun( STREC *a, STREC *b )int i,j=0,score;score=a0.s;for(i=0;iai.s)score=ai.s;for(i=0;iN;i+)if(score=ai.s) strcpy(bj.num,ai.num); bj.s=ai.s; j+;return j;第34套1.给定程序中,函数fun的功能是:计算下式前n项的和作为函数值返回。#include double fun(int n) int i, k; dou

17、ble s, t; s=0;/*found*/ k=_1_; for(i=1; i=n; i+) /*found*/ t=_2_; s=s+k*(2*i-1)*(2*i+1)/(t*t);/*found*/ k=k*_3_; return s;main() int n=-1; while(n0): ); scanf(%d,&n); printf(nThe result is: %fn,fun(n);答案:(1) 1 (2) 2*I (3) (-1)2.给定程序MODI1.C中函数fun的功能是:判断一个整数是否是素数,若是返回1,否则返回0。在main()函数中,若fun返回1输出YES,若f

18、un返回0输出NO!。#include int fun ( int m ) int k = 2; while ( k = m & (m%k)/*found*/ k+/*found*/ if (m = k ) return 1; else return 0;main( ) int n; printf( nPlease enter n: ); scanf( %d, &n ); if ( fun ( n ) ) printf( YESn ); else printf( NO!n ); 答案:(1) k+; (2) if(m= =k)3. 请编写一个函数fun,它的功能是:找出一维整型数组元素中最大的

19、值和它所在的下标。最大的值和它所在的下标通过形参传回。数组元素中的值已在主函数中赋予。 #include #include 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+) xi = rand()%50; printf(%4d, xi) ; printf(n); fun( x, n , &max, &index); printf(Max =%5d , Index =%4dn,max, index ); NONO();voi

20、d NONO ()/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ FILE *fp, *wf ; int i, x20, max , index, n = 10, j ; fp = fopen(c:testin.dat,r) ; wf = fopen(c:testout.dat,w) ; for(i = 0 ; i 10 ; i+) for(j = 0 ; j n ; j+) fscanf(fp, %d, &xj) ; fun( x, n , &max, &index); fprintf(wf, Max=%d,Index=%dn, max, index) ; fclo

21、se(fp) ; fclose(wf) ; 主函数中x是数组名,n是x中的数据个数。max存放最大值,index存放最大值所在元素的下标。答案:83.void fun(int a, int n , int *max, int *d ) int i,j,k=0;i=a0;for(j=0;jn;j+)if(iaj)i=aj;k=j;*max=i;*d=k;第35套1.函数fun的功能是进行数字字符转换。若形参ch中是数字字符09,则09,则0转换成9,1转换成8,2转换成7,9转换成0。若是其他字符则保持不变,并将转换后的结果作为函数值返回。#include /*found*/_1_ fun(ch

22、ar ch)/*found*/ if (ch=0 & _2_)/*found*/ return 9- (ch-_3_); return ch ;main() char c1, c2; printf(nThe result :n); c1=2; c2 = fun(c1); printf(c1=%c c2=%cn, c1, c2); c1=8; c2 = fun(c1); printf(c1=%c c2=%cn, c1, c2); c1=a; c2 = fun(c1); printf(c1=%c c2=%cn, c1, c2);答案:(1) char (2) ch=9 (3) 02.给定程序MOD

23、I1.C中函数fun的功能是:将p所指字符串中的所有字符复制到b中,要求每复制3个字符之后插入一个空格。#include void fun(char *p, char *b) int i, k=0; while(*p) i=1; while( i=3 & *p ) /*found*/ bk=p; k+; p+; i+; if(*p) /*found*/ bk+= ; bk=0;main() char a80,b80; printf(Enter a string: ); gets(a); printf(The original string: ); puts(a); fun(a,b); prin

24、tf(nThe string after insert space: ); puts(b); printf(nn); 答案:(1) bk=*p; (2) bk=;k+;3. N名学生的成绩已在主函数中放入一个带头节点的链表结构中,h指向链表的头节点。请编写函数fun,它的功能是:求出平均分,由函数值返回。#include #include #define N 8struct slist double s; struct slist *next;typedef struct slist STREC;double fun( STREC *h )STREC * creat( double *s) S

25、TREC *h,*p,*q; int i=0; h=p=(STREC*)malloc(sizeof(STREC);p-s=0; while(is=si; i+; p-next=q; p=q; p-next=0; return h;outlist( STREC *h) STREC *p; p=h-next; printf(head); do printf(-%4.1f,p-s);p=p-next; while(p!=0); printf(nn);main() double sN=85,76,69,85,91,72,64,87,ave; void NONO ( ); STREC *h; h=cre

26、at( s ); outlist(h); ave=fun( h ); printf(ave= %6.3fn,ave); NONO();void NONO()/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ FILE *in, *out ; int i,j ; double sN,ave; STREC *h ; in = fopen(c:testin.dat,r) ; out = fopen(c:testout.dat,w) ; for(i = 0 ; i 10 ; i+) for(j=0 ; j next;while(p) sum+=p-s;p=p-next; ave=

27、sum/N;return ave;第36套1.给定程序中,函数fun的功能是:计算出带有头结点的单向链表中各结点数据域中值之和作为函数值返回。#include #include #define N 8typedef struct list int data; struct list *next; SLIST;SLIST *creatlist(int *);void outlist(SLIST *);int fun( SLIST *h) SLIST *p; int s=0; p=h-next; while(p) /*found*/ s+= p-_1_;/*found*/ p=p-_2_; ret

28、urn s;main() SLIST *head; int aN=12,87,45,32,91,16,20,48; head=creatlist(a); outlist(head);/*found*/ printf(nsum=%dn, fun(_3_);SLIST *creatlist(int a) SLIST *h,*p,*q; int i; h=p=(SLIST *)malloc(sizeof(SLIST); for(i=0; idata=ai; p-next=q; p=q; p-next=0; return h;void outlist(SLIST *h) SLIST *p; p=h-n

29、ext; if (p=NULL) printf(The list is NULL!n); else printf(nHead ); do printf(-%d, p-data); p=p-next; while(p!=NULL); printf(-Endn); 答案:(1) data (2) next (3) head2.给定程序MODI1.C中函数fun的功能是:将s所指字符串中出现的与t1所指字符串相同的子串全部替换成t2 所指字符串,所形成的新串放在w所指的数组中。在此处,要求t1和t2所指字符串的长度相同。#include #include void fun (char *s, cha

30、r *t1, char *t2 , char *w) char *p , *r, *a; strcpy( w, s ); while ( *w ) p = w; r = t1;/*found*/ while ( r ) if ( *r = *p ) r+; p+; else break; if ( *r = 0 ) a = w; r = t2; while ( *r )/*found*/ *a = *r; a+; r+ w += strlen(t2) ; else w+; main() char s100, t1100, t2100, w100; printf(nPlease enter st

31、ring S:); scanf(%s, s); printf(nPlease enter substring t1:); scanf(%s, t1); printf(nPlease enter substring t2:); scanf(%s, t2); if ( strlen(t1)=strlen(t2) ) fun( s, t1, t2, w); printf(nThe result is : %sn, w); else printf(Error : strlen(t1) != strlen(t2)n); 答案:(1) while(*r) (2) *a=*r,a+;r+;3. 函数fun的

32、功能是:将s所指字符串中下标为偶数的字符删除,串中剩余字符形成的新串放在t所指数组中。#include #include void fun(char *s, char t)main() char s100, t100; void NONO ( ); printf(nPlease enter string S:); scanf(%s, s); fun(s, t); printf(nThe result is: %sn, t); NONO();void NONO ( )/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ char s100, t100 ; FILE *rf, *wf ; int i ; rf = fopen(c:testin.dat,r) ; wf = fopen(c:testout.dat,w) ; for(i = 0 ; i 10 ; i+) fscanf(rf, %s, s) ; fun(s, t) ; fprintf(wf, %sn, t) ; fclose(rf) ; fclose(wf) ;答案:85.void fun(char *s, char t) int i;for(i=0;i50;i+)ti=s2*i+1;第37套1.给定程序中,函数fun的功能是:在带有头结点的单向链表中,查找数据域中值

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

当前位置:首页 > 研究报告 > 商业贸易


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