| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 680 人关注过本帖
标题:这道简单的3级题把我弄晕了
只看楼主 加入收藏
Oscar_0049
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-6-29
收藏
 问题点数:0 回复次数:5 
这道简单的3级题把我弄晕了
题目74:函数READDAT()实现从文件IN.DAT中读取一篇英文文章存入到字符串数组XX中;请编制函数StrCharJL(),其函数功能是:以行为单位把字符串中的所有字符的ASCII值左移4位,如果左移后,其字符的ASCII值小于等于32或大于100,则原字符保持不变.否则就把左移后的字符ASCII值再加上原字符的ASCII值,得到新的字符仍存入原字符串对应的位置上。最后把已处理的字符串仍按行重新存入字符串数组XX中,最后调用函数WRITEDAT()把结果XX输出到文件OUT7.DAT文件中.原始数据文件存放的格式是:每行的宽度均小于80个字符(含标点符号和空格).

IN.DAT
Madame Curie
Madame Curie was born in Warsaw in 1867. She was the daughter of a professor of physics and mathematics. As a young girl she left Poland and studied science at paris University. Soon she met the French scientist, Pierre Curie, and they got married in 1895.
They were both very poor, so they had to work in an laboratory. But they discovered two new elements. One was called polonium because Marie came from Poland. The other was called radium.
In 1926 pierre was killed in an accident. Marie was very sad, but her work went on. She won two Nobel prizes, in 1903 and in 1901. she died in 1934.

[[it] 本帖最后由 Oscar_0049 于 2008-3-29 11:49 编辑 [/it]]
搜索更多相关主题的帖子: 标点符号 英文文章 字符串 
2008-03-29 11:42
Oscar_0049
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-6-29
收藏
得分:0 
我做的
#include <stdio.h>
#include <string.h>
#include <conio.h>
char xx[50][80];
int maxline=0;/*文章的总行数*/
int ReadDat(void);
void WriteDat(void);
void StrCharJL(void)
{
    int i,j;
    char ch;
    for(i=0;i<maxline;i++)
        for(j=0;xx[i][j];j++)
        {
            ch=xx[i][j]<<4;
            if(ch<=32||ch>100);
            else xx[i][j]+=ch;
        }

}
void main()
{

     if(ReadDat())
{
          printf("数据文件IN.DAT不能打开!\n\007");
          return;
     }    
     StrCharJL();
     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("OUT7.DAT","w");
     for(i=0;i<maxline;i++)
{
           printf("%s\n",xx[i]);
           fprintf(fp,"%s\n",xx[i]);
     }
     fclose(fp);
}

结果
OUT7.DAT
Ma?m?s舝i?
Ma?m?s舝i?wa?born in War?w in 18?. 僪?wa?磆??舋h吹r o?a pro频#or o?
 phy?摚 an?ma磆祄a磇摚. A?a yo舗g girl ??l灯?Polan?an?4扭i丹 ?i祅摰 a
?pari??i值r?磞. 僶on ??m荡 磆??祅揾 ?i祅磇4, Pi祌r?s舝i? an?磆祔 g
o?marri丹 in 189?
攈祔 w祌?bo磆 值ry poor, ? 磆祔 ha?磑 work in an labora磑ry. B糯 磆祔 ??o?
祌丹 磜o n祑 祃祄祅矗. On?wa?揳ll丹 poloni舖 b祿a牛?Mari?揳m?苧om Polan?
攈?o磆祌 wa?揳ll丹 ra?舖.
In 192?pi祌r?wa?kill丹 in an a摀iさn? Mari?wa?值ry ?? b糯 h祌 work w祅?
 on. 僪?won 磜o Nob祃 priz担, in 190c an?in 1901. ???丹 in 19ct.

[[it] 本帖最后由 Oscar_0049 于 2008-3-29 11:51 编辑 [/it]]

如果不能改变环境,那就要适应环境!
2008-03-29 11:43
Oscar_0049
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-6-29
收藏
得分:0 
南开答案
#include <stdio.h>
#include <string.h>
#include <conio.h>
char xx[50][80];
int maxline=0;/*文章的总行数*/
int ReadDat(void);
void WriteDat(void);
void StrCharJL(void)
{
    int i,j,str;
    for(i=0;i<maxline;i++)
    {
        str=strlen(xx[i]);
        for(j=0;j<str;j++)
            if (xx[i][j]<<4<=32||xx[i][j]<<4>100)
                ;
            else xx[i][j]+=xx[i][j]<<4;
    }

}
void main()
{

     if(ReadDat())
{
          printf("数据文件IN.DAT不能打开!\n\007");
          return;
     }    
     StrCharJL();
     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("OUT7.DAT","w");
     for(i=0;i<maxline;i++)
{
           printf("%s\n",xx[i]);
           fprintf(fp,"%s\n",xx[i]);
     }
     fclose(fp);
}

结果:
OUT7.DAT
Madame Curie
Madame Curie was born in Warsaw in 1867. She was the daughter of a professor of
 physics and mathematics. As a young girl she left Poland and studied science a
t paris University. Soon she met the French scientist, Pierre Curie, and they g
ot married in 1895.
They were both very poor, so they had to work in an laboratory. But they discov
ered two new elements. One was called polonium because Marie came from Poland.
The other was called radium.
In 1926 pierre was killed in an accident. Marie was very sad, but her work went
 on. She won two Nobel prizes, in 1903 and in 1901. she died in 1934.

如果不能改变环境,那就要适应环境!
2008-03-29 11:47
Oscar_0049
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2007-6-29
收藏
得分:0 
感觉两个程序差不多,为什么结果不一样呢?请教各位

如果不能改变环境,那就要适应环境!
2008-03-29 11:50
yunsuoyan
Rank: 1
等 级:新手上路
帖 子:31
专家分:0
注 册:2007-10-16
收藏
得分:0 
int i,j;
    char ch;
    for(i=0;i<maxline;i++)
        for(j=0;xx[i][j];j++)//判定结束循环的条件不对,不理解你的xx[i][j],改掉就好了!
        {
            ch=xx[i][j]<<4;
            if(ch<=32||ch>100);
            else xx[i][j]+=ch;
        }
2008-03-29 13:45
forever74
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
来 自:CC
等 级:版主
威 望:58
帖 子:1685
专家分:4252
注 册:2007-12-27
收藏
得分:0 
你的程序唯一的错误就在于使用了char类型的ch保存了左移4位的结果,这时必然会造成数据溢出从而丢失数据位。如果你的ch改为int类型的就不会出错了。
2008-03-29 17:12
快速回复:这道简单的3级题把我弄晕了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014246 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved