★题目100(无忧id 21字符替换题)
函数ReadDat()实现从文件IN.DAT中读取一篇英文文章存入到字符串数组xx中,请编制函数CharConvA(),其函数的功能是:以行为单位把字符串中的最后一个字符的ASCII值右移4位后加最后第二个字符的ASCII值,得到最后一个新的字符,最后第二个字符的ASCII值右移4位后加最后第三个字符的ASCII值,得到最后第二个新的字符,依此类推一直处理到第二个字符,第一个字符的ASCII值加原最后一个字符的ASCII值,得到第一个新的字符,得到的新字符分别存放在原字符串对应的位置上。最后已处理的字符串仍按行重新存入字符串数组xx中,最后调用函数writeDat()把结果xx输出到文件OUT10.DAT中。
原始数据文件存放的格式是:每行的宽度均小于80个字符,含标点符号和空格。
部分源程序存在文件prog1.c中。
请勿改动主函数main()和写函数writeDat()的内容。
#include <stdio.h>
#include <string.h>
#include <conio.h>
char xx[50][80];
int maxline=0;/*文章的总行数*/
int ReadDat(void);
void WriteDat(void);
void CharConvA()
{ int i,j;
char p,c;
for(i=0;i<maxline;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("数据文件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<maxline;i++){
printf("%s\n",xx[i]);
fprintf(fp,"%s\n",xx[i]);
}
fclose(fp);
}
新题补充:
函数ReadDat()实现从文件ENG.IN中读取一篇英文文章存入到字符串数组xx中;请编制函数DelWord()分别按行删除空格、标点符号等非字符,并对每行的单词按升序排好序后存放回数组XX中,最后调用
函数WriteDat()把结果xx输出到文件PS6.OUT中。
原始数据文件存放的格式是:每行的宽度均小于80个字符,含标点符号和空格。
注意:部分源程序存放在文件prog1.c中。文章每行中的单词与单词之间用空格或其它标点符号分隔,每单词均小于20个字符。
请勿改动主函数main()、读数据函数ReadDat()和输出数据函数WriteDat()的内容。
#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <ctype.h>
char xx[50][80] ;
int maxline = 0 ; /* 文章的总行数 */
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<maxline;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<cnt-1;j++)
for(m=j+1;m<cnt;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<cnt;j++)
strcat(abc,str[j]);
strcpy(xx[i],abc);
}
}
void main()
{
clrscr() ;
if(ReadDat()) {
printf("数据文件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) ;
}
考试编辑:admin