谁能把这个问题搞定?急!!

itrydo 2002-04-06 07:25:28
25、函数readdat()实现从文件in.dat中读取一篇文文章存入到字符串数组xx中,请编制
函数str0l(),其函数的功能是:以行为单位对行中以空格或标点符号为分隔的所有单词
进行倒排。最后把已处理的字符串(应不含标点符号)仍按行重新存入字符串数组xx中
,最后调用函数writedat()把结果xx输出到文件out6.dat中。
例如:原文:You He Me
I am a student.
结果:Me He You
student a am I
原始数据文件存放的格式是:每行的宽度均小于80个字符,含标点符号和空格。
部分源程序已给出。
请勿改动主函数main()、读数据函数readdat()和输出数据函数writedat()的内容。
# include"stdio.h"
# include"string.h"
# include"conio.h"
# include"ctype.h"
# include"mem.h"

unsigned char xx[50][80];
int maxline=0;

int readdat(void);
void writedat(void);

void Str0L(void)
{

}

void main()
{
clrscr();
if(readdat())
{printf("Can't open the file ENG.IN!\n");
return;
}
Str0L();
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("out6.dat","w");
for(i=0;i<maxline;i++)
{printf("%s\n",xx[i]);
fprintf(fp,"%s\n",xx[i]);
}
fclose(fp);
}
...全文
64 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyoubing 2002-04-06
  • 打赏
  • 举报
回复
增加函数
void reverse(char * ptstr,int start,int end)
{
char *p, *q;
p = ptstr+start; /*points to the start char of the string*/
q = ptstr+end;/* points to the end char of the string*/
while( p < q ) /* reverse the string */
{
temp = *p;
*p = *q;
*q = temp;
p++;
q--;
}
}
void Str0L(void)
{
int n;
struct reverserange{
int first;
int last;
int flag;
}revrang;


for(int i=0,i<=maxline,i++)
{
revrang.first=0:
revrang.last=0;
revrang.flag=0;

n=strlen(xx[i]);
reverse(xx[i],0,n);/*reverse the all chars of the string.*/
for ( int j = 0; j < n -1; j++ )
{
if((xx[i][j]<'a'||xx[i][j]>'Z')&& xx[i][j]!=' ')
xx[i][j]=' ';
if(xx[i][j]>='a'&& xx[i][j]<='Z'&& revrang.flag==0)
{
revrang.first=j;
revrang.flag=1;
}
if(xx[i][j]==' ' && revrang.flag==1)
{
revrang.last=j-1;
reverse(xx[i],revrang.first,revrang.last);
/* reverse each word of the string.*/
reverse.flag=0;
}
}
}
}

这样就可以得到正确的倒排了。
lyoubing 2002-04-06
  • 打赏
  • 举报
回复
不对用这样的转换后you he me 成了em eh uoy了
Leung_ 2002-04-06
  • 打赏
  • 举报
回复
yeah:

p = &xx[i][0];
q = &xx[i][n-1];
Leung_ 2002-04-06
  • 打赏
  • 举报
回复
oh, I'am sorry to make such a big error.
how about:

p = xx[i][0];
heimeng 2002-04-06
  • 打赏
  • 举报
回复
q = &xx[i][n-1];
heimeng 2002-04-06
  • 打赏
  • 举报
回复
to:Leung_()  

那是不对地

p++;是指向下一个字符串而非指向本字符串的下一个字符
Leung_ 2002-04-06
  • 打赏
  • 举报
回复
Oh, you should define the variables:

int j, n; char temp

another:

n = strlen(xx[i]);
Leung_ 2002-04-06
  • 打赏
  • 举报
回复
you can do like this:

void Str0L(void)
{
int i;
char *p, *q;
for ( i = 0; i < maxline; i++ )
{
p = xx[i]; /* points to the first char of the string*/
strlen( xx[i] );
q = xx[i][n-1]; /* points to the last char of the string*/
while( p < q ) /* reverse the string */
{
temp = *p;
*p = *q;
*q = temp;
p++;
q--;
}
for ( j = 0; j < n -1; j++ )
{
if ( xx[i][j] == ',' || xx[i][j] == '.' ||
xx[i][j] == '?' || xx[i][j] == '!' ||
xx[i][j] == ';' || xx[i][j] == ':' ||
xx[i][j] == ''' || xx[i][j] == '"'
)
xx[i][j] == ' ';
}
if ( xx[i][j] == ',' || xx[i][j] == '.' ||
xx[i][j] == '?' || xx[i][j] == '!' ||
xx[i][j] == ';' || xx[i][j] == ':' ||
xx[i][j] == ''' || xx[i][j] == '"'
) /* test the last char */
xx[i][j] = '\0';
}
}

I'm now in the computer room of our school, I can't debug the program, so I can't gurrantee the correctness of the it. Maybe
there is some errors, you can debug it and modify.


heimeng 2002-04-06
  • 打赏
  • 举报
回复
to:afsfop(Unix Supporter)  用不着得到fp :)

但是如果只允许在Str0L里加东西就有点麻烦了 不过也不难

要是能用堆栈解决这问题再简单不过
itrydo 2002-04-06
  • 打赏
  • 举报
回复
上面这题是计算机等级考试3级B中很典型的一类题目,明天就要考了,可这道题我还不会做,
哪位能帮忙做一下,在下感之不尽!
(一定给分)

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧