詞典翻譯  英譯漢

nsjlive發表於2018-07-21

詞典翻譯 

英譯漢

  public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Dictionary<string, string> dic = new Dictionary<string, string>();
        private void Form1_Load(object sender, EventArgs e)
        {

          
            //窗體載入事件 
            //讀取檔案
            string[] lines = File.ReadAllLines("English Chinese Dictionary.txt", Encoding.Default);

            //遍歷每一行,把空格都切掉
            for(int i = 0; i < lines.Length; i++)
            {
                string[] words = lines[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

                string chinese = "";
                for(int j = 1; j < words.Length; j++)
                {
                    chinese += words[j];//把當前這一行的所有中文意思累加

                }
                if(!dic.ContainsKey(words[0]))//通過Key查詢元素
                {
                    dic.Add(words[0],chinese);
                }
                else
                {
                    dic[words[0]] += chinese;
                }
            }
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            //翻譯的按鈕
            //判斷使用者在文字框中輸入的單詞在字典中是否存在
            string s = TextBox1.Text.ToLower();
            if (dic.ContainsKey(s))
            {
                TextBox2.Text = dic[s];
            }
            else
            {
                TextBox2.Text = "該單詞沒有收錄";

            }
        }
    }

 

相關文章