【真题1】 给定程序的功能是:调用函数fun 将指定源文件中的内容复制到指定的 目标文件中,复制成功时函数返回值是1,失败时返回值为0。在复制的过程中,把 复制的内容输出到终端屏幕。主函数中源文件名放在变量sfname 中,目标文件名放 在变量tfname 中。 #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("\n\n"); 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("\n\n"); if(fun(sfname, tfname)) printf("Succeed!"); else printf("Fail!"); } (2011年2月) 解析: 本题的考核点是文件的读写函数等。 题中第一个空填""r"":"r"是文件的使用方式之一,其含义是为输入打开一个文 件用于输入。 题中第二个空填"fs":此语句是设置循环退出条件,即判断是否到文件末尾。 题中第三个空填"ft":fputc 函数功能是把一个字符写到磁盘文件上去。其一般 调用形式为: fputc(ch,fp); 其中ch 是要输出的字符,它可以是一个字符常量,也可以是一个字符变量。fp 是文件指针变量。 答案:【1】“r” 【2】fs 【3】ft 【真题2】 给定程序modi.c 中,函数fun 的功能是:首先把b 所指字符串中的字 符按逆序存放,然后将a 所指字符串中的字符和b所指字符串中的字符,按排列的 顺序交叉合并到c 所指数组中,过长的剩余字符接在c 所指数组的尾部。 例如,当 a 所指字符串中的内容为"abcdefg",b 所指字符串中的内容为"1234"时,c 所指数 组中的内容应该为"a4b3c2d1efg";而当a 所指字符串中的内容为"1234",b 所指 字符串中的内容为"abcdefg"时,c 所指数组中的内容应该为"1g2f3e4dcba"。 请 改正程序中的错误,使它能得出正确结果。 注意:不要改动main 函数。 #include < stdio.h > #include < string.h > #include < conio.h > fun(char *a, char *b, char *c ) { /************found************/ char ap, at, ch; ap=b; at=b+strlen(b) - 1; while (ap <= at) { ch=*ap; *ap=*at; *at=ch; ap++; at--; } while (*a||*b) /************found************/ { if(*a ){*c=*a; c++; a++;} if(*b ){*c=*b; c++; b++;} } *c=0; } main() { char s1[100], s2[100], t[200]; clrscr(); printf("\nEnter s1 string:"); scanf("%s",s1); printf("\nEnter s2 string:"); scanf("%s",s2); fun(s1, s2, t); printf("\nThe result is:%s\n", t); } (2011年2月) 解析: (1)错误:char ap, at, ch; 正确:char *ap,at,ch; (2)错误:if(*a) {*c=*a;c++;a++} 正确:if(*a) {*c=*a;c++;a++;} 【真题3】 编写一个函数,从传入的num 个字符串找出最长的一个字符串,并通过 形参指针max传回该串地址。(注意:用****作为结束输入的标志。) 注意:部 分源程序存在文件prog.c 中。 请勿改动主函数main 和其他函数中的任何内容,仅 在函数fun 的花括号中填入你编写的若干语句。 #include < stdio.h > #include < string.h > #include < conio.h > fun(char(*a)[81], int num, char **max) {……} main() { char ss[10][81],*ps; int n,i=0; clrscr(); printf("输入若干个字符串:"); gets(ss[i]); puts(ss[i]); while(!strcmp(ss[i],"****")==0) { i++; gets(ss[i]); puts(ss[i]); } n=i; fun(ss,n,&ps); printf("\nmax=%s\n",ps); } (2011年2月) 解析: 本题的考核点是从一组字符串中找出最长串的算法。 解题思路:通过求 字符串长度函数对一组字符串的字符进行比较,并将地址赋给一指针变量。 fun(char(*a)[81], int num, char **max) { int i; *max=a[0];/*将第一个字符串赋值给max 指针*/ for (i=1; i<=num; i++) /*假如字符串a[i]的长度比指针max 中的字符串的长度长,则将a[i] 字符串赋值给max 指针*/ if (strlen(a[i])>strlen(*max)) *max=a[i]; } |