日期:2014-05-18  浏览次数:20949 次

求教各位,这样要这么判断!!!
有一个txt文件,里面存储这多行的数字,比如:
000000000000111010010101010000000000000000
111010101000111100000000000000000110010000
000010101010101010010000000000000000000000
100001010100101000000000110000000010001001
110010101010000000000000000000000000000000
按64个字符算成一组, 输出每组中字符为1的下标。 这个下标是每组内的下标,即0-63且衔接也已经完成,现在我要输出的格式中加入判断,就是碰到每组第一个1的时候输出start,最后一个1的时候输出end,如下所示:
.......
M[8] start X[8] P[8]  
M[11] P[8] X[11] P[11]  
M[12] P[11] X[12] P[12]
......
M[45] P[40] X[45] end
这样要这么解决?



------解决方案--------------------
C# code
var lines = File.ReadAllLines(@"E:\我的项目文档\c.txt");
var sw = new StreamWriter(@"E:\我的项目文档\De.txt", false);

var blen = 64;
foreach (var item in lines)
{
    string[] blks;
    var count = item.Length / blen;
    var rem = item.Length % blen;
    if (rem > 0)
    {
        blks = new string[count + 1];
        blks[count] = item.Substring(blen * count, rem);
    }
    else
    {
        blks = new string[count];
    }
    for (int i = 0; i < count; i++)
    {
        blks[i] = item.Substring(blen * i, blen);
    }

    for (int i = 0; i < blks.Length; i++)
    {
        var str = blks[i];
        var idxs = new List<int>(blen);
        var index = -1;
        while ((index = item.IndexOf('1', index + 1)) >= 0)
        {
            idxs.Add(index);
        }

        var istr = i.ToString();

        var sidxstr = idxs[0].ToString();
        sw.WriteLine(string.Concat("M", istr, "[", sidxstr, "] O[", istr, "] X[", sidxstr, "] P", istr, "[", sidxstr, "]"));

        var lidx = idxs.Count - 1;
        for (int j = 1; j < lidx; j++)
        {
            var idxstr = idxs[j].ToString();
            sw.WriteLine(string.Concat("M", istr, "[", idxstr, "] P", istr, "[", sidxstr, "] X[", idxstr, "] P", istr, "[", idxstr, "]"));
            sidxstr = idxstr;
        }

        var eidxstr = idxs[lidx].ToString();
        sw.WriteLine(string.Concat("M", istr, "[", eidxstr, "] P", istr, "[", sidxstr, "] X[", eidxstr, "] I[", istr, "]"));

        sw.Flush();
    }
}