试卷详情
-
全国计算机等级考试二级C语言机试真题2009年9月
-
[填空题]给定程序的功能是:从键盘输入若干行文本(每行不超过80个字符),并写入文件myfile4.txt中,用-1作为字符串输入结束的标志,然后将文件的内容读出显示在屏幕上。文件的读写分别由自定义函数ReadText和WriteText实现。
[注意] 部分源程序给出如下。
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的横线上填入所编写的若干表达式或语句。
[试题源程序]
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void WriteText(FILE *);
void ReadText(FILE *);
main( )
FILE *fp;
if((fp=fopen("myfile4.txt", "W"))==NULL)
printf("open fail !!/n"); exit(0);
WriteText(fp);
fclose(fp);
if((fp=fopen("myfile4.txt", "r"))==NULL)
printf("open fail !!1/n"); exit(0);
ReadText(fp);
fclose(fp);
/*********found*********/
void WriteText(FILE (1) )
char str[81];
printf("/nEnter string with -1 to end :/n");
gets(str);
while(strcmp(Str, "-1")!=0)
/**********found*********/
fputs( (2) , fw);
fputs("/n", fw);
gets(str);
void ReadText(FILE *fr)
-
[简答题]请编写函数fun,其功能是:计算并输出3~n之间(含3和n)所有素数的平方根之和。
例如,在主函数中从键盘为n输入100后,输出为:sum=148.874270。
[注意] 要求n的值大于2但不大于100。
[注意] 部分源程序给出如下。请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
[试题源程序]
#include<math.h>
#include<stdio.h>
double fun(int n)
main( )
int n;
double sum;
printf("/n/nInput n:");
scanf("%d", &n);
sum=fun(n);
printf("kn/nsum=%f/n/n", sum);
-
[简答题]给定程序中函数fun的功能是:从低位开始取出长整型变量S中偶数位上的数,依次构成一个新数放在t中。高位仍在高位,低位仍在低位。
例如,当s中的数为7654321时,t中的数为642。
请改正程序中的错误,使它能得到正确结果。
[注意] 不要改动main函数,不得增行或删行,也不得更改程序的结构。
[试题源程序]
#include<stdio.h>
/************found***********/
void fun(long s, long t)
long s1=10;
s/=10;
*t=s% 10;
/************found************/
while(s<0)
s=s/100;
*t=s% 10*s1+*t;
s1=s1 *10;
msin( )
long s, t;
printf("/nPlease enter s:");
scanf("%ld", &s);
fun(s, &t);
printf("The result is: %ld/n", t);