簡體   English   中英

如何越界回到數組的開頭?

[英]How to go out of bounds back to the start of an array?

我正在制作凱撒密碼,目前我要轉移3個密碼來加密郵件。 如果任何字母具有“ x,y或z”,它將給我一個超出范圍的數組錯誤(因為移位為3)。

我如何通過返回數組的開頭但以平移的其余部分結尾來傳遞錯誤?

這是我目前的代碼:

using System;
using System.Text;


//caesar cipher
namespace Easy47
{
    class Program
    {
        static void Main(string[] args)
        {
            const string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
            var input = Input(alphabet);
            Encrypt(3, input, alphabet);
            Console.WriteLine();
        }

        private static void Encrypt(int shift, string input, string alphabet)
        {
            var message = new StringBuilder();

            foreach (char letter in input)
            {
                for (int i = 0; i < alphabet.Length; i++)
                {
                    if (letter == alphabet[i])
                    {
                        message.Append(alphabet[i + shift]);
                    }
                }
            }

            Console.WriteLine("\n" + message);
        }

        private static string Input(string alphabet)
        {
            Console.Write("Input your string\n\n> ");

            string input = Console.ReadLine().ToUpper();

            return input;
        }
    }
}

您使用模運算符:

var i = 255
var z = i % 200  // z == 55 

您的情況在這里:

for (int i = 0; i < alphabet.Length; i++)
{
    if (letter == alphabet[i])
    {
        message.Append(alphabet[ (i + shift) % alphabet.Length]);
    }
}

如果加上shift后索引的大小大於alphabet.Length ,則長度將再次從0開始。

請參見C#Ref Modulo運算符


無關,但是您的循環效率不是很高。 一條"ZZZZZ"消息將通過您的完整字母進行5次翻譯。 您應該使用字典作為查找。 您可以在翻譯消息之前一開始就創建它,然后查找就非常快-這就是字典擅長的領域。 O(1)查找:o)

如果您對linq有所了解,這應該是可以理解的:

// needs:  using System.Linq;

private static void Encrypt(int shift, string input, string alphabet)
{
    var message = new StringBuilder();
    // create a string that is shifted by shift characters  
    // skip: skips n characters, take: takes n characters
    // string.Join reassables the string from the enumerable of chars
    var moved = string.Join("",alphabet.Skip(shift))+string.Join("",alphabet.Take(shift));

    // the select iterates through your alphabet, c is the character you currently handle,
    // i is the index it is at inside of alphabet
    // the rest is a fancy way of creating a dictionary for 
    // a->d
    // b->e
    // etc   using alphabet and the shifted lookup-string we created above.
    var lookup = alphabet
        .Select( (c,i)=> new {Orig=c,Chiff=moved[i]})
        .ToDictionary(k => k.Orig, v => v.Chiff);


    foreach (char letter in input)
    {
        // if the letter is not inside your alphabet, you might want to add
        // it "as-is" in a else-branch. (Numbers or dates or .-,?! f.e.)
        if (lookup.ContainsKey(letter)) 
        {
            message.Append(lookup[letter]);
        }
    }

    Console.WriteLine("\n" + message);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM