全国计算机等级四级机试试题及答案一-1315-
13./* 请编写一个函数changestr(char *s),函数的功能是把s串中所有的字母改写成该字母的下一个字母,字母z改写成字母a。大写仍为大写字母,小写字母仍为小写字母,其它的字符不变。函数readwrite()实现从文件in2.dat中读取两个字符串,并调用函数changestr(),最后把结果输出到文件out2.dat中。
注意:部分源程序存在文件prog1.c中。请勿改动主函数main()和其它函数中的任何内容,仅在函数changestr()的花括号中填入你编写的若干语名。*/
#include
#include
#include
#include
#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\n', a) ;
}
fclose(rf) ;
fclose(wf) ;
}
14./* 程序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
#include
#include
float countvalue()
{
}
main()
{
clrscr();
printf('实根=%f\n', countvalue());
printf(' %f\n',cos(countvalue())-countvalue());
writedat();
}
writedat()
{
file *wf ;
wf=fopen('out4.dat','w') ;
fprintf(wf, '%f\n', countvalue()) ;
fclose(wf) ;
}
15./* 已知在文件in.dat中存有若干个(个数<200)四位数字的正整数,函数readdat()读取这些正整数并存入数组xx中。请编制函数calvalue()其功能要求是:
1.求出这个文件中共有多少个正整数totnum;2.求出这些数中的各位数字之和是奇数的数的个数totcnt,以及不满足此条件的所有数的算术平均值totpjz,最后调用函数writedat()把所求的结果输出到文件out8.dat中。
注意:部分源程序存放在prog1.c中。
请勿改动主函数main(),读数据函数readdat()和输出数据函数writedat()的内容。 */
#include
#include
#define maxnum 200
int xx[maxnum] ;
int totnum = 0 ; /* 文件in.dat中共有多少个正整数 */
int totcnt = 0 ; /* 符合条件的正整数的个数 */
double totpjz = 0.0 ; /* 平均值 */
int readdat(void) ;
void writedat(void) ;
void calvalue(void)
{
}
void main()
{
clrscr() ;
if(readdat()) {
printf('数据文件in.dat不能打开!\007\n') ;
return ;
}
calvalue() ;
printf('文件in.dat中共有正整数=%d个\n', totnum) ;
printf('符合条件的正整数的个数=%d个\n', totcnt) ;
printf('平均值=%.2lf\n', totpjz) ;
writedat() ;
}
int readdat(void)
{
file *fp ;
int i = 0 ;
if((fp = fopen('in.dat', 'r')) == null) return 1 ;
while(!feof(fp)) {
fscanf(fp, '%d,', &xx[i++]) ;
}
fclose(fp) ;
return 0 ;
}
void writedat(void)
{
file *fp ;
fp = fopen('out8.dat', 'w') ;
fprintf(fp, '%d\n%d\n%.2lf\n', totnum, totcnt, totpjz) ;
fclose(fp) ;
}
16./* 编写一个函数findstr(),该函数统计一个长度为2的子字符串在另一个字符串中出现的次数。例如,假定输入的字符串为“asd asasdfg asd as zx67 asd mklo”,子字符串为“as”,则输出6。 函数readwrite()实现从文件in1.dat中读取两个字符串,并调用函数findstr(),最后把结果输出到文件out1.dat中。
注意:部分源程序存在文件prog1.c中。请勿改动主函数main()和其它函数中的任何内容,仅在函数findstr()的花括号中填入你编写的若干语句。*/
#include
#include
#include
int findstr(char *str,char *substr)
{
}
main()
{
char str[81], substr[3] ;
int n ;
clrscr() ;
gets(str) ;
gets(substr) ;
puts(str) ;
puts(substr) ;
n=findstr(str, substr) ;
printf('n=%d\n', n) ;
readwrite() ;
}
readwrite()
{
char str[81], substr[3], ch;
int n, len, i = 0;
file *rf, *wf ;
rf = fopen('in1.dat', 'r') ;
wf = fopen('out1.dat', 'w') ;
while(i < 5) {
fgets(str, 80, rf) ;
fgets(substr, 10, rf) ;
len = strlen(substr) - 1 ;
ch = substr[len] ;
if(ch == ’\n’ || ch == 0x1a) substr[len] = 0 ;
n=findstr(str, substr);
fprintf(wf, '%d\n', n) ;
i++ ;
}
fclose(rf) ;
fclose(wf) ;
}
17./* 请编写函数void countvalue(int *a,int *n),它的功能是:求出1到1000之内能被7或11整除但不能同时被7和11整除的所有整数,并放在数组a中,然后通过n返回这些数的个数。
注意:部分源程序存入在prog1.c中。
请改动主函数main()和输入输出数据函数writedat()的内容。*/
#include
int cnt, sum ;
void countvalue()
{
}
void main()
{
cnt = sum = 0 ;
countvalue() ;
printf('素数的个数=%d\n', cnt) ;
printf('满足条件素数值的和=%d', sum) ;
writedat() ;
}
writedat()
{
file *fp ;
fp = fopen('out6.dat', 'w') ;
fprintf(fp, '%d\n%d\n', cnt, sum) ;
fclose(fp) ;
}