二級C上機模擬試題及答案(5)

字號:

函數ReadDat()實現從文件ENG.IN中讀取一篇英文文章存入到
    字符串數組xx中; 請編制函數ComWord()分別計算出單詞長度2,4,
    6,8的單詞數以及單詞總數并依次存入整型數組yy[0]至yy[4]中,
    最后調用函數WriteDat()把結果yy輸出到文件PS3.OUT中。
    原始數據文件存放的格式是:每行的寬度均小于80個字符, 含
    標點符號和空格。
    注意: 部分源程序存放在PROG1.C中。文章每行中的單詞與單
    詞之間用空格或其它標點符號分隔, 每單詞均小于20個字符。
    請勿改動主函數main()、讀數據函數ReadDat()和輸出數據函
    數WriteDat()的內容。
    /*參考答案*/
    #include
    #include
    #include
    #include
    char xx[50][80] ;
    int yy[5] ;
    int maxline = 0 ; /* 文章的總行數 */
    int ReadDat(void) ;
    void WriteDat(void) ;
    void ComWord(void)
    {
    int i,j,k,n,len,s[4] = {2,4,6,8};
    char word[20],c;
    memset(yy,0,5*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’;
    if(word[0] != ’\0’)
    {
    for(k = 0; k < 4; k++)
    if(n == s[k])
    yy[k]++;
    yy[4]++;
    }
    n = 0;
    }
    }
    }
    }
    void main()
    {
    int i ;
    clrscr() ;
    for(i = 0 ; i < 5 ; 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’) ;