我将如何让Caesar Cypher写入文本文件 [英] How Would I Get The Caesar Cypher Out Put To Write To A Text File

查看:94
本文介绍了我将如何让Caesar Cypher写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Caesar
{
    class Program
    {
        static void Main(string[] args)
        {
            //Reads text from file I am using absolute links as I am unsure on how to use relative links.
            string Text = System.IO.File.ReadAllText(@"C:\Users\Grimswolf\Documents\Visual Studio 2015\Projects\Caesar\Caesar\Text\caesarShiftEncodedText.txt");
            //iterates through 26 different caesar shift to find the correct shift by using the console.writeline and two variables the decrypt function below the text variable and the alphabet array.

            for (var i = 0; i < 27; i++)
            {
                Console.WriteLine("\n \n{0} \n \n {1}", i, Decrypt(Text, +i));
                
            }
            // leaving the the user to enter any letter to end the program so we can view the results.
            Console.ReadLine();
            
        }
        // method to apply during the loop. has two variables caesarString the actual string variable and and int that shifts the alphabet to the right.
        public static string Decrypt(string caeserString, int move)
        {

            var ab =
                //numbers both capital and normal letters up to 26 using the int move.
                Enumerable
                .Concat(
                                    Enumerable.Range((int)'a', 26),
                        Enumerable.Range((int)'A', 26))
                .Select(i => (char)i)
                .ToArray();
            //stores the characters into a array

            var map =
                ab
                //maps the array.
                    .Zip(
                        ab.Concat(ab).Concat(ab).Skip(ab.Length + move),
                        (c0, c1) => new { c0, c1 })
                    .ToDictionary(x => x.c0, x => x.c1);
                    //returns and formats the string to the main method.
                        return String.Join("", caeserString.Select(x => map.ContainsKey(x) ? map[x] : x));
        }
    }
}

推荐答案

我们不做你的作业:它是为一个原因。它就是为了让你思考你被告知的事情,并试着理解它。它也在那里,以便您的导师可以识别您身体虚弱的区域,并将更多的注意力集中在补救措施上。



亲自尝试,你可能会发现它不是和你想的一样困难!



如果遇到具体问题,请询问相关问题,我们会尽力提供帮助。但是我们不会为你做这一切!
We do not do your homework: it is set for a reason. It is there so that you think about what you have been told, and try to understand it. It is also there so that your tutor can identify areas where you are weak, and focus more attention on remedial action.

Try it yourself, you may find it is not as difficult as you think!

If you meet a specific problem, then please ask about that and we will do our best to help. But we aren't going to do it all for you!


解决了



Solved

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Caesar
{
    class Program
    {
        static void Main(string[] args)
        {
            //Reads text from file I am using absolute links as I am unsure on how to use relative links.
            string Text = System.IO.File.ReadAllText(@"C:\Users\Grimswolf\Documents\Visual Studio 2015\Projects\Caesar\Caesar\Text\caesarShiftEncodedText.txt");
           
            FileStream ostrm;
            StreamWriter writer;
            TextWriter oldOut = Console.Out;
            try
            {
                ostrm = new FileStream(@"C:\Users\Grimswolf\Documents\Visual Studio 2015\Projects\Caesar\Caesar\Text\Shifted.txt", FileMode.OpenOrCreate, FileAccess.Write);
                writer = new StreamWriter(ostrm);
            }
            catch (Exception e)
            {
                Console.WriteLine("Cannot open Redirect.txt for writing");
                Console.WriteLine(e.Message);
                return;
            }
            Console.SetOut(writer);
            //iterates through 26 different caesar shift to find the correct shift by using the console.writeline and two variables the decrypt function below the text variable and the alphabet array.
            for (var i = 0; i < 27; i++)
            {
                Console.WriteLine("\n \n{0} \n \n {1}", i, Decrypt(Text, +i));
                
            }
            Console.SetOut(oldOut);
            writer.Close();
            ostrm.Close();
            for (var i = 0; i < 27; i++)
            {
                Console.WriteLine("\n \n{0} \n \n {1}", i, Decrypt(Text, +i));

            }
            Console.WriteLine("Done");
            // leaving the the user to enter any letter to end the program so we can view the results.
            Console.ReadLine();
            
        }
        // method to apply during the loop. has two variables caesarString the actual string variable and and int that shifts the alphabet to the right.
        public static string Decrypt(string caeserString, int move)
        {

            var ab =
                //numbers both capital and normal letters up to 26 using the int move.
                Enumerable
                .Concat(
                                    Enumerable.Range((int)'a', 26),
                        Enumerable.Range((int)'A', 26))
                .Select(i => (char)i)
                .ToArray();
            //stores the characters into a array

            var map =
                ab
                //maps the array.
                    .Zip(
                        ab.Concat(ab).Concat(ab).Skip(ab.Length + move),
                        (c0, c1) => new { c0, c1 })
                    .ToDictionary(x => x.c0, x => x.c1);
                    //returns and formats the string to the main method.
                        return String.Join("", caeserString.Select(x => map.ContainsKey(x) ? map[x] : x));
        }
    }
}


这篇关于我将如何让Caesar Cypher写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆