二級(jí)C上機(jī)模擬試題及答案(4)

字號(hào):

函數(shù)ReadDat()實(shí)現(xiàn)從文件ENG.IN中讀取一篇英文文章存入到
    字符串?dāng)?shù)組xx中; 請(qǐng)編制函數(shù)ComWord()分別計(jì)算出10個(gè)不區(qū)分大
    小寫(xiě)的英文單詞(you,for,your,on,no,if,the,in,to,all)的頻數(shù)
    并依次存入整型數(shù)組yy[0]至yy[9]中, 最后調(diào)用函數(shù)WriteDat( )
    把結(jié)果yy輸出到文件PS1.OUT中。
    原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個(gè)字符, 含
    標(biāo)點(diǎn)符號(hào)和空格。
    注意: 部分源程序存放在PROG1.C中。文章每行中的單詞與單
    詞之間用空格或其它標(biāo)點(diǎn)符號(hào)分隔, 每單詞均小于20個(gè)字符。
    請(qǐng)勿改動(dòng)主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函
    數(shù)WriteDat()的內(nèi)容。
    /*參考答案*/
    #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 ; /* 文章的總行數(shù) */
    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("數(shù)據(jù)文件ENG.IN不能打開(kāi)!\n\007") ;
    return ;
    }
    ComWord() ;
    WriteDat() ;
    }
    int ReadDat(void)
    {
    FILE *fp ;
    int i = 0 ;
    char *p ;