2006年9月全國等級考試三級c語言上機(jī)題庫(九十八)

字號:

題目98(無憂id 85 字符替題)
    (無憂id 85只是將替代關(guān)系改為了f(p)=p*11 mod 256))
     函數(shù)ReadDat()實(shí)現(xiàn)從文件ENG.IN中讀取一篇英文文章,存入到字符串?dāng)?shù)組xx中;請編制函數(shù)encryptChar(),按給定的替代關(guān)系對數(shù)組xx中的所有字符進(jìn)行替代,仍存入數(shù)組xx的對應(yīng)的位置上,最后調(diào)用函數(shù)WriteDat()把結(jié)果xx輸出到文件PS3.DAT中。
    替代關(guān)系:f(p)=p*17 mod 256(p是數(shù)組中某一個字符的ASCII值,f(p)是計(jì)算后新字符的ASCII值),如果計(jì)算后f(p)值小于等于32或其ASCII值是奇數(shù),則該字符不變,否則將f(p)所對應(yīng)的字符進(jìn)行替代。
    部分源程序存在文件prog1.c中。原始數(shù)據(jù)文件存放的格式是:每行的寬度均小于80個字符。
    請勿改動主函數(shù)main()、讀數(shù)據(jù)函數(shù)ReadDat()和輸出數(shù)據(jù)函數(shù)WriteDat()的內(nèi)容。
    #include
    #include
    #include
    #include
    unsigned char xx[50][80];
    int maxline=0;/*文章的總行數(shù)*/
    int ReadDat(void);
    void WriteDat(void);
    void encryptChar()
    { int i,j;
    for(i=0;i    for(j=0;j    if(xx[i][j]*17%256<=32||(xx[i][j]*17%256)%2!=0) continue;
    else xx[i][j]=xx[i][j]*17%256;
    }
    void main()
    {
    clrscr();
    if(ReadDat()){
    printf("數(shù)據(jù)文件ENG.IN不能打開!\n\007");
    return;
    }
    encryptChar();
    WriteDat();
    }
    int ReadDat(void)
    {
    FILE *fp;
    int i=0;
    unsigned 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)*p=0;
    i++;
    }
    maxline=i;
    fclose(fp);
    return 0;
    }
    void WriteDat(void)
    {
    FILE *fp;
    int i;
    fp=fopen("ps3.dat","w");
    for(i=0;i    printf("%s\n",xx[i]);
    fprintf(fp,"%s\n",xx[i]);
    }
    fclose(fp);
    }
    此題還有許多解法,方法可看題8