1、全国计算机二级 C 语言上机试题 69+2015 年及答案解析(总分:30.00,做题时间:90 分钟)1.给定程序中,函数 fun 的功能是将 a 和 b 所指的两个字符串转换成面值相同的整数,并进行相加作为函数值返回,规定字符串中只含 9 个以下数字字符。 例如,主函数中输入字符串:32486 和 12345,在主函数中输出的函数值为:44831。 请在程序的下划线处填入正确的内容并把下划线删除, 使程序得出正确的结果。 注意:源程序存放在考生文件夹下的 BLANK1.C 中。不得增行或删行,也不得更改程序的结构! 给定源程序: #include #include #include #de
2、fine N 9 long ctod( char *s ) long d=0; while(*s) if(isdigit( *s) /*found*/ d=d*10+*s-_1_; /*found*/ _2_; return d; long fun( char *a, char *b ) /*found*/ return _3_; main() char s1N,s2N; do printf(“Input string s1 : “); gets(s1); while( strlen(s1)N ); do printf(“Input string s2 : “); gets(s2); whil
3、e( strlen(s2)N ); printf(“The result is: %ld/n“, fun(s1,s2) ); (分数:10.00)_2.给定程序 MODI1.C 中 fun 函数的功能是:分别统计字符串中大写字母和小写字母的个数。 例如, 给字符串 s 输入:AAaaBBb123CCccccd,则应输出结果: upper = 6, lower = 8。 请改正程序中的错误,使它能计算出正确的结果。 注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构! 给定源程序: #include /*found*/ void fun ( char *s, int a, in
4、t b ) while ( *s ) if ( *s = A do printf(“Input string s2 : “); gets(s2); while( strlen(s2)N ); printf(“The result is: %ld/n“, fun(s1,s2) ); (分数:10.00)_正确答案:(第一处:数字字符与其对应的数值相差 48,所以应填:48。 第二处:到字符串下一个位置,所以应填:s+。 第三处:返回两个数字字符串经转换成数值的和,所以应填:ctod(a)+ctod(b)。)解析:2.给定程序 MODI1.C 中 fun 函数的功能是:分别统计字符串中大写字母和小
5、写字母的个数。 例如, 给字符串 s 输入:AAaaBBb123CCccccd,则应输出结果: upper = 6, lower = 8。 请改正程序中的错误,使它能计算出正确的结果。 注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构! 给定源程序: #include /*found*/ void fun ( char *s, int a, int b ) while ( *s ) if ( *s = A s+; main( ) char s100; int upper = 0, lower = 0 ; printf( “/nPlease a string : “ ); g
6、ets ( s ); fun ( s, printf( “/n upper = %d lower = %d/n“, upper, lower ); (分数:10.00)_正确答案:(第一处:在等式右边应写*a。 第二处:在等式右边应写*b。)解析:3.请编一个函数 fun,函数的功能是使实型数保留 2 位小数,并对第三位进行四舍五入 (规定实型数为正数)。 例如:实型数为 1234.567, 则函数返回 1234.570000; 实型数为 1234.564, 则函数返回 1234.560000。 注意: 部分源程序存在文件 PROG1.C 文件中。 请勿改动主函数 main 和其它函数中的任何
7、内容,仅在函数 fun 的花括号中填入你编写的若干语句。 给定源程序: #include float fun ( float h ) main( ) float a; printf (“Enter a: “); scanf ( “%f“, printf ( “The original data is : “ ); printf ( “%f /n/n“, a ); printf ( “The result : %f/n“, fun ( a ) ); NONO( ); (分数:10.00)_正确答案:(float fun ( float h ) long w ; w = h * 100 + 0.5 ; return (float) w / 100 ; )解析: