計算機等級考試三級機試C語言編程詳細解析一、替換字符

字號:

一、替換字符
    函數(shù)ReadDat()實現(xiàn)從文件ENG.IN中讀取一篇英文文章,存入到字符串數(shù)組xx中;請編制函數(shù)encryptChar(),按給定的替代關系對數(shù)組xx中的所有字符進行替代,仍存入數(shù)組xx的對應的位置上,最后調(diào)用函數(shù)WriteDat()把結(jié)果xx輸出到文件PS10.DAT中。
    替代關系:f(p)=p*11 mod 256 (p是數(shù)組中某一個字符的ASCII值,f(p)是計算后新字符的ASCII值),如果原字符的ASCII值是偶數(shù)或計算后f(p)值小于等于32,則該字符不變,否則將f(p)所對應的字符進行替代。
    部分源程序已給出,原始數(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()
    {
    }
    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++;
    }