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
142 lines
6.1 KiB
C#
142 lines
6.1 KiB
C#
// See https://aka.ms/new-console-template for more information
|
|
|
|
/// ┌─────────────────────────────────────────────────────────────────────────────────────────┐
|
|
/// ETML
|
|
/// Auteur : KGE
|
|
/// Date : 31.08.2026
|
|
/// Description : Algorithme de conversion texte -> Morse. Fonctionne avec les nombres
|
|
/// └─────────────────────────────────────────────────────────────────────────────────────────┘
|
|
|
|
using System;
|
|
using System.Linq;
|
|
using System.Threading.Tasks.Dataflow;
|
|
|
|
class PI114
|
|
{
|
|
static void Main(string[] args)
|
|
{
|
|
Console.WriteLine("┌───────────────────────────────────────────────┐");
|
|
Console.WriteLine("\n cc");
|
|
Console.WriteLine("└───────────────────────────────────────────────┘");
|
|
programeDictionnary();
|
|
}
|
|
|
|
static void programeDictionnary(){
|
|
|
|
Dictionary<string,string> specialCharacterDictionnary = new Dictionary<string, string>();
|
|
specialCharacterDictionnary.Add("é","e");
|
|
specialCharacterDictionnary.Add("è","e");
|
|
specialCharacterDictionnary.Add("ê","e");
|
|
specialCharacterDictionnary.Add("ë","e");
|
|
specialCharacterDictionnary.Add("ì","i");
|
|
specialCharacterDictionnary.Add("í","i");
|
|
specialCharacterDictionnary.Add("î","i");
|
|
specialCharacterDictionnary.Add("ï","i");
|
|
specialCharacterDictionnary.Add("ò","o");
|
|
specialCharacterDictionnary.Add("ó","o");
|
|
specialCharacterDictionnary.Add("ô","o");
|
|
specialCharacterDictionnary.Add("ö","o");
|
|
specialCharacterDictionnary.Add("ù","u");
|
|
specialCharacterDictionnary.Add("ú","u");
|
|
specialCharacterDictionnary.Add("û","u");
|
|
specialCharacterDictionnary.Add("ü","u");
|
|
specialCharacterDictionnary.Add("à","a");
|
|
specialCharacterDictionnary.Add("á","a");
|
|
specialCharacterDictionnary.Add("â","a");
|
|
specialCharacterDictionnary.Add("ä","a");
|
|
specialCharacterDictionnary.Add("ç","c");
|
|
|
|
foreach (var ele in specialCharacterDictionnary)
|
|
{
|
|
Console.WriteLine($"Key: {ele.Key}, Value: {ele.Value}");
|
|
}
|
|
IDictionary<string, string> letterToMorse = programmeLetterToMorseCode();
|
|
askUserSentence(letterToMorse);
|
|
}
|
|
public static IDictionary<string,string> programmeLetterToMorseCode()
|
|
{
|
|
Dictionary<string, string> letterToMorse = new Dictionary<string, string>();
|
|
letterToMorse.Add("a", ".-");
|
|
letterToMorse.Add("b", "-...");
|
|
letterToMorse.Add("c", "-.-.");
|
|
letterToMorse.Add("d", "-..");
|
|
letterToMorse.Add("e", ".");
|
|
letterToMorse.Add("f", "..-.");
|
|
letterToMorse.Add("g", "--.");
|
|
letterToMorse.Add("h", "....");
|
|
letterToMorse.Add("i", "..");
|
|
letterToMorse.Add("j", ".---");
|
|
letterToMorse.Add("k", "-.-");
|
|
letterToMorse.Add("l", ".-..");
|
|
letterToMorse.Add("m", "--");
|
|
letterToMorse.Add("n", "-.");
|
|
letterToMorse.Add("o", "---");
|
|
letterToMorse.Add("p", ".--.");
|
|
letterToMorse.Add("q", "--.-");
|
|
letterToMorse.Add("r", ".-.");
|
|
letterToMorse.Add("s", "...");
|
|
letterToMorse.Add("t", "-");
|
|
letterToMorse.Add("u", "..-");
|
|
letterToMorse.Add("v", "...-");
|
|
letterToMorse.Add("w", ".--");
|
|
letterToMorse.Add("x", "-..-");
|
|
letterToMorse.Add("y", "-.--");
|
|
letterToMorse.Add("z", "--..");
|
|
letterToMorse.Add("1", "-----");
|
|
letterToMorse.Add("2", ".----");
|
|
letterToMorse.Add("3", "..---");
|
|
letterToMorse.Add("4", "...--");
|
|
letterToMorse.Add("5", "....-");
|
|
letterToMorse.Add("6", "-....");
|
|
letterToMorse.Add("7", "--...");
|
|
letterToMorse.Add("8", "---..");
|
|
letterToMorse.Add("9", "----.");
|
|
letterToMorse.Add(" ", "/");
|
|
return letterToMorse;
|
|
}
|
|
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, letterToMorse);
|
|
}
|
|
|
|
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 + " ");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
} |