2015年计算机二级C语言上机操作题及答案(76)
发布时间:2011/7/22 10:10:27 来源:城市学习网 编辑:ziteng
第一题:请补充fun函数,函数的功能是:把字符串str中的字符按字符的ASCII码降序排列,处理后的字符串仍然保存在原串中,字符串及其长度作为函数传入。
例如:如果输入“abcde”,则输出为“decba”。
请勿改动主函数main和其他函数中的任何内容,仅在fun函数的横线上填入所编写的若干表达式或语句。
#include
#define N 80
void fun(char s[], int n)
{
int i, j;
char ch;
for (i=0; i for (j=___1___; j if (s[i] < s[j])
{
ch = s[j];
___2___;
s[i] = ch;
}
}
main()
{
int i = 0, strlen = 0;
char str[N];
printf("\nInput a string:\n");
gets(str);
while (str[i] != '\0')
{
strlen++;
i++;
}
fun(str, strlen);
printf("\n********* display string *********\n");
puts(str);
}
答案:
第1空应填:i
第2空应填:s[j]=s[i] [NextPage] 第二题:下列给定程序中函数fun的功能是:将长整数中每一位上为偶数的数依次取出,构成一个新数在t中。高位仍在高位,低位仍在低位。例如,当s中的数为87653142时,t中的数为8462。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结果。
#include
#include
void fun(long s, long *t)
{
/********found********/
int d;
long s1 = 0;
*t = 0;
while (s > 0)
{
d = s;
if (d%2 == 0)
{
*t = d*s1 + *t;
s1 *= 10;
}
/********found********/
s \= 10;
}
}
main()
{
long s, t;
printf("\nPlease enter s:");
scanf("%ld", &s);
fun(s, &t);
printf("The result is :%ld\n", t);
}
答案:
第1处应改为:long s1=1;
第2处应改为:s/=10; [NextPage] 第三题:请编写函数fun,其功能是:计算并输出当x<0.97时下列多式的值,直到为止。
例如,若主函数从键盘给x输入0.21后,则输出为s=1.100000.
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include
#include
double fun(double x)
{
}
main()
{
int i;
double x,s;
FILE *out;
printf("Input x: ");
scanf("%lf",&x);
s=fun(x);
printf("s=%f\n",s);
out=fopen ("out.dat", "w");
for (i = 20; i < 30; i++)
fprintf(out, "%f\n", fun(i/100.0));
fclose (out );
}
答案:
double fun(double x)
{
double s1=1.0,p=1.0,sum=0.0,s0,t=1.0;
int n=1;
do
{
s0=s1;
sum+=s0;
t*=n;
p*=(0.5-n+1)*x;
s1=p/t;
n++;
}
while(fabs(s1-s0)>=le-6);
return sum;
}