二级c语言题库答案.docx
二级C语言题库答案第1套题一、程序填空题#include#includestructnum;#define typedef int char name10;char tel10;JSTYPE;void check();/*found*/ int fun(S*T*PE*std)/*found*/FILE *fp;intretum(O);if(fp=fopen("myfile5.dat","wb")=NULL) printf("nOutput data to file !n"); for(i=0; i<N; i+) /*found*/fwrite(&stdi, sizeof(STYPE), 1, fp); fclose(fp);return (1);二、程序改错题#include <stdio.h>#include <string.h>void fun (char *s, char *t) int i, si;si = strlen(s);/*found*/ for( i=0; i<=sl; i +)ti = si;for (i=0; i<sl; i+)tsl+i = ssl-i-l;/*tound*/心l+sl = '0'main() chars100,t100;printf("nPlease enter string s:"); scanf("%s", s); fun(s, t); printf("The result is: %sn", t);)第2套题一、程序填空题#include<stdio.h>#include<string.h>#include<stdlib.h>void WriteText(FILE *);void ReadText(FILE *);main() FILE *fp;if(fp=fopen("myfile4.txt","w")=NULL) printf(" open fail!n"); exit(O); WriteText(fp);fclose(fp);if(fp=fopen("myfile4.txt","r")=NULL) printf(" open fail! !n"); exit(O); ReadText(fp);fclose(fp);/*found*/void WriteText(FILE *fw) char str81;printf("nEnter string with -1 to end :n"); gets(str); while(strcmp(str,"-1 ")!=0) /* * found * 豕/gets(str);fputs(str,fw); fputs("n",fw);void ReadText(FILE *fr) char str81;printf("nRead file and output to screen :n"); fgets(str,81,fr); while( !feof(fr) /*tound*/printf("%s",str);fgets(str,81,fr);)二、程序改错题#include <stdio.h>/*found*/ void fun (Ion。 s long* t) long sl=10;*t = s % 10;while ( s > 0) s = s/100;*t = s%10 * si + *t;/* *found* * */ si = si "10*main() long s, t;printf("nPlease enter s:"); scanf("%ld", &s);fun(s, &t);printf("The result is: %ldn", t);第3套题一、程序填空题#include<math.h>#include<stdio.h>int fun(char *fname) FILE *fp; int i,n; float x;if(fp=fopen(fname, "w")=NULL) return 0;for(i= 1 ;i<= 10;i+)/* * *found */fprintf(fp,"%d %fn",i,sqrt(double)i);printf("nSucceed! n");/*tound*/Fclose(fp);printf("nThe data in file :n");/*found*/if(fp=fopen(myfile3.tex,"r")=NULL)return 0;fscanf(fp,"%d%f",&n,&x);while(!feof(fp) printf("%d %fn",n,x); fscanf(fp,"%d%f",&n,&x); fclose(fp);return 1;)main() char fname="myfile3.txt"fun(fname);二、程序改错题#include &It;stdio.h>#include <stdlib.h>void fun (int n, int *a ) int i,j, p, t;for (j = 0; j<n-l ; j+ ) p=j;/*,0und*/for (i=j+l; i<n ; i+ )if (ap>ai)/* * * *found* */ p=i;if(p!=j) t = a|j; a|j = ap; ap = t; )void putarr( int n, int *z) int i;for (i = 1; i <= n; i+, z+ ) printf( "%4d", *z );if( !(i%10) printf( "n"); printf("n");main() int aa20=9,3,0,4,1,2,5,6,8,10,7, n= 11;printf( "nnBefore sorting %d numbers:n"» n ); putarr( n, aa );printf( "nAfter sorting %d numbers:n", n ); putarr( n, aa ); fun( n, aa);第4套题一、程序填空题#include<stdio.h>#include<stdlib.h>int fun(char *source, char *target) /*found*/if(fs=fopen(source, "r" )=NULL) if(ft=fopen(target, "w")=NULL)char ch;return 0;return 0;printf("nThe data in file :n"); ch=fgetc(fs);/*found*/ putchar( ch );/*found*/ ch=fgetc(fs);while(!feof(fs)fputc(ch,ft);fclose(fs); fclose(ft);printf("nn");return二、程序改错题#include <stdio.h>void fun (long s, long *t) int d;long sl=l;*t = 0;while ( s > 0) d = s%10;if(d%2=0)s/= 10;/*ound*/ *t=d* sl+ *t;si *= 10;)/*tound*/main() long s, t;printf("nPlease enter s:"); scanf("%ld", &s); fun(s, &t); printf("The result is: %ldn", t);第5套题一、程序填空题#include<stdio.h>#include<stdlib.h>#define N 8 typedef struct list int data; struct list *next; SLIST;void fun( SLIST *h, int x) SLIST *p, *q, *s;s=(SLIST *)malloc(sizeof(SLIST); /*found*/ s->data=x;q=h;p=h->next;while(p!=NULL && x>p->data) /* * *found */ q=p;p=p->next;s->next=p;/*found*/q->next=s;SLIST *creatlist(int *a) SLIST *h,*p,*q; int i;h=p=(SLIST*)malloc(sizeof(SLIST);for(i=0; i<N; i+) q=(SLIST *)malloc(sizeof(SLIST); q->data=ai; p->next=q; p=q;p->next=O;return h;1void outlist(SLIST *h) SLIST *p;p=h->next;if (p=NULL) printf("nThe list is NULL!n"); else printf("nHead");do printf("->%d",p->data); p=p->next; while(p!=NULL); printf("->Endn");二、程序改错题long fun (long num)/*found*/ long k=l;do k*=num%10 ;/*iound*/ Num/=10 ; while(num);return (k);main() long n ;scanf("%ld",&n)printf("nPlease enter a number:") printf("n%ldn",fun(n);第6套题一、程序填空题#include<stdio.h>#include<stdlib.h>#define N 8 typedef struct list int data; struct list *next; SLIST;void fun( SLIST *p) SLIST *t, *s;t=p->next; s=p;while(t->next != NULL) s=t;/*found* 不/ t=t->next;/* * found * */printf(" %d ",t->data);s->next=NULL;/* * * found */ free(t);)SLIST *creatlist(int *a) SLIST *h,*p,*q; int i;h=p=(SLIST*)malloc(sizeof(SLIST);for(i=0; i<N; i+) q=(SLIST *)malloc(sizeof(SLIST); q->data=ai; p->next=q; p=q;)p->next=0;return h;void outlist(SLIST *h) SLIST *p;p=h->next;if (p=NULL) printf("nThe list is NULL!n"); else printf("nHead");do printf("->%d",p->data); p=p->next; while(p!=NULL); printf("->Endn");二、程序改错题#include <stdio.h>/* * fou nd * */ fun (char"*a) if(*a) fun(a+l);/* * *fk'iund * */ printf("%c", *a);main() char s10="abcd"printf("处理前字符串=%sn处理后字符串二", s);fun(s); printf("n");1第7套题一、程序填空题#include<stdio.h>#include<stdlib.h>#define N 8 typedef struct list int data; struct list *next; SLIST;void fun( SLIST *h) SLIST *p, *q; p=h->next; if(p!=NULL) q=p->next; while(q!=NULL) if (p->data=q->data) p->next=q->next;/*found*/free(q);/* * found */q=p->next;)elsep=q;/*nd */q=q->next;) ) ) )SLIST *creatlist(int *a) SLIST *h,*p,*q;int i;h=p=(SLIST *)malloc(sizeof(SLIST);for(i=0;i<N; i+) q=(SLIST *)malloc(sizeof(SLIST); p->next=0;q->data=ai; p->next=q; p=q; return h;I二、程序改错题#include <stdio.h>#define N 20void fun(int a, int n) int i,j, t, p;for (j = 0 ;j < n-1 ;j+) /*iound*/P=J;for (i=j+l;i < n; i+)if(ai < ap)/*found*/p=l;t = ap; ap = aj ; aj = t;main()int aN=9,6,8,3,-l),i,m = 5;printf("排序前的数据:");fun(a,m);for(i = 0;i < m;i+) printf("%d ",ai); printf("n");printf("排序后的数据:");for(i = 0;i < m;i+) printf("%d ",ai); printf("n"); 第8套题一、程序填空题#include &!t;stdio.h>#include <stdlib.h>#define N 8 typedef struct list int data;struct list *next; SLIST;SLIST *creatlist(char *);void outlist(SLIST *);int fun( SLIST *h, char ch) SLIST *p; int n=0;p=h->next;/* * found */while(p!=NULL)n+;/*lound*/if (p->data=ch) return n;else p=p->next;return 0;)main() SLIST *head;int k;char ch;charhead=creatlist(a);outlist(head);printf("Enter a letter:");scanf("%c",&ch);/*found*不/ k=fun(head,ch);if (k=0) printf("nNot found!n");else printf("The sequence number is : %dn",k); SLIST *creatlist(char *a) SLIST *h,*p,*q; int i;h=p=(SLIST *)malloc(sizeof(SLIST);for(i=0; i<N; i+) q=(SLIST *)malloc(sizeof(SLIST); q->data=aij; p->next=q; p=q; p->next=0;return h;void outlist(SLIST *h) SLIST *p;p=h->next; if (p=NULL) printf("nThe list is NULL!n"); else printf("nHead"); do printf("->%c",p->data); p=p->next; ) while(p!=NULL); printf("->Endn");二、程序改错题#include <string.h>#include <stdio.h>#include <ctype.h>fun ( char *p) int i,t; char c80;/*found*/ for (i = 0 t = pi , i+)if(!isspace(*(p+i) cl+=pi; /*found*/ct= '0'strcpy(p.c); main() char c,s80;int i=0;printf("Input a string:");c=getchar();while(c!=<r) si=c;i+;c=getchar(); si=40fun(s);puts(s);第9套题 一、程序填空题#include<stdio.h>#include<stdlib.h>#define N 8 typedef struct list int data; struct list *next; SLIST;SLIST *creatlist(int *a); void outlist(SLIST *); void fun( SLIST *h, int *n) SLIST *p; /* * * found */ *n=0; p=h->next; while(p) (*n)+;/*ound*/ p=p->next; ) ) main() SLIST *head;int aN= 12,87,45,32,91,16,20,48), num; head=creatlist(a); outlist(head); /*found*/fun(head, &num);printf("nnumber=%dn",num); SLIST *creatlist(int aJ) SLIST *h,*p,*q;int i; h=p=(SLIST *)malloc(sizeof(SLIST); for(i=0;i<N; i+) q=(SLIST *)malloc(sizeof(SLIST); q->data=ai; p->next=q; p=q; p->next=0; return h;二、程序改错题#include <stdio.h>#include <string.h>char * fun (char *s, char *t)char *p , *r, *a;/*found*/ a = NULL;while ( *s) p = s; r = t;while ( *r)/* * *nd * */if (* r =* p ) r+; p+; else break;if(*r=tO,)a = s;s+;return a;main()char s100,t100, *p;printf("nPlease enter string S :"); scanf("%s", s );printf("nPlease enter substring t :"); scanf("%s", t); p = fun( s, t);if ( p ) printf("nThe result is : %sn", p); else printf("nNot found !n");第10套题一、程序填空题#include<stdio.h>#include<stdlib.h>#define N 8 typedef struct list int data; struct list *next; SLIST;SLIST *creatlist(int *); void outlist(SLIST *); int fun( SLIST *h) SLIST *p; while(p)int s=0;/*ound*/*found*/p=h->next;s+= p->data;p=p->next;return s;main() SLIST *head;head=creatlist(a);outlist(head);int aN= 12,87,45,32,91,16,20,48);/* fou nd */printf("nsum=%dn", fun(head); SLIST *creatlist(int a) SLIST *h,*p,*q;int i; h=p=(SLIST *)malloc(sizeof(SLIST); for(i=0;i<N; i+) q=(SLIST *)malloc(sizeof(SLIST); q->data=ai; p->next=q; p=q; p->next=0;return h;二、程序改错题#include <stdio.h>#include <string.h>void fun (char *s, char *tl, char *t2 , char *w) char *p , *r, *a;strcpy( w, s);while ( *w) p = w; r = tl;/* *不fou nd */while (* r)if(*r=*p) r+; p+; else break;if(*r=A0,) a = w; r = t2;while ( *r)/* 代 und*/*a = *r; a+; r+;w += strlen(t2);)else w+;)第11套题一、程序填空题#include<stdio.h>#include<string.h>#defineM5#defineN20int fun(char (*ss)N, int *n) /* x found * */ len=strlen(ssi);if(i=0) *n=len;/* * * u nd * / *n=len;k=i;/*found* */)二、程序改错题int i, k=0, len= N;for(i=0; i<M; i+)if(len < *n)return(k);#include <stdio.h>#include <string.h> char* fun( char tt)int i;for(i = 0; tti; i+ )/* * found */* found * */return( tt);if(匕<= tti )&&( tti <=,z') tti-= 32;main()char tt81;printf( "nPlease enter a string: "); gets( tt); printf( "nThe result string is:n%s", fun( tt);第12套题一、程序填空题#include <stdio.h>void fun(char *s) int i, j=0, k=0; char tl80, t280; for(i=0; si!=40 i+)if(si>=<0, && si<=49,)/* 豕/t2j=si;j+;else tlk+=sij;t2j=0; tlk=O;/* * found */* 不found * *不/for(i=0; i<k; i+)for(i=0; i<j; i+) sk+i=t2i; main() char s80J="def35adh3kjsdf7"printf("nThe original string is : %sn",s); fun(s);printf("nThe result is : %sn",s); 二、程序改错题#include <stdio.h>#include <string.h>#define MAXLINE 20fun (char *pstr6) int i,j ; char *p;for (i = 0 ; i < 5 ; i+ ) /* * found 不*/for (j = i + 1; j < 6; j+)if(strcmp(*(pstr + i), *(pstr + j) > 0)p = *(pstr + i);/* *fou nd */*(pstr + j) = p ;*(pstr + i) = *(pstr + j );第13套题一、程序填空题#include <stdio.h>void fun(char *s) int i, j, n;for(i=0; si!=t0,; i+)/* nd */if(si>=40, && si<=49,) n=0;/*found*/while(si+1 +nj != 0) n+;/*found*/s|j+l= sj; s|j+lk*'; i=i+l;for(j=i+n+l;j>i; j-)main() char s80="ba3a54cd23a"printf("nThe original string is : %sn",s); fun(s);printf("nThe result is : %sn",s); 二、程序改错题#include <stdio.h>double fun (int m ) double y = 1.0 ;int i;for(i = 2 ; i <= m ; i+)y+= 1.0/(i*i);/* *fou nd */ /*1ound*