📄 二级c机试题.txt
字号:
main()
{char tt [81 ] ;
printf("\nPlease enter an string within 80 characters:\n");gets(tt );
printf("\n \nAfter changing,the string \n \"%s \"",tt );
fun(tt );
printf("\nbecomes \n \"%s \"\n",tt );
NONO ();
}
NONO ()
{/*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
FILE *fp,*wf ;
char tt [81 ] ;
int i ;
fp =fopen("c:\\test \\in.dat","r");
wf =fopen("c:\\test \\out.dat","w");
for(i =0 ;i <10 ;i++){
fscanf(fp,"%s",tt);
fun(tt );
fprintf(wf,"%s \n",tt);
}
fclose(fp);
fclose(wf);
}
※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
第10 套:
给定程序中,函数fun 的功能是:计算形参x 所指数组中N 个数的平均值(规定
所有数均为正数),将所指数组中大于平均值的数据移至数组的前部,小于等于平
均值的数据移至x 所指数组的后部,平均值作为函数值返回,在主函数中输出平均
值和移动后的数据。
例如,有10 个正数:46 30 32 40 6 17 45 15 48 26,平均值为:
30.500000?二级C 语言上机试题汇编 第 39 页 共362 页 昌黎科普计算机学校
选自捷成软件 网址:http://www.ncre.he.cn QQ :153599495
移动后的输出为:46 32 40 45 48 30 6 17 15 26
请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结
果。
注意:源程序存放在考生文件夹下的BLANK1.C 中。
不得增行或删行,也不得更改程序的结构!
给定源程序:
#include <stdlib.h>
#include <stdio.h>
#define N 10
double fun(double *x)
{int i,j;double s,av,y [N ];
s=0;
for(i=0;i<N;i++)s=s+x [i ];
/**********found**********/
av=__1__;
for(i=j=0;i<N;i++)
if(x [i ]>av ){
/**********found**********/
y [__2__]=x [i ];x [i ]=-1;}
for(i=0;i<N;i++)
/**********found**********/
if(x [i ]!=__3__)y [j++]=x [i ];
for(i=0;i<N;i++)x [i ] =y [i ];
return av;
}
main()
{int i;double x [N ];
for(i=0;i<N;i++){x [i ]=rand()%50;printf("%4.0f ",x [i ]);}
printf("\n");
printf("\nThe average is:%f \n",fun(x));
printf("\nThe result :\n",fun(x));
for(i=0;i<N;i++)printf("%5.0f ",x [i ]);
printf("\n");
}
解题思路:
第一处:计算N 个数的平均值,所以应填:s/N 。
第二处:利用for 循环语句,把数组x 中大于平均值的数,依次存放到数组y 中,同时
把数组x 上的该数置为-1,其中位置由变量j 来控制,所以应填:j++。
第三处:再利用循环把不是-1 的数,依次仍存放到数组y 中,所以应填:-1 。
**********************************************************************
给定程序MODI1.C 的功能是:读入一个英文文本行,将其中每个单词的第一个
字母改成大写,然后输出此文本行(这里的“单词”是指由空格隔开的字符串)。
例如,若输入:I am a student to take the examination.,
则应输出:I Am A Student To Take The Examination.。
请改正程序中的错误,使程序能得出正确的结果。
注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构!
给定源程序:
#include <ctype.h>
#include <string.h>
/************found************/
include <stdio.h>
/************found************/
upfst (char p )
{int k=0;
for (;*p;p++)
if (k )
{if (*p =='')k =0;}
else if (*p !='')
{k =1;*p =toupper(*p );}
}
main()
{char chrstr [81 ];
printf("\nPlease enter an English text line:");gets(chrstr );
printf("\n \nBefore changing:\n %s",chrstr );
upfst(chrstr );
printf("\nAfter changing:\n %s \n",chrstr );
}
解题思路:
第一处:包含头文件的标识错误,在include 前漏写了#。
第二处:由于传入的参数是字符串,所以应为upfst(char *p)。
**********************************************************************
程序定义了N ×N 的二维数组,并在主函数中赋值。请编写函数fun,函数的功
能是:求出数组周边元素的平均值并作为函数值返给主函数中的s 。
例如:a 数组中的值为
|0 1 2 7 9|
|1 9 7 4 5|
a =|2 3 8 3 1|
|4 5 6 8 2|
|5 9 1 4 1|
则返回主程序后s 的值应为:3.375 。
注意:部分源程序存在文件PROG1.C 文件中。
请勿改动主函数main 和其它函数中的任何内容,仅在函数fun 的花括号中填入
你编写的若干语句。
给定源程序:
#include <stdio.h>
#include <stdlib.h>
#define N 5
double fun (int w [][N ] )
{
}
main ()
{int a [N ][N ]={0,1,2,7,9,1,9,7,4,5,2,3,8,3,1,4,5,6,8,2,5,9,1,4,1};
int i,j;
double s ;
printf("*****The array *****\n");
for (i =0;i<N;i++)
{for (j =0;j<N;j++)
{printf("%4d",a [i ][j ] );}
printf("\n");
}
s =fun (a );
printf ("*****THE RESULT *****\n");
printf("The sum is :%lf \n",s );
NONO();
}NONO(
)
{/*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
FILE *rf,*wf ;
int i,j,a [5 ][5 ];double s ;
rf =fopen("c:\\test \\in.dat","r");
wf =fopen("c:\\test \\out.dat","w");
for(i =0 ;i <5 ;i++)
for(j =0 ;j <5 ;j++)fscanf(rf,"%d ",&a [i ][j ]);
s =fun(a);
fprintf(wf,"%lf \n",s);
fclose(rf);
fclose(wf);
}
解题思路:
本题是统计二维数组周边元素值的平均值,但要注意的是不要重复计算四个角上的
元素值。
参考答案:
#include <stdio.h>
#include <stdlib.h>
#define N 5
double fun (int w [][N ] )
{
int i,j,n=0;
double sum=0;
for (i =0;i<N;i++){
sum+=w [0 ][i ]+w [N-1 ][i ];n+=2;
}
for (i =1;i<N -1;i++){
sum +=w [i ][0 ]+w [i ][N-1 ];
n+=2;
}return
sum/n;
}
main ()
{int a [N ][N ]={0,1,2,7,9,1,9,7,4,5,2,3,8,3,1,4,5,6,8,2,5,9,1,4,1};
int i,j;
double s ;
printf("*****The array *****\n");
for (i =0;i<N;i++)
{for (j =0;j<N;j++)
{printf("%4d",a [i ][j ] );}
printf("\n");
}
s =fun (a );
printf ("*****THE RESULT *****\n");
printf("The sum is :%lf \n",s );
NONO();
}NONO(
)
{/*本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
FILE *rf,*wf ;
int i,j,a [5 ][5 ];double s ;
rf =fopen("c:\\test \\in.dat","r");
wf =fopen("c:\\test \\out.dat","w");
for(i =0 ;i <5 ;i++)
for(j =0 ;j <5 ;j++)fscanf(rf,"%d ",&a [i ][j ]);
s =fun(a);
fprintf(wf,"%lf \n",s);
fclose(rf);
fclose(wf);
}
※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
第11 套:
给定程序中,函数fun 的功能是将a 和b 所指的两个字符串转换成面值相同的整
数,并进行相加作为函数值返回,规定字符串中只含9 个以下数字字符。
例如,主函数中输入字符串:32486 和12345,在主函数中输出的函数值为:
44831 。
请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结
果。
注意:源程序存放在考生文件夹下的BLANK1.C 中。
不得增行或删行,也不得更改程序的结构!
给定源程序:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define 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 s1 [N ],s2 [N ];
do {
printf("Input string s1 :");gets(s1);}
while(strlen(s1)>N );
do {
printf("Input string s2 :");gets(s2);}
while(strlen(s2)>N );
printf("The result is:%ld \n",fun(s1,s2));
}
解题思路:
第一处:数字字符与其对应的数值相差48,所以应填:48 。
第二处:到字符串下一个位置,所以应填:s++。
第三处:返回两个数字字符串经转换成数值的和,所以应填:ctod(a)+ctod(b)。
**********************************************************************
给定程序MODI1.C 中 fun 函数的功能是:分别统计字符串中大写字母和小写
字母的个数。
例如,给字符串 s 输入:AAaaBBb123CCccccd,则应输出结果:
upper =6,lower =8 。
请改正程序中的错误,使它能计算出正确的结果。
注意:不要改动 main 函数,不得增行或删行,也不得更改程序的结构!
给定源程序:
#include <stdio.h>
/**********found**********/
void fun (char *s,int a,int b )
{
while (*s )
{if (*s >='A'&&*s <='Z')
/**********found**********/
*a=a+1 ;
if (*s >='a'&&*s <='z')
/**********found**********/
*b=b+1;
s++;
}
}
main()
{char s [100 ];int upper =0,lower =0 ;
printf("\nPlease a string :");gets (s );
fun (s,&upper,&lower );
printf("\n upper =%d lower =%d \n",upper,lower );
}
解题思路:
第一处:在等式右边应写*a 。
第二处:在等式右边应写*b 。
**********************************************************************
请编一个函数fun,函数的功能是使实型数保留2 位小数,并对第三位进行四
舍五入 (规定实型数为正数)。
例如:实型数为 1234.567,则函数返回 1234.570000;
实型数为 1234.564,则函数返回 1234.560000 。
注意:部分源程序存在文件PROG1.C 文件中。
请勿改动主函数main 和其它函数中的任何内容,仅在函数fun 的花括号中填入
你编写的若干语句。
给定源程序:
#include <stdio.h>
float fun (float h )
{}main(
)
{float a;
printf ("Enter a:");scanf ("%f",&a );
printf ("The original data is :");
printf ("%f \n \n",a );
printf ("The result :%f \n",fun (a ));
NONO();
}NONO(
)
{/*请在此函数内打开文件,输入测试数据,调用 fun 函数,
输出数据,关闭文件。 */
int i ;
float a ;
FILE *rf,*wf ;
rf =fopen("c:\\test \\in.dat","r");
wf =fopen("c:\\test \\out.dat","w");
for(i =0 ;i <20 ;i++){
fscanf(rf,"%f",&a);
fprintf(wf,"%f \n",fun(a));
}
fclose(rf);
fclose(wf);
}
解题思路:
本题主要是考察考生保留小数点后两位数并对第三位进行四舍五入。方法是先把这
个数乘以100,然后再加0.5(实现四舍五入),再把这个数存放到一个长整型变量中,
目的是把小数点后的小数去除,最后把这个数转换成浮点型数除以100,即可得出所的结
果。
参考答案:
#include <stdio.h>
float fun (float h )
{
long w ;
w =h *100 +0.5 ;
return (float)w /100 ;
}main(
)
{float a;
printf ("Enter a:");scanf ("%f",&a );
printf ("The original data is :");
printf ("%f \n \n",a );
printf ("The result :%f \n",fun (a ));
NONO();
}NONO(
)
{/*请在此函数内打开文件,输入测试数据,调用 fun 函数,
输出数据,关闭文件。 */
int i ;
float a ;
FILE *rf,*wf ;
rf =fopen("c:\\test \\in.dat","r");
wf =fopen("c:\\test \\out.dat","w");
for(i =0 ;i <20 ;i++){
fscanf(rf,"%f",&a);
fprintf(wf,"%f \n",fun(a));
}
fclose(rf);
fclose(wf);
}
※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
第12 套:
给定程序中,函数fun 的功能是将形参给定的字符串、整数、浮点数写到文本
文件中,再用字符方式从此文本文件中逐个读入并显示在终端屏幕上。
请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结
果。
注意:源程序存放在考生文件夹下的BLANK1.C 中。
不得增行或删行,也不得更改程序的结构!
给定源程序:
#include <stdio.h>
void fun(char *s,int a,double f)
{
/**********found**********/
__1__
fp;
char
ch;
fp =fopen("file1.txt","w");
fprintf(fp,"%s %d %f \n",s,a,f);
fclose(fp);
fp =fopen("file1.txt","r");
printf("\nThe result :\n \n");
ch =fgetc(fp);
/**********found**********/
while (!feof(__2__)){
/**********found**********/
putchar(__3__);ch =fgetc(fp);}
putchar('\n');
fclose(fp);
}
main()
{char a [10 ]="Hello!";int b=12345;
double c=98.76;
fun(a,b,c);
}
解题思路:
本题是考察先把给定的数据写入到文本文件中,再从该文件读出并显示在屏幕上。
第一处:定义文本文件类型变量,所以应填:FILE *。
第二处:判断文件是否结束,所以应填:fp 。
第三处:显示读出的字符,所以应填:ch 。
**********************************************************************
给定程序MODI1.C 中函数fun 的功能是:依次取出字符串中所有数字字符,形
成新的字符串,并取代原字符串。
请改正函数fun 中指定部位的错误,使它能得出正确的结果。
注意:不要改动main 函数,不得增行或删行,也不得更改程序的结构!
给定源程序:
#include <stdio.h>
void fun(char *s)
{int i,j;
for(i=0,j=0;s [i ]!='\0';i++)
if(s [i ]>='0'&&s [i ]<='9')
/**********found**********/
s [j ]=s [i ];
/**********found**********/
s [j ]="\0";
}
main()
{char item [80 ];
printf("\nEnter a string :");gets(item);
printf("\n \nThe string is :\"%s \"\n",item);
fun(item);
printf("\n \nThe string of changing is :\"%s \"\n",item );
}
解题思路:
第一处:要求是取出原字符串中所有数字字符组成一个新的字符串,程序
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -