试题答案

3773考试网计算机等级考试试题答案正文

三级B上级模拟试题及答案(4)

来源:招生考试网 2005-10-25 15:36:47

.函数ReadDat()实现从文件ENG.IN中读取一篇英文文章存入到
字符串数组xx中; 请编制函数ComWord()分别计算出10个不区分大
小写的英文单词(you,for,your,on,no,if,the,in,to,all)的频数
并依次存入整型数组yy[0]至yy[9]中, 最后调用函数WriteDat( )
把结果yy输出到文件PS1.OUT中。
原始数据文件存放的格式是:每行的宽度均小于80个字符, 含
标点符号和空格。
注意: 部分源程序存放在PROG1.C中。文章每行中的单词与单
词之间用空格或其它标点符号分隔, 每单词均小于20个字符。
请勿改动主函数main()、读数据函数ReadDat()和输出数据函
数WriteDat()的内容。

/*参考答案*/

#include
#include
#include
#include

char WORD[10][10] = {"you", "for", "your", "on", "no","if","the","in","to","all"} ;
char xx[50][80] ;
int yy[10] ;
int maxline = 0 ; /* 文章的总行数 */

int ReadDat(void) ;
void WriteDat(void) ;

void ComWord(void)
{
int i,j,k,n,len;
char word[20],c;

memset(yy,0,10*sizeof(int));

for(i = 0; i < maxline; i++)
{
len = strlen(xx);

n = 0;
for(j = 0; j < len+1; j++)
{
c = xx[j];

if((c>='a' && c<='z') || (c>='A' && c<='Z'))
{
word[n] = c;
n++;
}
else
{
word[n] = '\0';
for(k = 0; k < 10; k++)
if(strcmpi(WORD[k],word) == 0)
yy[k]++;
n = 0;
}
}
}
}

void main()
{
int i ;

clrscr() ;
for(i = 0 ; i < 10 ; i++) yy = 0 ;
if(ReadDat()) {
printf("数据文件ENG.IN不能打开!\n\007") ;
return ;
}
ComWord() ;
WriteDat() ;
}

int ReadDat(void)
{
FILE *fp ;
int i = 0 ;
char *p ;

if((fp = fopen("eng.in", "r")) == NULL) return 1 ;
while(fgets(xx, 80, fp) != NULL) {
p = strchr(xx, '\n') ;
if(p) xx[p - xx] = 0 ;
i++ ;
}
maxline = i ;
fclose(fp) ;
return 0 ;
}

void WriteDat(void)
{
FILE *fp ;
int i ;

fp = fopen("ps1.out", "w") ;
for(i = 0 ; i < 10 ; i++) {
printf("%s=%d\n", strupr(WORD), yy) ;
fprintf(fp, "%d\n", yy) ;
}
fclose(fp) ;
}

触屏版 电脑版
3773考试网 琼ICP备12003406号-1