三级考试上级题一求答案(要适当注释)

amartapple 2004-09-15 11:07:08
三级考试上级题一求答案(要适当注释)

函数ReadDat()实现从文件IN.DAT中读取一篇英文文章存入到字符串数组xx中,请编制函数StrOL(),其函数的功能是:以行为单位对行中以空格或标点符号为分隔的所有单词进行倒排。最后把已处理的字符串(应不含标点符号)仍按行重新存入字符串数组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>

char xx[50][80];
int maxline = 0; /*文章的总行数*/

int ReadDat(void)void WriteDat(void)

void StrOL(void)
{
}

void main()
{
clrscr();
if (ReadDat())
{
printf("数据文件ENG.IN不能打开!\n\007");
return ;
}
StrOL();
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("OUT6.DAT", "w");
for (i = 0; i < maxline; i++)
{
printf(" %s\n", xx[i]);
fprintf(fp, " %s\n", xx[i]);
}
fclose(fp);
}
...全文
55 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ftkghost 2004-09-15
  • 打赏
  • 举报
回复
#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 StrOL(void);

void main()
{
clrscr();
if(ReadDat())
{
printf("Can't open the file!\n\007");
return;
}
StrOL();
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=strrchr(xx[i],'\n');
if(p) *p=0;
i++;
}
maxline=i;
fclose(fp);
return 0;
}

void WriteDat(void)
{
FILE *fp;
int i;

clrscr();
fp=fopen("OUT6.DAT","w");
for(i=0;i<maxline;i++)
{
printf("%s\n",xx[i]);
fprintf(fp,"%s\n",xx[i]);
}
fclose(fp);
}
void StrOL()
{
int i,j,m,n,t1,t2,u; /*循环控制变量*/
char temp[80][20];
for(i=0;i<maxline;i++){
m=0;
n=0;
u=0;
j=0;
while(xx[i][j]!=0){
if((xx[i][j]>=97&&xx[i][j]<=122)||(xx[i][j]>=65&&xx[i][j]<=90)){
temp[m][n]=xx[i][j];
n++;
}
else{
temp[m][n]=0; /*在每个单词(字符串)最后一个字母后加上'\0'*/
m++;
n=0;
}
j++;
}
for(t1=m;t1>=0;t1--){ //连接单词(你可以用strcat函数连接)
for(t2=0;temp[t1][t2]!=0;t2++){
xx[i][u]=temp[t1][t2];
u++;
}
xx[i][u]=' '; //每个单词以空格间隔开
u++;
}
}
}

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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