步步为营-12-Dictionary-翻译

说明:https://pan.baidu.com/s/1nvPqhDJ所需文件在此目录下对应的位置

1 先做一个简单的英汉翻译词典.先搭UI页面

 

2 将百度网盘中提供的资料放置到bin\debug目录下

3 编写代码

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 英汉翻译
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Dictionary<string, string> dict = new Dictionary<string, string>();
        private void Form1_Load(object sender, EventArgs e)
        {
            //在页面加载后完成以下几件是事情
            //1 获取txt字典内容
            string[] dictArry = File.ReadAllLines("english.txt",Encoding.Default);
            //2 将数组转化为字典           
            foreach (var item in dictArry)
            {
                int spindex = item.IndexOf(" ");
                string english = item.Substring(0,spindex);
                string chinese = item.Substring(spindex);
                if (dict.Keys.Contains(english))
                {
                    dict[english] += chinese;
                    continue;
                }
                dict.Add(english, chinese);
            }

        }

        private void btnTranslate_Click(object sender, EventArgs e)
        {
            string english = txtEnglish.Text.Trim();
            //查找字典

            if (dict.ContainsKey(english))
            {
               txtChinese.Text = dict[english];
            }
            else
            {
                txtChinese.Text = "请下载最新版本的英文词典!!!";
            }
        }
    }
}
View Code

4 运行效果

 

posted @ 2017-04-15 22:17  逍遥小天狼  阅读(215)  评论(0编辑  收藏  举报