1、三级数据库技术-523 及答案解析(总分:100.00,做题时间:90 分钟)一、设计与应用题(总题数:1,分数:100.00)1.程序 test.c 的功能是:把 s 字符串中的所有字符左移一个位置,串中的第一个字符移到最后。请考生编写函数 chg(char*s)实现程序要求,最后调用函数 readwriteDAT()把结果输出到 out.dat 文件中。 例如:s 字符串中原有内容为:Mn.123xyZ,则调用该函数后,结果为:n.123xyZM。 请勿改动数据文件 in.dat 中的任何数据、主函数 main()和输出函数 readwriteDAT()的内容。 #include stdi
2、o.h #include string.h #define N 81 /定认宏变量 N, 其值等于 81 void readwriteDAT(); void chg(char *s) main () char a N ; printf(“Enter a string:“); gets(a); printf(“The original string is: “); puts(a); chg (a); printf (“The string after modified:“); puts (a); readwriteDAT (); /*从文件 in.dat 中读入字符串赋给字符串变量 a,调用函数
3、 chg(a)对字符串按照题目的要求进行处理,并把处理结果写入到文件 out.dat 中*/ void readwriteDAT() int i; char a N; FILE *rf, *wf; rf=fopen(“in.dat“, “r“); wf=fopen(“out.dat“, “w“); for(i=0; i10; i+) fscanf (rf, “%s“, a); /从文件 in.dat 中读取一个字符串赋给字符串 a chg (a); /调用函数 chg(a)对字符串 a 进行处理 fprintf(wf, “%sn“, a); /把处理结果写入到 out.dat 文件中 fclo
4、se(rf); fclose (wf); (分数:100.00)_三级数据库技术-523 答案解析(总分:100.00,做题时间:90 分钟)一、设计与应用题(总题数:1,分数:100.00)1.程序 test.c 的功能是:把 s 字符串中的所有字符左移一个位置,串中的第一个字符移到最后。请考生编写函数 chg(char*s)实现程序要求,最后调用函数 readwriteDAT()把结果输出到 out.dat 文件中。 例如:s 字符串中原有内容为:Mn.123xyZ,则调用该函数后,结果为:n.123xyZM。 请勿改动数据文件 in.dat 中的任何数据、主函数 main()和输出函数
5、readwriteDAT()的内容。 #include stdio.h #include string.h #define N 81 /定认宏变量 N, 其值等于 81 void readwriteDAT(); void chg(char *s) main () char a N ; printf(“Enter a string:“); gets(a); printf(“The original string is: “); puts(a); chg (a); printf (“The string after modified:“); puts (a); readwriteDAT (); /
6、*从文件 in.dat 中读入字符串赋给字符串变量 a,调用函数 chg(a)对字符串按照题目的要求进行处理,并把处理结果写入到文件 out.dat 中*/ void readwriteDAT() int i; char a N; FILE *rf, *wf; rf=fopen(“in.dat“, “r“); wf=fopen(“out.dat“, “w“); for(i=0; i10; i+) fscanf (rf, “%s“, a); /从文件 in.dat 中读取一个字符串赋给字符串 a chg (a); /调用函数 chg(a)对字符串 a 进行处理 fprintf(wf, “%sn“
7、, a); /把处理结果写入到 out.dat 文件中 fclose(rf); fclose (wf); (分数:100.00)_正确答案:()解析:int i; char first, *p=s; first=*s; /把字符串 s 的第一个字符赋给 first /*把 s 字符串中的所有字符左移一个位置,串中的第一个字符移到最后*/ for(i=0; i(int) strlen(s)-1; i+) *(p+i)=*(p+i+1); /字符*(p+i+1)左移一位 pstrlen(s)-1=first; /第一个字符移动到字符串 s 的最后位置 解析 所有字符左移一个位置,串中的第一个字符移到最后。 定义循环变量 i,字符变量 first 和字符指针变量 p,并使 p 指向字符串 s。 把字符串 s 的第一个字符赋给。first 变量保存起来。 在 for 循环语句中,循环变量 i 从 0 开始,依次递增直到其值等于或大于(int)strlen(s)-1,把*(p+i+1)的值赋给*(p+i),实现把字符串 s 中的所有字符左移一个位置,循环结束后,把 first 的值赋给pstrlen(s)-1,即把字符串的首字符移动到字符串的最后位置上。