feat(morse): implement morse code translation with audio output
Refactor morse code generation to return a dictionary for reuse across methods. Add translateUserSentenceToMorse method that converts user input to morse code and plays audio beeps based on dots and dashes. Pass letterToMorse dictionary through the call chain (askUserSentence -> userTextToAnArray -> translateUserSentenceToMorse) to enable translation functionality. Changes: - Make programmeLetterToMorseCode public and return IDictionary - Add morse code translation method with beep audio feedback - Update method signatures to accept morse code dictionary
This commit is contained in:
1 file changed
+39
-5
+39
-5
@@ -9,6 +9,7 @@
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks.Dataflow;
|
||||
|
||||
class PI114
|
||||
{
|
||||
@@ -49,9 +50,10 @@ class PI114
|
||||
{
|
||||
Console.WriteLine($"Key: {ele.Key}, Value: {ele.Value}");
|
||||
}
|
||||
askUserSentence();
|
||||
IDictionary<string, string> letterToMorse = programmeLetterToMorseCode();
|
||||
askUserSentence(letterToMorse);
|
||||
}
|
||||
static void programmeLetterToMorseCode()
|
||||
public static IDictionary<string,string> programmeLetterToMorseCode()
|
||||
{
|
||||
Dictionary<string, string> letterToMorse = new Dictionary<string, string>();
|
||||
letterToMorse.Add("a", ".-");
|
||||
@@ -90,19 +92,51 @@ class PI114
|
||||
letterToMorse.Add("8", "---..");
|
||||
letterToMorse.Add("9", "----.");
|
||||
letterToMorse.Add(" ", "/");
|
||||
return letterToMorse;
|
||||
}
|
||||
static void askUserSentence()
|
||||
static void askUserSentence(IDictionary<string, string> letterToMorse)
|
||||
{
|
||||
Console.Write("Entrez votre phrase à déchiffrer : ");
|
||||
string userText = Console.ReadLine();
|
||||
Console.WriteLine();
|
||||
Console.WriteLine(userText);
|
||||
userTextToAnArray(userText);
|
||||
userTextToAnArray(userText, letterToMorse);
|
||||
}
|
||||
|
||||
static void userTextToAnArray(string userText)
|
||||
static void userTextToAnArray(string userText, IDictionary<string, string> letterToMorse)
|
||||
{
|
||||
string[] userTextArray = userText.Select(x => x.ToString()).ToArray();
|
||||
translateUserSentenceToMorse(userTextArray, letterToMorse);
|
||||
}
|
||||
static void translateUserSentenceToMorse(string[] userTextArray, IDictionary<string, string> letterToMorse)
|
||||
{
|
||||
Console.WriteLine(string.Join(" ", userTextArray));
|
||||
foreach (string k in userTextArray)
|
||||
{
|
||||
foreach(KeyValuePair<string, string> v in letterToMorse)
|
||||
{
|
||||
if (k == v.Key)
|
||||
{
|
||||
if (v.Value.Contains("."))
|
||||
{
|
||||
var resultCount = v.Value.Count(x => x.ToString() == ".");
|
||||
for (int i = 0 ; i < resultCount; i++)
|
||||
{
|
||||
Console.Beep(1000, 200);
|
||||
}
|
||||
}
|
||||
else if (v.Value.Contains("-"))
|
||||
{
|
||||
var resultCount = v.Value.Count(x => x.ToString() == "-");
|
||||
for (int i = 0 ; i < resultCount; i++)
|
||||
{
|
||||
Console.Beep(1000, 200);
|
||||
}
|
||||
}
|
||||
Console.Write(v.Value + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user