Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Threading;
- class Program
- {
- static void Main()
- {
- Console.Write("Please, enter a text: ");
- string input = Console.ReadLine().ToUpper();
- string traslated = ToMorse(input);
- Play(traslated);
- }
- private static readonly Dictionary<char, string> MorseCode = new Dictionary<char, string>
- {
- {'A', " . - "},
- {'B', " - . . . "},
- {'C', " - . - . "},
- {'D', " - . . "},
- {'E', " . "},
- {'F', " . . - . "},
- {'G', " - - . "},
- {'H', " . . . . "},
- {'I', " . . "},
- {'J', " . - - - "},
- {'K', " - . - "},
- {'L', " . - . . "},
- {'M', " - - "},
- {'N', " - . "},
- {'O', " - - - "},
- {'P', " . - - . "},
- {'Q', " - - . - "},
- {'R', " . - . "},
- {'S', " . . . "},
- {'T', " - "},
- {'U', " . . - - "},
- {'V', " . . . - "},
- {'W', " . - - "},
- {'X', " - . . - "},
- {'Y', " - . - - "},
- {'Z', " - - . . "},
- {' ', "/"}
- };
- public static string ToMorse(string text)
- {
- StringBuilder result = new StringBuilder();
- foreach (char c in text)
- {
- result.Append(MorseCode[c]);
- }
- return result.ToString();
- }
- // Play the Morse Code
- protected static void Play(string text)
- {
- foreach (char c in text)
- {
- Console.Write(c);
- if (MorseTones[c].NoteTone == Tone.REST)
- {
- Thread.Sleep((int)MorseTones[c].NoteDuration);
- }
- else
- {
- Console.Beep((int)MorseTones[c].NoteTone, (int)MorseTones[c].NoteDuration);
- }
- }
- Console.WriteLine();
- }
- protected enum Tone
- {
- REST = 0,
- A = 220,
- }
- // Define the duration of a note in units of milliseconds.
- protected enum Duration
- {
- WHOLE = 1600,
- HALF = WHOLE / 2,
- QUARTER = HALF / 2,
- EIGHTH = QUARTER / 2,
- SIXTEENTH = EIGHTH / 2,
- }
- // Define a note as a frequency (tone) and the amount of
- // time (duration) the note plays.
- protected struct Note
- {
- Tone toneVal;
- Duration durVal;
- // Define a constructor to create a specific note.
- public Note(Tone frequency, Duration time)
- {
- toneVal = frequency;
- durVal = time;
- }
- // Define properties to return the note's tone and duration.
- public Tone NoteTone { get { return toneVal; } }
- public Duration NoteDuration { get { return durVal; } }
- }
- protected static readonly Dictionary<char, Note> MorseTones = new Dictionary<char, Note>
- {
- {'-', new Note(Tone.A, Duration.HALF)},
- {'.', new Note(Tone.A, Duration.QUARTER)},
- {' ', new Note(Tone.REST, Duration.QUARTER)},
- {'/', new Note(Tone.REST, Duration.HALF)}
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment