二級B上級模擬試題及答案(6)

字號:

函數(shù)readdat()實現(xiàn)從文件eng.in中讀取一篇英文文章存入到 字符串?dāng)?shù)組xx中; 請編制函數(shù)delword()分別按行刪除空格、標(biāo)點(diǎn) 符號以及10個不區(qū)分大小寫的英文單詞(you,for,your,on,no,if, the,in,to,all), 余下的單詞按順序重新存入數(shù)組xx中, 最后調(diào)用 函數(shù)writedat()把結(jié)果xx輸出到文件ps6.out中。
    例如: 原文: you are a student.
    結(jié)果: areastudent
    原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個字符, 含 標(biāo)點(diǎn)符號和空格。
    注意: 部分源程序存放在prog1.c中。文章每行中的單詞與單 詞之間用空格或其它標(biāo)點(diǎn)符號分隔, 每單詞均小于20個字符。
    請勿改動主函數(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 maxline = 0 ; /* 文章的總行數(shù) */
    int readdat(void) ;
    void writedat(void) ;
    void delword(void)
    {
    int i,j,k,n,len;
    char word[20],c;
    char str[80];
    for(i = 0; i < maxline; i++)
    {
    len = strlen(xx);
    memset(str,0,80*sizeof(char));
    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 < 10; k++)
    if(strcmpi(word[k],word) == 0)
    break;
    if(k >= 10)
    strcat(str,word);
    }
    n = 0;
    }
    }
    len = strlen(str);
    memcpy(xx,str,len+1);
    }
    }
    void main()
    {
    clrscr() ;
    if(readdat()) {
    printf("數(shù)據(jù)文件eng.in不能打開!\n\007") ;
    return ;
    }
    delword() ;
    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("ps6.out", "w") ;
    for(i = 0 ; i < maxline ; i++) {
    printf("%s\n", xx) ;
    fprintf(fp, "%s\n", xx) ;
    }
    fclose(fp) ;
    }