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

★題目16(無憂id 23 字元替換題)

 

函式readdat()實現從檔案in.dat中讀取一篇英文文章存入到字元串數組xx中,請編制函式strcharjr(),其函式的功能是:以行為單位把字元串中所有字元的ascii值右移4位,然後把右移後的字元ascii值再加上原字元的ascii值,得到新的字元仍存入原字元串對應的位置上。最後把已處理的字元串仍按行重新存入字元串數組xx中,最後調用函式writedat()把結果xx輸出到檔案out8.dat中。

原始數據檔案存放的格式是:每行的寬度均小於80個字元,含標點符號和空格。

部分源程式存在檔案prog1.c中。

請勿改動主函式main()、讀數據函式readdat()和輸出數據函式writedat()的內容。

#include <stdio.h>

#include <string.h>

#include <conio.h>

 

char xx[50][80];

int maxline=0;/*文章的總行數*/

 

int readdat(void);

void writedat(void);

 

void strcharjr()

{int i,j;

for(i=0;i<maxline;i++)

for(j=0;j<strlen(xx[i]);j++)

xx[i][j]+=(xx[i][j]>>4);

}

 

void main()

{

clrscr();

if(readdat()){

printf("數據檔案in.dat不能打開!\n\007");

return;

}

strcharjr();

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;

clrscr();

fp=fopen("out8.dat","w");

for(i=0;i<maxline;i++){

printf("%s\n",xx[i]);

fprintf(fp,"%s\n",xx[i]);

}

fclose(fp);

}