今天遇到的一个算发

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="";

阅读更多
文章标签: string 存储
上一篇关于Service Unavailable的解决方法
下一篇c#.net下的加密例子
想对作者说点什么? 我来说一句

EM_Introduction.ppt

2009年04月18日 904KB 下载

南方系列软件2.07算法注册机

2009年08月26日 1.28MB 下载

MATLAB经典十大算发

2009年09月06日 32KB 下载

没有更多推荐了,返回首页

关闭
关闭