关闭

今天遇到的一个算发

525人阅读 评论(0) 收藏 举报

encode:

一字符串:如果字符小于0则直接拷贝;如果是数字则拷贝当前数字加一个后继字符串;如果是其他字母等直接拷贝;如果是_下划线则转换成/UL;

decode:解码

encode代码部分:
            strOld = this.textBox1.Text;
            strLeng = strOld.Length;
            char[] a=strOld.ToCharArray();
            for(int i=0;i<strLeng;i++)
            {
              
                intNow=a[i];
                if(intNow<intZero)
                {
                    strNew+=a[i]+"_";
                }
                if((intNow>intZero)&&(intNow<intNight))
                {
                    if (i == strLeng-1)
                    {
                        strNew += a[i] ;
                    }
                    else
                    {
                        int ab = Convert.ToInt32(a[i].ToString());
                        //MessageBox.Show(ab.ToString());
                        for (int j = 0; j < ab + 1; j++)
                        {
                            strNew += a[i + 1];
                        }
                        strNew += "_";
                    }
                }
                if(intNow>intNight)
                {
                    if (intNow != intheng)
                    {
                        strNew += a[i] + "_";
                    }
                    else
                    {
                        strNew += "//UL";
                    }
                }
               
            }
            this.textBox3.Text=strNew;

 

decode:

 string enStrOld = this.textBox3.Text.Replace("//UL","_");
            string enStrNew = "";
            char[] ena = enStrOld.ToCharArray();
            int[] enb=new int[50];
            int enbindex=0;
            int intLength = enStrOld.Length;
            for (int eni = 0; eni < intLength; eni++)
            {
                if (ena[eni] == intheng)
                {
                    enb[enbindex] = eni;
                    enbindex++;
                }
               
            }
            for (int k = 0; k < enbindex; k++)
            {
                int ent = enb[k];
                if (ena[ent - 1] == ena[ent - 2])
                {
                    if (k == 0)
                    {
                        enStrNew += enb[k] - 1;
                    }
                    else
                    {
                        int test = enb[k] - enb[k - 1] - 2;
                        if (test > 0)
                        {
                            enStrNew += test;
                        }
                    }
                }
                else
                {
                   
                    int encount = enb[k] - enb[k - 1] - 1;
                    if (encount == 0)
                    {
                        enStrNew += "_";
                    }
                    else
                    {
                        for (int entt = 0; entt < encount; entt++)
                        {
                            enStrNew += ena[enb[k - 1] + 1 + entt];
                        }
                    }
                }
            }
            enStrNew += ena[enStrOld.LastIndexOf("_") + 1];
            this.textBox2.Text = enStrNew;

思路:关键是解码时候需要用第二个数组存储需要解码中的特殊字符串"_"的位置,然后再分别计算下划线前的部门

private int intZero = (char)'0';
        private int intNight = (char)'9';
        private int intheng = (char)'_';
        private string strOld = "";
        private int strLeng;
        private int intNow;
        private string strNew="";

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:100283次
    • 积分:1483
    • 等级:
    • 排名:千里之外
    • 原创:52篇
    • 转载:20篇
    • 译文:0篇
    • 评论:5条
    文章分类
    最新评论