2006年9月全國等級考試三級c語言上機題庫(一百)

字號:

★題目100(無憂id 21字符替換題)
    函數(shù)ReadDat()實現(xiàn)從文件IN.DAT中讀取一篇英文文章存入到字符串數(shù)組xx中,請編制函數(shù)CharConvA(),其函數(shù)的功能是:以行為單位把字符串中的最后一個字符的ASCII值右移4位后加最后第二個字符的ASCII值,得到最后一個新的字符,最后第二個字符的ASCII值右移4位后加最后第三個字符的ASCII值,得到最后第二個新的字符,依此類推一直處理到第二個字符,第一個字符的ASCII值加原最后一個字符的ASCII值,得到第一個新的字符,得到的新字符分別存放在原字符串對應的位置上。最后已處理的字符串仍按行重新存入字符串數(shù)組xx中,最后調(diào)用函數(shù)writeDat()把結果xx輸出到文件OUT10.DAT中。
    原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個字符,含標點符號和空格。
    部分源程序存在文件prog1.c中。
    請勿改動主函數(shù)main()和寫函數(shù)writeDat()的內(nèi)容。
    #include
    #include
    #include
    char xx[50][80];
    int maxline=0;/*文章的總行數(shù)*/
    int ReadDat(void);
    void WriteDat(void);
    void CharConvA()
    { int i,j;
    char p,c;
    for(i=0;i    { p=xx[i][strlen(xx[i])-1];
    c=xx[i][0];
    for(j=strlen(xx[i])-1;j>0;j--)
    xx[i][j]=(xx[i][j]>>4)+xx[i][j-1];
    xx[i][0]=p+c;
    }
    }
    void main()
    {
    clrscr();
    if(ReadDat()){
    printf("數(shù)據(jù)文件IN.DAT不能打開!\n\007");
    return;
    }
    CharConvA();
    WriteDat();
    }
    int ReadDat(void)
    {
    FILE *fp;
    int i=0;
    char *p;
    if((fp=fopen("IN.DAT","r"))==NULL) return 1;
    while(fgets(xx[i],80,fp)!=NULL){
    p=strchr(xx[i],'\n');
    if(p)*p=0;
    i++;
    }
    maxline=i;
    fclose(fp);
    return 0;
    }
    void WriteDat(void)
    {
    FILE *fp;
    int i;
    fp=fopen("OUT10.DAT","w");
    for(i=0;i    printf("%s\n",xx[i]);
    fprintf(fp,"%s\n",xx[i]);
    }
    fclose(fp);
    }
    新題補充:
    函數(shù)ReadDat()實現(xiàn)從文件ENG.IN中讀取一篇英文文章存入到字符串數(shù)組xx中;請編制函數(shù)DelWord()分別按行刪除空格、標點符號等非字符,并對每行的單詞按升序排好序后存放回數(shù)組XX中,最后調(diào)用
    函數(shù)WriteDat()把結果xx輸出到文件PS6.OUT中。
    原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個字符,含標點符號和空格。
    注意:部分源程序存放在文件prog1.c中。文章每行中的單詞與單詞之間用空格或其它標點符號分隔,每單詞均小于20個字符。
    請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
    #include
    #include
    #include
    #include
    char xx[50][80] ;
    int maxline = 0 ; /* 文章的總行數(shù) */
    int ReadDat(void) ;
    void WriteDat(void) ;
    void DelWord(void)
    { int i,j,k,m,cnt;
    char *p, word[20],str[80][20],abc[80];
    for(i=0;i    { memset(word,0,20);
    memset(str,0,80);
    memset(abc,0,80);
    k=cnt=0;
    p=xx[i];
    while(*p)
    { while(isalpha(*p)) word[k++]=*p++;
    if(strlen(word)) strcpy(str[cnt++],word);
    memset(word,0,20);
    k=0;
    while(!isalpha(*p)&&*p) p++;
    }
    for(j=0;j    for(m=j+1;m    if(strcmp(str[j],str[m])>0) { strcpy(word,str[j]);strcpy(str[j],str[m]);strcpy(str[m],word);}
    for(j=0;j    strcat(abc,str[j]);
    strcpy(xx[i],abc);
    }
    }
    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[i], 80, fp) != NULL) {
    p = strchr(xx[i], '\n') ;
    if(p) xx[i][p - xx[i]] = 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[i]) ;
    fprintf(fp, "%s\n", xx[i]) ;
    }
    fclose(fp) ;
    }