Guest User

Reddit project 3e

a guest
May 2nd, 2012
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.IO;
  5.  
  6. namespace Third___Easy
  7. {
  8.     class Program
  9.     {
  10.         static TextReader input;
  11.         static TextWriter output;
  12.         static int step;
  13.  
  14.         static void Main(string[] args)
  15.         {
  16.             Initialize(args);
  17.             string line = input.ReadLine();
  18.  
  19.             while (line.Length > 0)
  20.             {
  21.                 StringBuilder outputLine = new StringBuilder(line.Length);
  22.                 foreach (char c in line)
  23.                 {
  24.                     if (char.IsLower(c))
  25.                     {
  26.                         outputLine.Append(CaesarCypherLower(c));
  27.                     }
  28.                     else if (char.IsUpper(c))
  29.                     {
  30.                           outputLine.Append(CaesarCypherUpper(c));
  31.                     }
  32.                     else
  33.                     {
  34.                           outputLine.Append(CaesarCypherOther(c));
  35.                     }
  36.                 }
  37.                 output.WriteLine(outputLine);
  38.                 line = input.ReadLine();
  39.             }
  40.         }
  41.  
  42.         private static void Initialize(string[] args)
  43.         {
  44.             input = Console.In;
  45.             output = Console.Out;
  46.             step = 1;
  47.  
  48.             for (int i = 0; i < args.Length; i++)
  49.             {
  50.                 if (args[i] == "-i")
  51.                 {
  52.                     input = new StreamReader(args[i + 1]);
  53.                     i++;
  54.                 }
  55.                 else if (args[i] == "-o")
  56.                 {
  57.                     output = new StreamWriter(args[i + 1]);
  58.                     i++;
  59.                 }
  60.                 else if (args[i] == "-d")
  61.                 {
  62.                     step *= -1;
  63.                 }
  64.                 else if (args[i] == "-s")
  65.                 {
  66.                     step *= int.Parse(args[i + 1]);
  67.                     i++;
  68.                 }
  69.             }
  70.         }
  71.  
  72.         private static char CaesarCypherLower(char letter)
  73.         {
  74.             byte asciiValue = Encoding.ASCII.GetBytes(new char[] { letter })[0];
  75.             byte asciiMin = Encoding.ASCII.GetBytes(new char[] { 'a' })[0];
  76.             byte asciiMax = Encoding.ASCII.GetBytes(new char[] { 'z' })[0];
  77.             return CaesarCypher(ref asciiValue, asciiMin, asciiMax);
  78.         }
  79.  
  80.         private static char CaesarCypherUpper(char letter)
  81.         {
  82.             byte asciiValue = Encoding.ASCII.GetBytes(new char[] { letter })[0];
  83.             byte asciiMin = Encoding.ASCII.GetBytes(new char[] { 'A' })[0];
  84.             byte asciiMax = Encoding.ASCII.GetBytes(new char[] { 'Z' })[0];
  85.             return CaesarCypher(ref asciiValue, asciiMin, asciiMax);
  86.         }
  87.  
  88.         private static char CaesarCypherOther(char letter)
  89.         {
  90.             byte asciiValue = Encoding.ASCII.GetBytes(new char[] { letter })[0];
  91.             byte asciiMin = Encoding.ASCII.GetBytes(new char[] { '!' })[0];
  92.             byte asciiMax = Encoding.ASCII.GetBytes(new char[] { '@' })[0];
  93.             //is encryptable symbol?
  94.             if (asciiValue < asciiMin || asciiValue > asciiMax)
  95.             {
  96.                 return letter;
  97.             }
  98.             return CaesarCypher(ref asciiValue, asciiMin, asciiMax);
  99.         }
  100.  
  101.         private static char CaesarCypher(ref byte asciiValue, byte asciiMin, byte asciiMax)
  102.         {
  103.             asciiValue += (byte)step;
  104.             //character overflow or underflow correction
  105.             if (asciiValue < asciiMin)
  106.             {
  107.                 asciiValue = (byte)(asciiMax - (asciiMin - asciiValue) + 1);
  108.             }
  109.             if (asciiValue > asciiMax)
  110.             {
  111.                 asciiValue = (byte)(asciiMin + (asciiValue - asciiMax) - 1);
  112.             }
  113.             return Encoding.ASCII.GetChars(new byte[] { asciiValue })[0];
  114.         }
  115.     }
  116. }
Add Comment
Please, Sign In to add comment