1、计算机三级数据库技术-126 及答案解析(总分:100.00,做题时间:90 分钟)一、上机题(总题数:1,分数:100.00)1.函数 ReadDat()的功能是从文件 IN.DAT 中读取一篇英文文章存入到字符串数组 xx 中。请编写函数StrOR(),该函数的功能是:以行为单位把字符串中所有小写字母“o”左边的字符串内容移至该串的右边存放,然后把小写字母“o”删除,余下的字符串内容移到已处理字符串的左边存放,最后把已处理的字符串仍按行重新存入字符串数组 xx 中。最后调用函数 writeDat(),把结果输出到文件 OUT.DAT 中。 例如,原文:You can create an i
2、ndex on any field you have the correct record 结果:n any field Yu can create an index rd yu have the crrect rec 原始数据文件存放的格式是:每行的宽度均小于 80 个字符(含标点符号和空格)。 注意:请勿改动主函数 main()、读函数 ReadDat()和写函数 WriteDat()的内容。 【试题程序】 #include stdio.h #include string.h #include stdlib.h char xx5080; int maxline =0 ; int ReadD
3、at (void) ; void WriteDat (void) ; void StrOR (void) void main () system (“CLS“) ; if (Readmat () printf (“数据文件 IN.DAT 无法打开! n007“) ; return; StrOR () ; WriteDat () ; int ReadDat (void) FILE * fp; int i =0; char * p; if ( (fp = fopen (“IN .DAT“, “r“) ) =NULL) return 1 ; while (fgets (xxi ,80, fp) !
4、=NULL) p =strchr (xxi,“n“) ; if (p) *p =0; i+; maxline = i ; fclose (fp) ; return 0 ; void Writemat (void) FILE * fp; int i ; system (“CLS“) ; fp = fopen ( “OUT. DAT“, “w“ ) ; for (i =0;i maxline;i +) printf (“%sn“, xxi ) ; fprintf (fp, “%s n“, xxi ) ; fclose (fp) ; (分数:100.00)_计算机三级数据库技术-126 答案解析(总
5、分:100.00,做题时间:90 分钟)一、上机题(总题数:1,分数:100.00)1.函数 ReadDat()的功能是从文件 IN.DAT 中读取一篇英文文章存入到字符串数组 xx 中。请编写函数StrOR(),该函数的功能是:以行为单位把字符串中所有小写字母“o”左边的字符串内容移至该串的右边存放,然后把小写字母“o”删除,余下的字符串内容移到已处理字符串的左边存放,最后把已处理的字符串仍按行重新存入字符串数组 xx 中。最后调用函数 writeDat(),把结果输出到文件 OUT.DAT 中。 例如,原文:You can create an index on any field you
6、have the correct record 结果:n any field Yu can create an index rd yu have the crrect rec 原始数据文件存放的格式是:每行的宽度均小于 80 个字符(含标点符号和空格)。 注意:请勿改动主函数 main()、读函数 ReadDat()和写函数 WriteDat()的内容。 【试题程序】 #include stdio.h #include string.h #include stdlib.h char xx5080; int maxline =0 ; int ReadDat (void) ; void Write
7、Dat (void) ; void StrOR (void) void main () system (“CLS“) ; if (Readmat () printf (“数据文件 IN.DAT 无法打开! n007“) ; return; StrOR () ; WriteDat () ; int ReadDat (void) FILE * fp; int i =0; char * p; if ( (fp = fopen (“IN .DAT“, “r“) ) =NULL) return 1 ; while (fgets (xxi ,80, fp) ! =NULL) p =strchr (xxi,
8、“n“) ; if (p) *p =0; i+; maxline = i ; fclose (fp) ; return 0 ; void Writemat (void) FILE * fp; int i ; system (“CLS“) ; fp = fopen ( “OUT. DAT“, “w“ ) ; for (i =0;i maxline;i +) printf (“%sn“, xxi ) ; fprintf (fp, “%s n“, xxi ) ; fclose (fp) ; (分数:100.00)_正确答案:()解析:void StrOR(void) int i,j,k; /*定义循
9、环控制变量*/ int index,str; /*定义变量*/ char temp; /*暂存变量*/ for(i=0;imaxline;i+) /*逐行获取字符数据进行处理*/ str=strlen(xxi); /*求各行的长度*/ index=str; for(j=0;jstr;j+) /*将一行中所以小写字母 o 右边的字符依次向左移一位,并删除字母 o*/ if(xxij=“o“) for(k=j;kstr-1;k+) xxik=xxik+1; xxistr-1=“ “; index=j; /*记录下最后一个 o 所在的位置*/ j = 0; for(j=str-1;j=index;j
10、-) /*最后一个 o 右侧的所有字符都移到已处理字符串的左边*/ temp=xxistr-1; for(k=str-1;k0;k-) xxik=xxik-1; xxi0=temp; 考点 本题考查对字符串处理。考查的知识点主要包括:字符串元素的遍历访问, if 判断结构,逻辑表达式。 解析 本题的解题思路是,首先确定该行字符串的长度;然后使用循环结构依次对字符进行处理;先找到字符“o“,将“o“右侧的字符依次向左移,这个操作同时能够删除字符“o“。记录下最后一个“o“所在的位置,在这个“o“右侧的所有字符都要移到已处理字符串的左边,这个过程也是使用循环来完成的。 对字符数组进行逐元素访问;if 判断结构中的逻辑表达式。