计算机二级c语言历年真题及答案 联系客服

发布时间 : 星期日 文章计算机二级c语言历年真题及答案更新完毕开始阅读3a37eaf7f724ccbff121dd36a32d7375a417c6d0

精品

double fun ( int n ) { double result = 1.0 ; if n = = 0 return 1.0 ;

while( n 1 n 170 ) result *= n-- return result ; } main ( ) { int n ;

printf(input n:) ; scanf(%d, n) ;

printf(\\n\\n%d! =%lf\\n\\n, n, fun(n)) ; } 解题思路:

第一处:条件语句书写格式错误,应改为:if (n==0)。 第二处:语句后缺少分号。

*************************************************** 请编写一个函数fun,它的功能是:将一个数字字符串转换为一个整数(不得调用c语言提供的将字符串转换为整数的。例如,若输入字符串-1234,则函数把它转换为整数值 -1234。函数fun中给出的语句仅供参考。

注意: 部分源程序存在文件prog1.c文件中。

请勿改动主函数main和其它函数中的任何内容, 仅在函数fun的花括号中填入你编写的若干语句。 给定源程序: #include stdio.h #include string.h long fun ( char *p)

{int i, len, t; /* len为串长,t为正负标识 */ long x=0; len=strlen(p); if(p[0]==-)

-可编辑-

精品

{ t=-1; len--; p++; } else t=1;

/* 以下完成数字字符串转换为一个数字 */ return x*t; } main() /* 主函数 */ { char s[6]; long n;

printf(enter a string:\\n) ; gets(s); n = fun(s); printf(%ld\\n,n); nono ( ); } nono ( )

{/* 本函数用于打开文件,输入数 据,调用函数,输出数据,关闭 文件。 */ file *fp, *wf ; int i ; char s[20] ; long n ;

fp = fopen(c:\\\\test\\\\in.dat,r) ; wf =

fopen(c:\\\\test\\\\out.dat,w) ; for(i = 0 ; i 10 ; i++) { fscanf(fp, %s, s) ; n = fun(s); fprintf(wf, %ld\\n, n) ; } fclose(fp) ; fclose(wf) ; } 解题思路:

本题是将一个数字字符串转换为一个整数。 参考答案:

#include stdio.h #include string.h long fun ( char *p) {-可编辑-

精品

int i, len, t; /* len为串长,t为正负标识 */ long x=0; len=strlen(p); if(p[0]==-) { t=-1; len--; p++; } else t=1;

/* 以下完成数字字符串转换为一个数字 */

while(*p) x = x*10-48+(*p++); return x*t; } main() /* 主函数 */ { char s[6]; long n;

printf(enter a string:\\n) ; gets(s); n = fun(s); printf(%ld\\n,n); nono ( ); } nono ( )

{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */ file *fp, *wf ; int i ; char s[20] ; long n ;

fp = fopen(c:\\\\test\\\\in.dat,r) ; wf =

fopen(c:\\\\test\\\\out.dat,w) ; for(i = 0 ; i 10 ; i++) { fscanf(fp, %s, s) ; n = fun(s); fprintf(wf, %ld\\n, n) ; }

fclose(fp) ; fclose(wf) ; } 注意:由于nono( )这个函数是改卷人用的,与考生没有什么关系,故下面从第2套试题开始均省略nono( )

-可编辑-

精品

※※※※※※※※※※※※※※※※※※※※※※※※※ 第02套:

给定程序中,函数fun的功能是将形参给定的字符串、整数、浮点数写到文本 文件中,再用字符方式从此文本文件中逐个读入并显示在终端屏幕上。 请在程序的下划线处填入正确的内容并把下划线删除, 使程序得出正确的结果。 注意:源程序存放在考生文件夹下的blank1.c中。

不得增行或删行,也不得更改程序的结构! 给定源程序: #include stdio.h

void fun(char *s, int a, double f) { __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);

while (!feof(__2__)) {

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); }

-可编辑-