本文介绍了我将如何让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));
}
}
}
推荐答案
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写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!