二级C语言题库.docx
《二级C语言题库.docx》由会员分享,可在线阅读,更多相关《二级C语言题库.docx(178页珍藏版)》请在冰豆网上搜索。
![二级C语言题库.docx](https://file1.bdocx.com/fileroot1/2022-11/24/e6f1df57-bbe2-488a-828e-e978bb849f78/e6f1df57-bbe2-488a-828e-e978bb849f781.gif)
二级C语言题库
第1套题
一、程序填空题
#include
#include
#defineN5
typedefstruct
{intnum;
charname[10];
chartel[10];
}STYPE;
voidcheck();
/**********found**********/
intfun(___1___*std)
{
/**********found**********/
___2___*fp;inti;
if((fp=fopen("myfile5.dat","wb"))==NULL)
return(0);
printf("\nOutputdatatofile!
\n");
for(i=0;i/**********found**********/
fwrite(&std[i],sizeof(STYPE),1,___3___);
fclose(fp);
return
(1);
}
main()
{STYPEs[10]={{1,"aaaaa","111111"},{1,"bbbbb","222222"},{1,"ccccc","333333"},
{1,"ddddd","444444"},{1,"eeeee","555555"}};
intk;
k=fun(s);
if(k==1)
{printf("Succeed!
");check();}
else
printf("Fail!
");
}
voidcheck()
{FILE*fp;inti;
STYPEs[10];
if((fp=fopen("myfile5.dat","rb"))==NULL)
{printf("Fail!
!
\n");exit(0);}
printf("\nReadfileandoutputtoscreen:
\n");
printf("\nnumnametel\n");
for(i=0;i{fread(&s[i],sizeof(STYPE),1,fp);
printf("%6d%s%s\n",s[i].num,s[i].name,s[i].tel);
}
fclose(fp);
}
二、程序改错题
#include
#include
voidfun(char*s,char*t)
{inti,sl;
sl=strlen(s);
/************found************/
for(i=0;i<=s1;i++)
t[i]=s[i];
for(i=0;it[sl+i]=s[sl-i-1];
/************found************/
t[sl]='\0';
}
main()
{chars[100],t[100];
printf("\nPleaseenterstrings:
");scanf("%s",s);
fun(s,t);
printf("Theresultis:
%s\n",t);
}
三、程序编写题
#include
voidfun(inta,intb,long*c)
{
}
main()
{inta,b;longc;
voidNONO();
printf("Inputab:
");scanf("%d%d",&a,&b);
fun(a,b,&c);
printf("Theresultis:
%d\n",c);
NONO();
}
voidNONO()
{/*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。
*/
FILE*rf,*wf;
inti,a,b;longc;
rf=fopen("K:
\\k01\\24010001\\in.dat","r");
wf=fopen("K:
\\k01\\24010001\\out.dat","w");
for(i=0;i<10;i++){
fscanf(rf,"%d,%d",&a,&b);
fun(a,b,&c);
fprintf(wf,"a=%d,b=%d,c=%ld\n",a,b,c);
}
fclose(rf);
fclose(wf);
}
第2套题
一、程序填空题
#include
#include
#include
voidWriteText(FILE*);
voidReadText(FILE*);
main()
{FILE*fp;
if((fp=fopen("myfile4.txt","w"))==NULL)
{printf("openfail!
!
\n");exit(0);}
WriteText(fp);
fclose(fp);
if((fp=fopen("myfile4.txt","r"))==NULL)
{printf("openfail!
!
\n");exit(0);}
ReadText(fp);
fclose(fp);
}
/**********found**********/
voidWriteText(FILE*fw___1___)
{charstr[81];
printf("\nEnterstringwith-1toend:
\n");
gets(str);
while(strcmp(str,"-1")!
=0){
/**********found**********/
fputs(___2_str__,fw);fputs("\n",fw);
gets(str);
}
}
voidReadText(FILE*fr)
{charstr[81];
printf("\nReadfileandoutputtoscreen:
\n");
fgets(str,81,fr);
while(!
feof(fr)){
/**********found**********/
printf("%s",___3__str_);
fgets(str,81,fr);
}
}
二、程序改错题
#include
/************found************/
voidfun(longs,longt)
{longsl=10;
*t=s%10;
while(s>0)
{s=s/100;
*t=s%10*sl+*t;
/************found************/
sl=sl*100;
}
}
main()
{longs,t;
printf("\nPleaseenters:
");scanf("%ld",&s);
fun(s,&t);
printf("Theresultis:
%ld\n",t);
}
三、程序编写题
#include
#defineN16
typedefstruct
{charnum[10];
ints;
}STREC;
intfun(STREC*a,STREC*b)
{
}
main()
{STRECs[N]={{"GA05",85},{"GA03",76},{"GA02",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}};
STRECh[N];
inti,n;FILE*out;
n=fun(s,h);
printf("The%dlowestscore:
\n",n);
for(i=0;iprintf("%s%4d\n",h[i].num,h[i].s);
printf("\n");
out=fopen("K:
\\k01\\24010001\\out.dat","w");
fprintf(out,"%d\n",n);
for(i=0;ifprintf(out,"%4d\n",h[i].s);
fclose(out);
}
第3套题
一、程序填空题
#include
#include
intfun(char*fname)
{FILE*fp;inti,n;floatx;
if((fp=fopen(fname,"w"))==NULL)return0;
for(i=1;i<=10;i++)
/**********found**********/
fprintf(__fp_1___,"%d%f\n",i,sqrt((double)i));
printf("\nSucceed!
!
\n");
/**********found**********/
_fclose(fp)__2___;
printf("\nThedatainfile:
\n");
/**********found**********/
if((fp=fopen(___3___,"r"))==NULL)
return0;
fscanf(fp,"%d%f",&n,&x);
while(!
feof(fp))
{printf("%d%f\n",n,x);fscanf(fp,"%d%f",&n,&x);}
fclose(fp);
return1;
}
main()
{charfname[]="myfile3.txt";
fun(fname);
}
二、程序改错题
#include
#include
voidfun(intn,int*a)
{inti,j,p,t;
for(j=0;j{p=j;
/************found************/
for(i=j+1;iif(a[p]>a[i])
/************found************/
t=i;
if(p!
=j)
{t=a[j];a[j]=a[p];a[p]=t;}
}
}
voidputarr(intn,int*z)
{inti;
for(i=1;i<=n;i++,z++)
{printf("%4d",*z);
if(!
(i%10))printf("\n");
}printf("\n");
}
main()
{intaa[20]={9,3,0,4,1,2,5,6,8,10,7},n=11;
printf("\n\nBeforesorting%dnumbers:
\n",n);putarr(n,aa);
fun(n,aa);
printf("\nAftersorting%dnumbers:
\n",n);putarr(n,aa);
}
三、程序编写题
#include
voidfun(inta,intb,long*c)
{
}
main()
{inta,b;longc;
voidNONO();
printf("Inputab:
");
scanf("%d%d",&a,&b);
fun(a,b,&c);
printf("Theresultis:
%ld\n",c);
NONO();
}
voidNONO()
{/*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。
*/
FILE*rf,*wf;
inti,a,b;longc;
rf=fopen("K:
\\k01\\24010001\\in.dat","r");
wf=fopen("K:
\\k01\\24010001\\out.dat","w");
for(i=0;i<10;i++){
fscanf(rf,"%d,%d",&a,&b);
fun(a,b,&c);
fprintf(wf,"a=%d,b=%d,c=%ld\n",a,b,c);
}
fclose(rf);
fclose(wf);
}
第4套题
一、程序填空题
#include
#include
intfun(char*source,char*target)
{FILE*fs,*ft;charch;
/**********found**********/
if((fs=fopen(source,___’r”1___))==NULL)
return0;
if((ft=fopen(target,"w"))==NULL)
return0;
printf("\nThedatainfile:
\n");
ch=fgetc(fs);
/**********found**********/
while(!
feof(___2___))
{putchar(ch);
/**********found**********/
fputc(ch,___3___);
ch=fgetc(fs);
}
fclose(fs);fclose(ft);
printf("\n\n");
return1;
}
main()
{charsfname[20]="myfile1",tfname[20]="myfile2";
FILE*myf;inti;charc;
myf=fopen(sfname,"w");
printf("\nTheoriginaldata:
\n");
for(i=1;i<30;i++){c='A'+rand()%25;fprintf(myf,"%c",c);printf("%c",c);}
fclose(myf);printf("\n\n");
if(fun(sfname,tfname))printf("Succeed!
");
elseprintf("Fail!
");
}
二、程序改错题
#include
voidfun(longs,long*t)
{intd;
longsl=1;
*t=0;
while(s>0)
{d=s%10;
/************found************/
if(d%2=0)
{*t=d*sl+*t;
sl*=10;
}
/************found************/
s\=10;
}
}
main()
{longs,t;
printf("\nPleaseenters:
");scanf("%ld",&s);
fun(s,&t);
printf("Theresultis:
%ld\n",t);
}
三、程序编写题
#include
#include
voidfun(char*s,chart[])
{
}
main()
{
chars[100],t[100];
voidNONO();
printf("\nPleaseenterstringS:
");scanf("%s",s);
fun(s,t);
printf("\nTheresultis:
%s\n",t);
NONO();
}
voidNONO()
{/*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。
*/
chars[100],t[100];
FILE*rf,*wf;
inti;
rf=fopen("K:
\\k01\\24010001\\in.dat","r");
wf=fopen("K:
\\k01\\24010001\\out.dat","w");
for(i=0;i<10;i++){
fscanf(rf,"%s",s);
fun(s,t);
fprintf(wf,"%s\n",t);
}
fclose(rf);
fclose(wf);
}
第5套题
一、程序填空题
#include
#include
#defineN8
typedefstructlist
{intdata;
structlist*next;
}SLIST;
voidfun(SLIST*h,intx)
{SLIST*p,*q,*s;
s=(SLIST*)malloc(sizeof(SLIST));
/**********found**********/
s->data=___1___;
q=h;
p=h->next;
while(p!
=NULL&&x>p->data){
/**********found**********/
q=___2___;
p=p->next;
}
s->next=p;
/**********found**********/
q->next=___3___;
}
SLIST*creatlist(int*a)
{SLIST*h,*p,*q;inti;
h=p=(SLIST*)malloc(sizeof(SLIST));
for(i=0;i{q=(SLIST*)malloc(sizeof(SLIST));
q->data=a[i];p->next=q;p=q;
}
p->next=0;
returnh;
}
voidoutlist(SLIST*h)
{SLIST*p;
p=h->next;
if(p==NULL)printf("\nThelistisNULL!
\n");
else
{printf("\nHead");
do{printf("->%d",p->data);p=p->next;}while(p!
=NULL);
printf("->End\n");
}
}
main()
{SLIST*head;intx;
inta[N]={11,12,15,18,19,22,25,29};
head=creatlist(a);
printf("\nThelistbeforeinserting:
\n");outlist(head);
printf("\nEnteranumber:
");scanf("%d",&x);
fun(head,x);
printf("\nThelistafterinserting:
\n");outlist(head);
}
二、程序改错题
#include
longfun(longnum)
{
/************found************/
longk;
do
{k*=num%10;
/************found************/
num\=10;
}while(num);
return(k);
}
main()
{longn;
printf("\nPleaseenteranumber:
");scanf("%ld",&n);
printf("\n%ld\n",fun(n));
}
三、程序编写题
#include
floatfun(float*a,intn)
{
}
main()
{floatscore[30]={90.5,72,80,61.5,55},aver;
voidNONO();
aver=fun(score,5);
printf("\nAveragescoreis:
%5.2f\n",aver);
NONO();
}
voidNONO()
{/*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。
*/
FILE*fp,*wf;
inti,j;
floataver,score[5];
fp=fopen("K:
\\k01\\24010001\\in.dat","r");
wf=fopen("K:
\\k01\\24010001\\out.dat","w");
for(i=0;i<10;i++){
for(j=0;j<5;j++)fscanf(fp,"%f,",&score[j]);
aver=fun(score,5);
fprintf(wf,"%5.2f\n",aver);
}
fclose(fp);
fclose(wf);
}
第6套题
一、程序填空题
#include
#include
#defineN8
typedefstructlist
{intdata;
structlist*next;
}SLIST;
voidfun(SLIST*p)
{SLIST*t,*s;
t=p->next;s=