时间:2011-12-24 23:50:44
第4套题
一、程序填空题
#include <stdio.h>
#include <stdlib.h>
int fun(char *source, char *target)
{ FILE *fs,*ft; char ch;
/**********found**********/
if((fs=fopen(source, ___1___))==NULL)
return 0;
if((ft=fopen(target, "w"))==NULL)
return 0;
printf("nThe data in file :n");
ch=fgetc(fs);
/**********found**********/
while(!feof(___2___))
{ putchar( ch );
/**********found**********/
fputc(ch,___3___);
ch=fgetc(fs);
}
fclose(fs); fclose(ft);
printf("nn");
return 1;
}
main()
{ char sfname[20] ="myfile1",tfname[20]="myfile2";
FILE *myf; int i; char c;
myf=fopen(sfname,"w");
printf("nThe original data :n");
for(i=1; i<30; i++){ c='A'+rand()%25;fprintf(myf,"%c",c); printf("%c",c); }
fclose(myf);printf("nn");
if (fun(sfname, tfname)) printf("Succeed!");
else printf("Fail!");
}
二、程序改错题
#include <stdio.h>
void fun (long s, long *t)
{ int d;
long sl=1;
*t = 0;
while ( s > 0)
{ d = s%10;
/************found************/
if (d%2=0)
{ *t=d* sl+ *t;
sl *= 10;
}
/************found************/
s = 10;
}
}
main()
{ long s, t;
printf("nPlease enter s:"); scanf("%ld", &s);
fun(s, &t);
printf("The result is: %ldn", t);
}
三、程序编写题
#include <stdio.h>
#include <string.h>
void fun(char *s, char t[])
{
}
main()
{
char s[100], t[100];
void NONO ( );
printf("nPlease enter string S:"); scanf("%s", s);
fun(s, t);
printf("nThe result is: %sn", t);
NONO();
}
void NONO ( )
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
char s[100], t[100] ;
FILE *rf, *wf ;
int i ;
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, "%sn", t) ;
}
fclose(rf) ;
fclose(wf) ;
}