网站导航:首页 -> 计算机等级考试 -> 计算机等级考试四级 -> 计算机四级考试复习资料 -> 全国计算机等级四级机试试题及答案一-1012-

全国计算机等级四级机试试题及答案一-1012-

10./*prog10 已知在文件in.dat中存有若干个(个数<200)四位数字的正整数,函数readdat()是读取这若干个正整数并存入数组xx中。请编制函数calvalue(),其功能要求:
  1、求出这文件中共有多少个正整数totnum;
  2、求出这些数中的各位数字之和是奇数的数的个数totcnt,以及满足此条件的这些数的算术平均值totpjz,最后调用函数writedat()把所求的结果输出到文件out1.dat中。
  注意:部分部分源程序存放在prog10.c中。

  请勿改动主函数main(),读数据函数reacddat()和输出数据函数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)
{ int i;
int qw,bw,sw,gw;
double sum=0;
for ( i=0 ; iif (xx[i] > 0)
{
totnum ++ ;
qw = xx[i] / 1000;
bw = xx[i] / 100 - qw * 10;
sw = xx[i] / 10 - qw * 100- bw * 10;
gw = xx[i] % 10;
if ( ( (qw+bw+sw+gw) % 2)!=0)
{
totcnt ++ ;
sum = sum + xx[i];
}
}
totpjz = sum / totcnt;
}
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('out1.dat', 'w') ;
fprintf(fp, '%d\n%d\n%.2lf\n', totnum, totcnt, totpjz) ;
fclose(fp) ;
}                        
11./* 程序prog1.c的功能是:选出100至1000之间所有个位数字与十位数字之和被10除所得余数恰是百位数字的素数(如293)。计算并输出上述这些素数的个数cnt以及这些素数值的和sum。请考生编写函数countvalue( )实现程序的要求,最后调用函数writedat( )把结果cnt和sum输出到文件out6.dat中。
  注意:部分源程序存放在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) ;
}
12. /* 编写函数sumvalue( ),它的功能是:计算正整数n的所有因子(1和n除外)之和作为函数值返回。例如:n=20时,函数值为21。函数readwrite( )是实现从文件in9.dat中读取两个字符串,并调用函数sumvalue(),最后把结果输出到文件out9.dat中。
  注意:部分源程序存在文件prog1.c中,请勿改动主函数main()和其它函数中的任何内容,仅在函数sumvalue()的花括号中填入你编写的若干语句。 */
#include 
#include 
int sumvalue(int n)
{
}
main()
{ clrscr() ;
printf('%d\n', 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\n', s) ;
}
fclose(fp) ;
fclose(wf) ;
}