词典翻译  英译汉

词典翻译 

英译汉

  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 = "该单词没有收录";

            }
        }
    }

 

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页