| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 629 人关注过本帖
标题:发一道题给大家做做!!
只看楼主 加入收藏
我菜119
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:938
专家分:1756
注 册:2009-10-17
结帖率:98.26%
收藏
已结贴  问题点数:20 回复次数:8 
发一道题给大家做做!!
编写一个程序,从文件中一行一行的读取文本,并完成如下任务:
如果文件中有两行或者是更多行相邻的文本内容相同,那么就打印出其中一行!你可以假定文件中的文本在长度上不会超过128个字符!!!

考虑下面对输入文件:
This is the first line.Another line.
And another.
And another.
And another.
And another.
Still more.
Almost done now --
Almost done now --
Another line.
Still more.
Finished!
输出的结果是:
This is the first line.Another line.
And another.
Still more.
Almost done now --
Another line.
Still more.
Finished!
搜索更多相关主题的帖子: first 
2010-10-20 11:20
我菜119
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:938
专家分:1756
注 册:2009-10-17
收藏
得分:0 
这是我自己写的代码,供大家参考一下:
程序代码:
# include <stdio.h>
# include <stdlib.h>
# include <string.h>

char string[300][130] ;

int
main()
{
    FILE *file_point ;
    char* pfile_name = "E:\\file.txt" ;
    int i , j ;
    if( ( file_point = fopen( pfile_name , "r") ) == NULL )
    {
        printf( "open error !\n" ) ;
        exit( EXIT_FAILURE ) ;
    }
    for( i = 0 ; !feof( file_point ) ; i++ )
    {
        fgets( string[i] ,sizeof( string[i] ) , file_point ) ;
    }
    for(j = 0 ; j < i ; j++ )
    {
        if( strcmp( string[j] , string[j + 1] ) == 0 )
            continue ;
        else
            puts( string[j] ) ;
    }
    return 0 ;
}

愿用余生致力编程
2010-10-20 13:41
vandychan
Rank: 15Rank: 15Rank: 15Rank: 15Rank: 15
等 级:贵宾
威 望:18
帖 子:2296
专家分:6418
注 册:2010-8-20
收藏
得分:3 
不太想做

到底是“出来混迟早要还”还是“杀人放火金腰带”?
2010-10-20 14:58
tzp876301129
Rank: 2
等 级:论坛游民
帖 子:29
专家分:31
注 册:2010-5-16
收藏
得分:3 
不太想做
2010-10-20 16:50
我菜119
Rank: 10Rank: 10Rank: 10
等 级:青峰侠
帖 子:938
专家分:1756
注 册:2009-10-17
收藏
得分:0 
大家觉得这样的题很容易吗???不屑一做吗??悲哀呀!!!

愿用余生致力编程
2010-10-21 12:36
ququguoguo
Rank: 2
等 级:论坛游民
帖 子:73
专家分:90
注 册:2010-10-20
收藏
得分:3 
我是新手 ,是来学习的。。。。
2010-10-21 14:13
哈工马靖
Rank: 1
等 级:新手上路
帖 子:1
专家分:3
注 册:2010-10-21
收藏
得分:3 
我也是来学习的,对c语言还懂的不多
2010-10-21 14:14
linshao512
Rank: 2
等 级:论坛游民
帖 子:15
专家分:18
注 册:2010-10-10
收藏
得分:3 
有时间先
2010-10-21 14:39
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
收藏
得分:3 
这个问题用正则表达式解决再合适不过了。
代码是用C#写的,已经5、6年没用过C语言了还请见谅。
用什么语言并不重要,算法的思想是通用的。
using System;
using System.Text.RegularExpressions;
using
namespace RegTest
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                StreamReader sr = new StreamReader(args[0]);
                string str = sr.ReadToEnd();
                Regex reg = new Regex(@"(^.+)((\r\n\1)+)", RegexOptions.Multiline);
                string result = reg.Replace(str, "$1");
                Console.WriteLine(result);
            }
            catch
            {
                Console.WriteLine("File is not exist.");
            }
        }
    }
}

重剑无锋,大巧不工
2010-10-21 16:31
快速回复:发一道题给大家做做!!
数据加载中...
 
   



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

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