Переводчик с английского на русский и обратно - C#
Формулировка задачи:
Помогите, пожалуйста. Требуется разработать приложение - переводчик с английского на русский и обратно. Словарь с переводом должен загружаться из файла. У пользователя 2 действия: перевести англ. текст или перевести рус. текст. Все вводится с консоли.
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace Курсовая
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[] ruText = new string[File.ReadAllLines("ru.txt").Length];
- string[] engText = new string[File.ReadAllLines("eng.txt").Length];
- string n = Console.ReadLine();
- string[] mas1 = n.Split();
- StreamWriter SW= new StreamWriter(new FileStream("VvodRus.txt",FileMode.Create, FileAccess.ReadWrite));
- for (int i = 0; i < mas1.Length; i++)
- {
- SW.WriteLine(mas1[i]);
- }
- SW.Close();
- string[] rus = new string[File.ReadAllLines("VvodRus.txt").Length];
- /* for (int i = 0; i < ruText.Length; i++)
- {
- rus[i] = File.ReadAllLines("VvodRus.txt")[i];
- ruText[i] = File.ReadAllLines("ru.txt")[i];
- if (rus[i].Equals(StringComparison.CurrentCultureIgnoreCase))
- {
- Console.WriteLine(ruText[i]);
- return;
- }
- }*/
- }
- }
- }
Решение задачи: «Переводчик с английского на русский и обратно»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace ConsoleApplication4
- {
- class Program
- {
- static void Main(string[] args)
- {
- Translator enru = new Translator(1);
- string text = "I do not understand";
- Console.WriteLine(text);
- Console.WriteLine(enru.Translate(text));
- Console.WriteLine();
- Translator ruen = new Translator(2);
- string text2 = "я делать не понимать";
- Console.WriteLine(text2);
- Console.WriteLine(ruen.Translate(text2));
- Console.ReadKey();
- }
- }
- class Translator
- {
- Dictionary<string, string> dict=new Dictionary<string,string>();
- public Translator(int direction) //конструктор, в котором заполняется словарь из файла
- {
- string[] dictionary = File.ReadAllLines(@"d:\Dictionary.txt", Encoding.Default);
- foreach (string line in dictionary)
- {
- string[] words = line.Split(' ');
- if (direction==1)
- dict.Add(words[0], words[1]);
- else
- dict.Add(words[1], words[0]);
- }
- }
- public string Translate(string text) //метод перевода
- {
- string[] textArray = text.Split(' ');
- string result = string.Empty;
- foreach (string word in textArray)
- {
- if (dict.ContainsKey(word))
- result += dict[word] + " ";
- else
- result += "<???> "; //если нет в словаре
- }
- return result;
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д