知ing

最新计算机二级C语言上机试题汇编100套

NCRE研究组 编 / 高等教育出版社

゛Zirro、Y 上传

查看本书

31套: 

给定程序中,函数fun的功能是:对形参s所指字符串中下标为奇数的字符按ASCII码大小递增排序,并将排序后下标为奇数的字符取出,存入形参p所指字符数组中,形成一个新串。 

例如,形参s所指的字符串为:baawrskjghzlicda,执行后p所指字符数组中 

的字符串应为:aachjlsw。 

请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。 

注意:源程序存放在考生文件夹下的BLANK1.C中。 

不得增行或删行,也不得更改程序的结构! 

给定源程序: 

#include <stdio.h> 

void fun(char *s, char *p) 

{ int i, j, n, x, t; 

n=0; 

for(i=0; s[i]!='\0'; i++) n++; 

for(i=1; i<n-2; i=i+2) { 

/ **********found**********/ 

___1___; 

/ **********found**********/ 

for(j=___2___+2 ; j<n; j=j+2) 

if(s[t]>s[j]) t=j; 

if(t!=i) 

{ x=s[i]; s[i]=s[t]; s[t]=x; } 

for(i=1,j=0; i<n; i=i+2, j++) p[j]=s[i]; 

/ **********found**********/ 

p[j]=___3___; 

main() 

{ char s[80]="baawrskjghzlicda", p[50]; 

printf("\nThe original string is : %s\n",s); 

fun(s,p); 

printf("\nThe result is : %s\n",p); 

 

解题思路: 

第一处:取外循环的控制变量,所以应填:t=i。 

第二处:内循环的起始变量,应该是i+2,所以应填:i。 

第三处:新字符串处理完后应添加字符串结束符,所以应填:'\0'。 

*************************************************** 

给定程序MODI1.C中函数 fun 的功能是:用下面的公式求π的近似值,直到最后一项的绝对值小于指定的数(参数num )为止: 

π 1 1 1 

┄┄≈1 - ┄┄ ┄┄ ┄┄ + ... 

4 3 5 7 

例如程序运行后输入0.0001, 则程序输出3.1414。 

请改正程序中的错误,使它能输出正确的结果。 

注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构! 

给定源程序: 

#include <math.h> 

#include <stdio.h> 

float fun ( float num ) 

{ int s ; 

float n, t, pi ; 

t = 1 ; pi = 0 ; n = 1 ; s = 1 ; 

/ **************found**************/ 

while(t >= num) 

pi = pi + t ; 

n = n + 2 ; 

s = -s ; 

/ **************found**************/ 

t = s % n ; 

pi = pi * 4 ; 

return pi ; 

main( ) 

{ float n1, n2 ; 

printf("Enter a float number: ") ; 

scanf("%f", &n1) ; 

n2 = fun(n1) ; 

printf("%6.4f\n", n2) ; 

解题思路: 

第一处:要判断t的最后一项绝对小于指定的数,由于t是实数,那么应改为while(fabs(t)>=num)。 

第二处:ts除以n的值,而不是取余数,所以应改t=s/n;。 

*************************************************** 

请编写一个函数void fun (char a[],char b[],int n),其功能是:删除一个字符串中指定下标的字符。其中, a指向原字符串删除指定字符后的字符串存放在b所指的数组中,n中存放指定的下标。

例如,输入一个字符串: World,然后输入3,则调用该函数后的结果为: Word。 

注意部分源程序在文件PROG1.C中。 

请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。 

给定源程序: 

#include <stdio.h> 

#include <string.h> 

#define LEN 20 

void fun (char a[], char b[], int n) 

main( ) 

{ char str1[LEN], str2[LEN] ; 

int n ; 

printf("Enter the string:\n") ; 

gets(str1) ; 

printf("Enter the position of the string deleted:") ; 

scanf("%d", &n) ; 

fun(str1, str2, n) ; 

printf("The new string is: %s\n", str2) ; 

NONO() ; 

解题思路: 

本题是利用字符串拷贝和字符串连接来生成新的字符串。 

参考答案: 

void fun (char a[], char b[], int n) 

strncpy(b, a, n) ; 

b[n] = 0 ; 

 

strcat(b, a + n + 1) ; 

※※※※※※※※※※※※※※※※※※※※※※※※※ 


查看更多