2015年计算机二级C语言上机操作题及答案(56)
发布时间:2011/7/17 18:20:31 来源:城市学习网 编辑:ziteng
第一题;填空题:
请补充main函数,该函数的功能是把文本文件B中的内容追加到文本文件A的内容之后。
例如,文件B的内容为“I’m 12.”,文件A的内容为“I’m a students!”,追加之后文件A的内容为”I’m a students!I’m 12.”
仅在横线上填入所编写的若干表达式或语句,勿改动函数中的其他任何内容。
#include
#include
#define N 80
main()
{
FILE *fp, *fp1, *fp2;
int i;
char c[N], ch;
fp = fopen("A.dat", "w");
fprintf(fp, "I'm File A.dat!\n");
fclose(fp);
fp = fopen("B.dat", "w");
fprintf(fp, "I'm File B.dat!\n");
fclose(fp);
if ((fp = fopen("A.dat", "r")) == NULL)
{
printf("file A cannot be opened\n");
exit(0);
}
printf("\n A contents are :\n\n");
for (i=0; (ch = fgetc(fp))!=EOF; i++)
{
c[i] = ch;
putchar(c[i]);
}
fclose(fp);
if ((fp=fopen("B.dat", "r")) == NULL)
{
printf("file B cannot be opened\n");
exit(0);
}
printf("\n\n\nB contents are :\n\n");
for (i=0; (ch = fgetc(fp))!=EOF; i++)
{
c[i] = ch;
putchar(c[i]);
}
fclose(fp);
if ((fp1=fopen("A.dat", "a")) ___1___ (fp2=fopen("B.dat", "r")))
{
while ((ch=fgetc(fp2)) != EOF)
___2___;
}
else
{
printf("Can not open A B !\n");
}
fclose(fp2);
fclose(fp1);
printf("\n********new A contents*********\n\n");
if ((fp=fopen("A.dat", "r")) == NULL)
{
printf("file A cannot be opened\n");
exit(0);
}
for (i=0; (ch=fgetc(fp))!=EOF; i++)
{
c[i] = ch;
putchar(c[i]);
}
___3___;
}
答案
第一处填空:&&
第二处填空:fput c (ch,fp1)
第三处填空:fclose (fp) [NextPage] 第二题;改错题
下列定程序中,函数fun的功能是;用冒泡法对6个字符串按由小到大的顺序进行排序。
请改正程序中的错误。使它能得出正确结果。
注意:不要改动main函数,不得增加或删行,也不得更改程序的结构!
#include
#include
#define MAXLINE 20
/********found********/
void fun(char (*pstr)[6])
{
int i, j;
char *p;
for (i=0; i<5; i++)
{
for (j=i+1; j<6; j++)
{
/********found********/
if(strcmp(*(pstr + i), pstr + j) > 0)
{
p = *(pstr+i);
/********found********/
*(pstr + i) = pstr + j;
*(pstr+j) = p;
}
}
}
}
main()
{
int i;
char *pstr[6], str[6][MAXLINE];
for (i=0; i<6; i++)
pstr[i] = str[i];
printf("\nEnter 6 sting(1 sting at each line): \n");
for (i=0; i<6; i++)
scanf("%s", pstr[i]);
fun(pstr);
printf("The strings after sorting:\n");
for (i=0; i<6; i++)
printf("%s\n", pstr[i]);
}
答案
第一处改错:double fun (char *s, char *t) 应改为 char *fun( char *s, char *t)
第二处改错:(*ss)++; 应改为 ss++;
第三处改错:(*tt)++; 应改为 tt++; [NextPage] main()
{
char s[81],*t;
FILE *out ;
printf("Enter a string:\n");
gets(s);
t=s;
while(*t)
t++;
t--;
while(*t=='*')
t--;
fun( s , t );
printf("The string after deleted:\n");
puts(s);
out=fopen ("out.dat","w");
strcpy(s, "****A*BC*DE*F*G********");
fun(s, s+14);
fprintf(out, "%s", s);
fclose (out );
}
答案
void fun (fun *a,char *p)
{
int i=0;
char *p=a;
while (q<=p)
{
if (*q!=’*’)
{
a[i]=*q;
i++;
}
q++;
}
while {*q}
{
a[i]=*q;
i++;
q++;
}
a[i]=’\0’;
}