zoukankan      html  css  js  c++  java
  • 导入英汉文本,用字符串切割,泛型集合存储的英汉字典

     using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    
    namespace 导入英汉文本,用字符串切割,泛型集合存储的英汉字典
    {
        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("1.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]))//判断字典中是否有这个单词,
                   {
                       dic.Add(words[0],chinese);//如果没有这个单词就把该单词和意思加到字典中
                   }
                   else
                   {
                       //该单词在字典中已经存在了,
                       dic[words[0]] += chinese;
                   }
                 
    
                }
            }
    
            private void btnOk_Click(object sender, EventArgs e)
            {
                //从第一个文本框中取出英文单词
                
                //判断这个单词在字典中是否存在,如果存在则显示中文意思
                if (dic.ContainsKey(txtEnglish.Text.ToLower()))
                {
                    txtChinese.Text=dic[txtEnglish.Text.ToLower()];
                }
                else
                {
                    txtChinese.Text = "字典中没有收录该单词";
                }
                
                //如果不存在则提示该单词字典中没有收录
    
            }
        }
    }
  • 相关阅读:
    fenby C语言P21
    fenby C语言 P20
    fenby C语言 P19
    fenby C语言 P18
    fenby C语言 P17
    fenby C语言 P16
    fenby C语言 P15
    fenby C语言 P14
    Python学习之路:subprocess模块和面向对象
    Python学习之路:XML模块
  • 原文地址:https://www.cnblogs.com/blacop/p/6009333.html
Copyright © 2011-2022 走看看