第四、五章解決隊列和串的編程問題

來源:互聯網
上載者:User

標籤:資料結構 隊列 串 凱撒加密解密

第四章      解決隊列的編程問題

隊列是一種特殊的線性表,是一種只允許在表的一端進行插入操作而在另一端進行刪除操作的線性表。把進行插入操作的表尾稱為隊尾,進行刪除操作的頭部稱為對頭;

         隊列的主要特點是:先進先出,或後進後出

         用一片連續的儲存空間來儲存隊列中的資料元素,這樣的隊列稱為順序隊列。

         將順序隊列看成是首位相接的迴圈結構,這種隊列叫做迴圈隊列。

第五章      解決串的編程問題

串即字串,是由0個或多個字元組成的有限序列,是資料元素為單個字元的特殊線性表。

順序結構儲存串:

         串的靜態儲存結構即順序儲存結構是用一組地址連續的儲存單中繼存放區串的字元序列。因此可用進階語言的字元數組來實現。不同的語言在用數組存放字串時,其處理方式可能有所不同。

1)  C語言:先預先定義數組的大小,為每個串變數分配一個固定長度的數組,接著再使用定長的字元數組存放串內容外,一般可使用一個不會出現在傳中的特殊字元“\0”放在串值的末尾來表示串的結束。如,儲存字串“abc”,申請的儲存地區可能大於實際的大小。

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/49/E3/wKiom1QeG0LSm3SzAAAmrnZQZpY165.jpg" title="1.png" alt="wKiom1QeG0LSm3SzAAAmrnZQZpY165.jpg" />

2)  C#等.net語言中,字串用常量的方式儲存。這意味著,當你用C#建立一個字串時,該字串以一種固定大小儲存在記憶體中以便CLR(通用語言運行時)運行更快一點。如,儲存字串“abc”。

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/49/E5/wKioL1QeG2CDiZpWAAAXqAHZxF8820.jpg" title="2.png" alt="wKioL1QeG2CDiZpWAAAXqAHZxF8820.jpg" />

凱撒密碼

凱撒密碼是一種簡單的密碼密碼編譯演算法,通過將資訊中的每個字母在字母表中向後移動常量k,以實現加密。例如,k=3,則a->d,b->e等;即映射關係為:

F(a)=(a+k)mod n

其中,a為要加密的字母;k為移動的位元,n是字母的長度。

要解密資訊,則將每個字母向前移動相同數目的字元即可。例如,如果k=3,對於已加密資訊frpsxwhu vbvwhpv,將解密為computer system;

【基本要求】

編寫程式實現加密和解密演算法

C#代碼實現如下:

1)  凱撒加密解密類封裝

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace LinkStack

{

    class Kaiser_EncDec

    {

        /// <summary>

        /// 密碼編譯演算法

        /// </summary>

        /// <paramname="str"></param>

        /// <paramname="k"></param>

        ///<returns></returns>

        public static stringEncrypt(string str, int k)

        {

            string res =string.Empty;

            char[] chalst =str.ToCharArray();

            int len =str.Length;

            for (int i = 0; i < len; i++)

            {

                inttmp=(int)chalst[i];

                if (tmp >=65 && tmp <= 90)

 

                {

                    tmp =(tmp-65 + k) % 26+65;

                }

                else if (tmp>= 97 && tmp <= 122)

                {

                    tmp = (tmp- 98 + k) % 26 + 98;

                }

                res +=Encoding.ASCII.GetString(new byte[]{(byte)tmp});

            }

            return res;

        }

        /// <summary>

        /// 解密演算法

        /// </summary>

        /// <paramname="str"></param>

        /// <paramname="k"></param>

        ///<returns></returns>

        public static stringDecrypt(string str, int k)

        {

            string res =string.Empty;

            char[] chalst =str.ToCharArray();

            int len =str.Length;

            for (int i = 0; i< len; i++)

            {

                int tmp =(int)chalst[i];

                if (tmp >=65 && tmp <= 90)

                {

                    tmp = (tmp - 65 - k+26) % 26 + 65;

                }

                else if (tmp>= 97 && tmp <= 122)

                {

                    tmp = (tmp- 98 - k+26) % 26 + 98;

                }

                res +=Encoding.ASCII.GetString(new byte[] { (byte)tmp });

            }

            return res;

        }

    }

}

2)  主程式調用

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace LinkStack

{

    class Program

    {

        static voidMain(string[] args)

        {

            string str ="Computer system";

           Console.WriteLine("要加密的字串:"+str);

            stringstr_1=Kaiser_EncDec.Encrypt(str, 3);

           Console.WriteLine("加密後的字串:" + str_1);

            stringstr_2=Kaiser_EncDec.Decrypt(str_1, 3);

           Console.WriteLine("解密後的字串:" + str_2);

            Console.ReadKey();

        }

    }

}


本文出自 “虎哥的部落格” 部落格,請務必保留此出處http://7613577.blog.51cto.com/7603577/1556630

第四、五章解決隊列和串的編程問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.