Guest User

Morse Code

a guest
Apr 8th, 2015
483
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.09 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Threading;
  5.  
  6. class Program
  7. {
  8.     static void Main()
  9.     {
  10.         Console.Write("Please, enter a text: ");
  11.         string input = Console.ReadLine().ToUpper();
  12.         string traslated = ToMorse(input);
  13.  
  14.         Play(traslated);
  15.     }
  16.  
  17.     private static readonly Dictionary<char, string> MorseCode = new Dictionary<char, string>
  18.     {
  19.         {'A', " . - "},
  20.         {'B', " - . . . "},
  21.         {'C', " - . - . "},
  22.         {'D', " - . . "},
  23.         {'E', " . "},
  24.         {'F', " . . - . "},
  25.         {'G', " - - . "},
  26.         {'H', " . . . . "},
  27.         {'I', " . . "},
  28.         {'J', " . - - - "},
  29.         {'K', " - . - "},
  30.         {'L', " . - . . "},
  31.         {'M', " - - "},
  32.         {'N', " - . "},
  33.         {'O', " - - - "},
  34.         {'P', " . - - . "},
  35.         {'Q', " - - . - "},
  36.         {'R', " . - . "},
  37.         {'S', " . . . "},
  38.         {'T', " - "},
  39.         {'U', " . . - - "},
  40.         {'V', " . . . - "},
  41.         {'W', " . - - "},
  42.         {'X', " - . . - "},
  43.         {'Y', " - . - - "},
  44.         {'Z', " - - . . "},
  45.         {' ', "/"}
  46.     };
  47.  
  48.     public static string ToMorse(string text)
  49.     {
  50.         StringBuilder result = new StringBuilder();
  51.         foreach (char c in text)
  52.         {
  53.             result.Append(MorseCode[c]);
  54.         }
  55.         return result.ToString();
  56.     }
  57.     // Play the Morse Code
  58.     protected static void Play(string text)
  59.     {
  60.         foreach (char c in text)
  61.         {
  62.             Console.Write(c);
  63.             if (MorseTones[c].NoteTone == Tone.REST)
  64.             {
  65.                 Thread.Sleep((int)MorseTones[c].NoteDuration);
  66.             }
  67.             else
  68.             {
  69.                 Console.Beep((int)MorseTones[c].NoteTone, (int)MorseTones[c].NoteDuration);
  70.             }
  71.         }
  72.         Console.WriteLine();
  73.     }
  74.  
  75.     protected enum Tone
  76.     {
  77.         REST = 0,
  78.         A = 220,
  79.     }
  80.  
  81.     // Define the duration of a note in units of milliseconds.
  82.     protected enum Duration
  83.     {
  84.         WHOLE = 1600,
  85.         HALF = WHOLE / 2,
  86.         QUARTER = HALF / 2,
  87.         EIGHTH = QUARTER / 2,
  88.         SIXTEENTH = EIGHTH / 2,
  89.     }
  90.  
  91.     // Define a note as a frequency (tone) and the amount of  
  92.     // time (duration) the note plays.
  93.     protected struct Note
  94.     {
  95.         Tone toneVal;
  96.         Duration durVal;
  97.  
  98.         // Define a constructor to create a specific note.
  99.         public Note(Tone frequency, Duration time)
  100.         {
  101.             toneVal = frequency;
  102.             durVal = time;
  103.         }
  104.  
  105.         // Define properties to return the note's tone and duration.
  106.         public Tone NoteTone { get { return toneVal; } }
  107.         public Duration NoteDuration { get { return durVal; } }
  108.     }
  109.  
  110.     protected static readonly Dictionary<char, Note> MorseTones = new Dictionary<char, Note>
  111.     {
  112.         {'-', new Note(Tone.A, Duration.HALF)},
  113.         {'.', new Note(Tone.A, Duration.QUARTER)},
  114.         {' ', new Note(Tone.REST, Duration.QUARTER)},
  115.         {'/', new Note(Tone.REST, Duration.HALF)}
  116.     };
  117. }
Advertisement
Add Comment
Please, Sign In to add comment