Compare commits

..
3 Commits
Author SHA1 Message Date
nocode aa52afa62c feat(morse converter): add loop for repeated conversions and improve user input handling
- Wrap main conversion logic in a while loop to allow multiple conversions
- Extract morse code output and user prompting into new showResultInConsole method
- Add input validation to ensure user enters non-empty text
- Convert user input to lowercase before processing
- Refactor translateUserSentenceToMorse to return the translated string instead of void
- Improve console UI with better formatting and title
- Move beeping and output logic from translation to display method
2026-09-14 09:55:53 +02:00
nocode 27747a3b96 feat: add character verification and refactor user input handling
- Add System.Runtime.InteropServices using statement
- Refactor askUserSentence() to return string[] instead of string
- Move userTextToAnArray() conversion into askUserSentence()
- Add new verifyUserSentence() method to validate and replace special characters
- Add console output in userTextToAnArray() to display array conversion
- Simplify main() flow by removing intermediate variable assignment
2026-09-07 10:36:14 +02:00
nocode 07afbb1486 refactor(morse): restructure program flow and improve separation of concerns
- Split main method logic into separate concerns: dictionary initialization, user input, text conversion, and translation
- Change programeDictionnary() to return dictionary instead of void
- Extract askUserSentence() to return user input string
- Extract userTextToAnArray() to return array instead of calling translation directly
- Remove debug output and dictionary printing
- Adjust Console.Beep durations: 100ms for dots, 400ms for dashes
- Improve method signatures to better reflect their responsibilities
2026-09-07 09:59:11 +02:00
+103 -38
View File
@@ -9,19 +9,31 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks.Dataflow; using System.Threading.Tasks.Dataflow;
class PI114 class PI114
{ {
static void Main(string[] args) static void Main(string[] args)
{ {
Console.WriteLine("┌───────────────────────────────────────────────┐"); bool convertAgain = true;
Console.WriteLine("\n cc"); while (convertAgain)
Console.WriteLine("└───────────────────────────────────────────────┘"); {
programeDictionnary(); Console.Title = "Convertisseur T2M";
Console.WriteLine("\t\t\t\t┌───────────────────────────────────────────────┐");
Console.WriteLine("\t\t\t\t\tConvertisseur de texte en Morse");
Console.WriteLine("\t\t\t\t└───────────────────────────────────────────────┘");
IDictionary<string, string> specialcharacterDictionnary = programeDictionnary();
IDictionary<string, string> letterToMorse = programmeLetterToMorseCode();
string[] userTextArray = askUserSentence();
verifyUserSentence(userTextArray, specialcharacterDictionnary);
string translatedText = translateUserSentenceToMorse(userTextArray, letterToMorse);
showResultInConsole(translatedText, ref convertAgain);
}
} }
static void programeDictionnary(){ static IDictionary<string, string> programeDictionnary(){
Dictionary<string,string> specialCharacterDictionnary = new Dictionary<string, string>(); Dictionary<string,string> specialCharacterDictionnary = new Dictionary<string, string>();
specialCharacterDictionnary.Add("é","e"); specialCharacterDictionnary.Add("é","e");
@@ -45,13 +57,7 @@ class PI114
specialCharacterDictionnary.Add("â","a"); specialCharacterDictionnary.Add("â","a");
specialCharacterDictionnary.Add("ä","a"); specialCharacterDictionnary.Add("ä","a");
specialCharacterDictionnary.Add("ç","c"); specialCharacterDictionnary.Add("ç","c");
return specialCharacterDictionnary;
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() public static IDictionary<string,string> programmeLetterToMorseCode()
{ {
@@ -94,49 +100,108 @@ class PI114
letterToMorse.Add(" ", "/"); letterToMorse.Add(" ", "/");
return letterToMorse; return letterToMorse;
} }
static void askUserSentence(IDictionary<string, string> letterToMorse) static string[] askUserSentence()
{ {
Console.Write("Entrez votre phrase à déchiffrer : "); Console.Write("Entrez votre phrase à déchiffrer (Sans accents, A-Z 0-9): ");
string userText = Console.ReadLine(); string userText = Console.ReadLine();
Console.WriteLine(); while(userText.Length == 0)
Console.WriteLine(userText); {
userTextToAnArray(userText, letterToMorse); Console.Write("[Erreur] ─ Veuillez entrer un texte (Sans accents, A-Z 0-9): ");
userText = Console.ReadLine();
}
string[] userTextArray = userTextToAnArray(userText);
return userTextArray;
} }
static void userTextToAnArray(string userText, IDictionary<string, string> letterToMorse) static string[] userTextToAnArray(string userText)
{ {
string[] userTextArray = userText.Select(x => x.ToString()).ToArray(); string[] userTextArray = userText.Select(x => x.ToString().ToLower()).ToArray();
translateUserSentenceToMorse(userTextArray, letterToMorse); Console.WriteLine(string.Join("", userTextArray));
return userTextArray;
} }
static void translateUserSentenceToMorse(string[] userTextArray, IDictionary<string, string> letterToMorse) static string[] verifyUserSentence(string[] userTextArray, IDictionary<string, string> specialcharacterDictionnary)
{
int i = 0;
foreach( KeyValuePair<string, string> v in specialcharacterDictionnary)
{
foreach( string k in userTextArray )
{
if (v.Key.Equals(k))
{
userTextArray[i] = v.Value;
Console.WriteLine("Character : {0} Modifié en {1}", k, userTextArray[i]);
}
// else if ()
// {
// } à faire character accepté
// else if (!v.Key.Contains(k))
// {
// while (!v.Key.Contains(k))
// {
// Console.WriteLine("Veuillez entrer votre phrase sans charactère spéciale");
// string newText = Console.ReadLine();
// userTextArray = userTextToAnArray(newText);
// }
// continue;
// } à mettre en else
}
i++;
}
return userTextArray;
}
static string translateUserSentenceToMorse(string[] userTextArray, IDictionary<string, string> letterToMorse)
{ {
Console.WriteLine(string.Join(" ", userTextArray)); Console.WriteLine(string.Join(" ", userTextArray));
string translatedText = null;
foreach (string k in userTextArray) foreach (string k in userTextArray)
{ {
foreach(KeyValuePair<string, string> v in letterToMorse) foreach(KeyValuePair<string, string> v in letterToMorse)
{ {
if (k == v.Key) if (k == v.Key)
{ {
if (v.Value.Contains(".")) translatedText += v.Value;
{
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 + " ");
} }
} }
} }
return translatedText;
} }
static bool showResultInConsole(string translatedText, ref bool convertAgain)
{
string[] translatedTextArray = translatedText.Select(x => x.ToString()).ToArray();
foreach (string v in translatedTextArray)
{
if (v.Equals("."))
{
Console.Write(v);
Console.Beep(1000, 100);
}
else if (v.Equals("-"))
{
Console.Write(v);
Console.Beep(1000, 400);
}
else if (v.Equals("/")){
Console.Write(v);
}
}
Console.WriteLine("");
Console.Write("Voulez vous refaire une conversion ? (O/N) : ");
string wannaDoItAgain = Console.ReadLine();
wannaDoItAgain = wannaDoItAgain.ToLower();
if (wannaDoItAgain == "o")
{
Console.Clear();
return convertAgain = true;
}
else
{
Console.Clear();
Console.WriteLine("Merci aurevoir !!");
return convertAgain = false;
}
}
} }