2015年计算机二级C语言上机操作题及答案(68)
发布时间:2011/7/20 13:15:33 来源:城市学习网 编辑:ziteng
一. 填空题.
请补充main()函数,该函数的功能是:从键盘输入若干字符放到一个字符数组中,当按回车键时结束输入,最后输出这个字符数组中的所有字符.
仅在横线上填入所编写的若干表达式或语句,勿改动函数中的其他任何内容.
#include
#include
main()
{
int i = 0;
char s[81];
char *p = s;
printf(" Input a string \n");
for (i=0; i<80; i++)
{
s[i] = getchar();
if (s[i] == '\n')
___1 ___;
}
s[i] = ___2 ___;
printf(" display the string \n");
while (*p)
putchar(___3___);
}
答案:
第一处填空:break
第一处填空:’\0’或0
第一处填空:*p++ [NextPage] 二. 改错题.
下列给定程序中,函数fun的功能是:根据输入的三个边长(整型值),叛断能否构成三角形;构成的是等边三角形,还是等腰三角形.若能构成等边三角形函数返回3,若能构成等腰三角形函数返回2,若能构成三角形函数返回1,若不能构成三角形函数返回0.
请改正函数fun中的错误,使它能得出正确的结果.
注意:不要改动主main函数,不得增行或删行,也不得更改程序的结构!
#include
int fun(int a, int b, int c)
{
if (a+b>c && b+c>a && a+c>b)
{
/********found********/
if (a==b && b==c)
return 1;
else if (a==b || b==c || a==c)
return 2;
else
/********found********/
return 3;
}
else
return 0;
}
main()
{
int a, b, c, shape;
printf("\nInput a,b,c: ");
scanf("%d%d%d", &a, &b, &c);
printf("\na=%d, b=%d, c=%d\n", a, b, c);
shape = fun(a, b, c);
printf("\n\nThe shape : %d\n", shape);
}
答案:
第一处:return 1;应改为return 3;
第一处:return 3;应改为return 1; [NextPage] 三.编程题.
请编写函数fun,其功能是:计算并输出下列多项式值:
Sn=1+1/1!+1/2!+1/3!+1/4!+…1/n!
例如,若主函数从键盘给n输入15,则输出为S=2.718282.
注意:n的值要求大于1但不大于100.
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句.
#include
double fun(int n)
{
}
main()
{
int n;
double s;
FILE *out;
printf("Input n: ");
scanf("%d",&n);
s=fun(n);
printf("s=%f\n",s);
out=fopen ("out.dat", "w");
for (n = 10; n < 15; n++)
fprintf(out, "%f\n", fun(n));
fclose (out );
}
答案:
double fun(int n)
{
double t,sn=1.0;
int i,j;
for(i=1;i<=n;i++)
{
t=1.0;
for(j=1;j<=i;j++)
t*=j;
sn+=1.0/t;
}
return sn;
}