四级机试试题及答案二_计算机等级真题,计算机等级考试,计算机考试(1) |
| 论文作者:佚名 论文来源:不详 论文发布时间:2006-6-9 1:27:16 论文发布人:chjchjchj |
减小字体
增大字体
1./* 程序PROG1.C的功能是:选出100至1000之间所有个位数字与十位数字之和 被10除所得余数恰是百位数字的素数(如293)。计算并输出上述这些素数的个 数cnt以及这些素数值的和sum。 请考生编写函数countValue( )实现程序的要求,最后调用函数writeDAT( )把 结果cnt和sum输出到文件out6.DAT中。 注意:部分源程序存放在PROG1.C中。 请勿改动主函数main( )和输出数据函数writeDAT( )的内容。 */ #include <stdio.h> int cnt, sum ; void countValue() { } void main() { cnt = sum = 0 ; countValue() ; printf("素数的个数=%d", cnt) ; printf("满足条件素数值的和=%d", sum) ; writeDAT() ; } writeDAT() { FILE *fp ; fp = fopen("OUT6.DAT", "w") ; fprintf(fp, "%d%d", cnt, sum) ; fclose(fp) ; }
2. /* 编写函数sumValue( ),它的功能是:计算正整数n的所有因子(1和n除外)之 和作为函数值返回。 例如:n=20时,函数值为21。 函数ReadWrite( )是实现从文件in9.dat中读取两个字符串,并调用函数 sumValue(),最后把结果输出到文件out9.dat中。 注意:部分源程序存在文件PROG1.C中,请勿改动主函数main()和其它函数 中的任何内容,仅在函数sumValue()的花括号中填入你编写的若干语句。 */ #include <conio.h> #include <stdio.h> int sumValue(int n) { } main() { clrscr() ; printf("%d", sumValue(20)) ; ReadWrite() ; } ReadWrite() { FILE *fp, *wf ; int i, n, s ; fp = fopen("in9.dat","r") ; if(fp == NULL) { printf("数据文件in9.dat不存在!") ; return ; } wf = fopen("out9.dat","w") ; for(i = 0 ; i < 10 ; i++) { fscanf(fp, "%d", &n) ; s = sumValue(n) ; fprintf(wf, "%d", s) ; } fclose(fp) ; fclose(wf) ; } 3./* 请编写一个函数changeStr(char *s),函数的功能是把s串中所有的字 母改写成该字母的下一个字母,字母z改写成字母a。大写仍为大写字母, 小写字母仍为小写字母,其它的字符不变。 函数ReadWrite()实现从文件in2.dat中读取两个字符串,并调用函 数changestr(),最后把结果输出到文件out2.dat中。 注意:部分源程序存在文件PROG1.C中。请勿改动主函数main()和其 它函数中的任何内容,仅在函数changeStr()的花括号中填入你编写的若干语名。*/ #include <conio.h> #include <string.h> #include <stdio.h> #include <ctype.h> #define N 81 changeStr ( char *s ) { } main( ) { char a[N] ; clrscr( ) ; printf ( "Enter a string : " ) ; gets ( a ) ; printf ( "The original string is : " ) ; puts( a ) ; changeStr ( a ) ; printf ( "The string after modified : ") ; puts ( a ) ; ReadWrite( ) ; } ReadWrite( ) { int i ; char a[N] ; FILE *rf, *wf ; rf = fopen("in2.dat", "r") ; wf = fopen("out2.dat", "w") ; for(i = 0 ; i < 10 ; i++) { fscanf(rf, "%s", a) ; changeStr(a) ; fprintf(wf, "%s", a) ; } fclose(rf) ; fclose(wf) ; } 4./* 程序PROG1.C的功能是:利用以下所示的简单迭代方法求方程: cos(x)-x=0的一个实根。 Xn+1=cos(Xn) 迭代步骤如下: (1) 取x1初步值为0.0; (2) x0=x1,把x1,把x1的值赋给x0; (3) x1=cos(x0),求出一个新的x1; (4) 若x0-x1的绝对值小于0.000001,执行步骤(5),否则执行步骤(2); (5) 所求x1就是方程cos(x)-x=0的一个实根,作为函数值返回。 请编写函数countValue()实现程序的要求,最后调用函数writeDAT()把 结果输出到文件out4.dat中。 注意:部分源程序存在文件PROG1.C中,请勿改动主函数main()和输出数 据函数WriteDAT()的内容。 */ #include <conio.h> #include <math.h> #include <stdio.h> float countValue() { } main() { clrscr(); printf("实根=%f", countValue()); printf(" %f",cos(countValue())-countValue()); writeDAT(); } writeDAT() { FILE *wf ; wf=fopen("out4.dat","w") ; fprintf(wf, "%f", countValue()) ; fclose(wf) ; }
|
|
|
|
|
|
| ∷相关试题评论 |
(评论内容只代表网友观点,与本站立场无关!) [查看发表评论...] | |
|
|
|
|
站内广告 |
| |
|
站内搜索 |
| |
栏目导航 |
| |
|
|
本月热门 |
| |
|
|
本日热门 |
| |
|
|
|