1、三级网络技术机试-250 及答案解析(总分:100.00,做题时间:90 分钟)一、上机题(总题数:1,分数:100.00)1.下列程序的功能是:把 s 字符串中的所有字母改成该字母的下一个字母,字母 z 改成字母 a。要求大写字母仍为大写字母,小写字母仍为小写字母,其他字符不做改变。请编写函数 chg(char*s)实现程序要求,最后调用函数 readwriteDAT(),读取 in36.dat 中的字符串,并把结果输出到文件 out36.dat 中。例如:s 字符串中原有的内容为 Mn 123Zxy,则调用该函数后,结果为 No 123Ayz。注意:部分源程序已给出。请勿改动主函数 mai
2、n()和输入输出函数 readwriteDAT()的内容。试题程序: #includestdio. h#includestring. h# includeconio, h# includectype, h#define N 81void readwriteDAT ();void chg(char as)main ( )char a N;clrscr (); printf(“Enter a string:“);gets (a);printf (“The original string is :“);puts (a);chg (a);printf(“The string after modifie
3、d :“);puts (a);readwriteDAT ();void readwriteDAT ()int i;char a N;FILE *rf, *wf;r f=fopen (“in36. dat“, “r“ );wf=fopen (“out36. dat“, “w“ );for (i=0;ifgets (a, 81, rf);chg (a);fprintf (wf, “%s“, a);fclose (rf);fclose (wf);(分数:100.00)_三级网络技术机试-250 答案解析(总分:100.00,做题时间:90 分钟)一、上机题(总题数:1,分数:100.00)1.下列程
4、序的功能是:把 s 字符串中的所有字母改成该字母的下一个字母,字母 z 改成字母 a。要求大写字母仍为大写字母,小写字母仍为小写字母,其他字符不做改变。请编写函数 chg(char*s)实现程序要求,最后调用函数 readwriteDAT(),读取 in36.dat 中的字符串,并把结果输出到文件 out36.dat 中。例如:s 字符串中原有的内容为 Mn 123Zxy,则调用该函数后,结果为 No 123Ayz。注意:部分源程序已给出。请勿改动主函数 main()和输入输出函数 readwriteDAT()的内容。试题程序: #includestdio. h#includestring.
5、h# includeconio, h# includectype, h#define N 81void readwriteDAT ();void chg(char as)main ( )char a N;clrscr (); printf(“Enter a string:“);gets (a);printf (“The original string is :“);puts (a);chg (a);printf(“The string after modified :“);puts (a);readwriteDAT ();void readwriteDAT ()int i;char a N;F
6、ILE *rf, *wf;r f=fopen (“in36. dat“, “r“ );wf=fopen (“out36. dat“, “w“ );for (i=0;ifgets (a, 81, rf);chg (a);fprintf (wf, “%s“, a);fclose (rf);fclose (wf);(分数:100.00)_正确答案:(void chg(char *s)while (*s)if(*s=z | |*s=z) /*字母 z 改成字母 a,字母 z 改成字母 A*/*s-=25;s+; /*取下一个字母* /else if(*s=as+; /*取下一个字母*/else if
7、(*s=As+; /*取下一个字母*/else s+; /*取下一个字母* /)解析:解析 本题考查的知识点如下:(1)英文字母对应的 ASCII 码的排列规律。(2)指针的操作。 (3)选择结构中多个条件的布尔运算。 在 ASCII 码中,大、小写字母的 ASCII 码是连贯的,az 对应 97122,AZ 对应 6590。z 的 ASCII码减去 25 即得到 a 的 ASCII 码,对 Z 同样适合。所以我们要对字符串中的字符依次进行判断,若是大(小)写字母,则按照 ASCII 码的规律和题意要求对字符进行转换。在这里,使用指针完成对字符的依次访问,每次将指针的地址加 1 直至指针为空即可。在选择结构的多个条件中,若需同时满足,则用“与”运算,否则,用“或”运算。