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

求教各位,帮帮忙!!!
有一个txt文件,里面存储这多行的数字,比如:
000000000000111010010101010000000000000000
111010101000111100000000000000000110010000
000010101010101010010000000000000000000000
100001010100101000000000110000000010001001
110010101010000000000000000000000000000000
按64个字符算成一组, 输出每组中字符为1的下标。 这个下标是每组内的下标,即0-63且衔接也已经完成,现在我要输出的格式中加入判断,就是碰到每组第一个1的时候在输出的上方显示start,最后一个1的时候输出的时候在下方显示end,如下所示:
.......
start后面还要跟上O[0]到0[11]、X[0]到X[63]、I[0]到I[11]
M[8] O[0] 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] I[0]
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);
    }

    sw.WriteLine("I[0] I[1] I[2] I[3] I[4] I[5] I[6] I[7] I[8] I[9] I[10] I[11]");
    sw.WriteLine("X[0] X[1] X[2] X[3] X[4] X[5] X[6] X[7] X[8] X[9] X[10] X[11] X[12] X[13] X[14] X[15] X[16] X[17] X[18] X[19] X[20] X[21] X[22] X[23] X[24] X[25] X[26] X[27] X[28] X[29]");
    sw.WriteLine("X[30] X[31] X[32] X[33] X[34] X[35] X[36] X[37] X[38] X[39] X[40] X[41] X[42] X[43] X[44]");
    sw.WriteLine("X[45] X[46] X[47] X[48] X[49] X[50] X[51] X[52] X[53] X[54] X[55] X[56] X[57] X[58] X[59]");
    sw.WriteLine("X[60] X[61] X[62] X[63] O[0] O[1] O[2] O[3] O[4] O[5] O[6] O[7] O[8] O[9] O[10] O[11]");

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

        if (idxs.Count > 0)
        {
            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;
            }

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

            sw.Flush();
        }
    }
}