Как упростить программу? - C#

Узнай цену своей работы

Формулировка задачи:

помогите с кодом пожалуйста. имеется повторяющийся код. нужно как то его упростить, я пока учусь и мало чего знаю. предполагаю что это делается через функцию. помогите, заранее спасибо
Листинг программы
  1. public void button3_Click(object sender, EventArgs e)
  2. {
  3. string word = richTextBox1.Text;
  4. if (comboBox1.Text == "english")
  5. {
  6. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  7. string path = "ru-en.txt";
  8. StreamReader sr = new StreamReader(path, Encoding.Default);
  9. string[] split;
  10. while (!sr.EndOfStream)
  11. {
  12. split = sr.ReadLine().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  13. if (!dictionary.ContainsKey(split[0]))
  14. {
  15. dictionary.Add(split[0], split[1]);
  16. }
  17. }
  18. sr.Close();
  19. sr.Dispose();
  20. richTextBox2.Text = dictionary[word];
  21. }
  22. if (comboBox1.Text == "italian")
  23. {
  24. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  25. string path = "ru-itl.txt";
  26. StreamReader sr = new StreamReader(path, Encoding.Default);
  27. string[] split;
  28. while (!sr.EndOfStream)
  29. {
  30. split = sr.ReadLine().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  31. if (!dictionary.ContainsKey(split[0]))
  32. {
  33. dictionary.Add(split[0], split[1]);
  34. }
  35. }
  36. sr.Close();
  37. sr.Dispose();
  38. richTextBox2.Text = dictionary[word];
  39. }
  40. if (comboBox1.Text == "french")
  41. {
  42. Dictionary<string, string> dictionary = new Dictionary<string, string>();
  43. string path = "ru-fr.txt";
  44. StreamReader sr = new StreamReader(path, Encoding.Default);
  45. string[] split;
  46. while (!sr.EndOfStream)
  47. {
  48. split = sr.ReadLine().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  49. if (!dictionary.ContainsKey(split[0]))
  50. {
  51. dictionary.Add(split[0], split[1]);
  52. }
  53. }
  54. sr.Close();
  55. sr.Dispose();
  56. richTextBox2.Text = dictionary[word];
  57. }
  58. }

Решение задачи: «Как упростить программу?»

textual
Листинг программы
  1. public void button3_Click(object sender, EventArgs e)
  2. {
  3.     string word = richTextBox1.Text, path;
  4.     if (comboBox1.Text == "english") path = "ru-en.txt";
  5.     else if (comboBox1.Text == "italian") path = "ru-itl.txt";
  6.     else if (comboBox1.Text == "french") path = "ru-fr.txt";
  7.     else return;
  8.     Dictionary<string, string> dictionary = new Dictionary<string, string>();
  9.     using (StreamReader sr = new StreamReader(path, Encoding.Default))
  10.     {
  11.         string[] split;
  12.         while (!sr.EndOfStream)
  13.         {
  14.             split = sr.ReadLine().Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
  15.             if (!dictionary.ContainsKey(split[0]))
  16.             {
  17.                 dictionary.Add(split[0], split[1]);
  18.             }
  19.         }
  20.     }
  21.     richTextBox2.Text = dictionary[word];
  22. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4.125 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы