前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C3第 1 节: 泛型集合及练习

C3第 1 节: 泛型集合及练习

作者头像
静心物语313
发布2020-03-24 10:56:41
4640
发布2020-03-24 10:56:41
举报
文章被收录于专栏:静心物语313的Coding
代码语言:javascript
复制
 1 C3第 1 节: 泛型集合及练习
 2 using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace 泛型键值对的练习
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             #region //把123转换为:壹贰叁。Dictionary<char,char>
15             ////把123转换为:壹贰叁。Dictionary<char,char>
16             //string str = "1壹 2贰 3叁 4肆 5伍 6陆 7柒 8捌 9玖 0零";
17             //string[] txts = str.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
18             //Dictionary<int, char> dic = new Dictionary<int, char>();
19             //for (int i = 0; i < txts.Length; i++)
20             //{
21             //    if (!dic.ContainsKey(txts[i][0]))
22             //    {
23             //        dic.Add(txts[i][0],txts[i][1]);//“1壹”   txts[i][0]==1  txts[i][1]=='壹'  
24             //    }                
25             //}
26             ////存储完毕!
27             ////下面就是判断用户的输入了该
28             //Console.WriteLine("请输入一串数字:");
29             //string num = Console.ReadLine();
30             //for (int i = 0; i < num.Length; i++)
31             //{
32             //    if (dic.ContainsKey(num[i]))
33             //    {
34             //        Console.Write(dic[num[i]]);
35             //    }
36             //    else
37             //    {
38             //        Console.Write(num[i]);
39             //    }
40             // }
41             //Console.ReadKey(); 
42             #endregion
43 
44 
45             #region //计算字符串中每种字母出现的次数“Welcome ,to Chinaworld”,不区分大小写。
46             //计算字符串中每种字母出现的次数“Welcome ,to Chinaworld”,不区分大小写。
47             string str = "Welcome ,to Chinaworld";
48             //首先将大小写统一化
49             str = str.ToLower();
50             //建立一个键值对来,来存储字母
51             Dictionary<char, int> dic = new Dictionary<char, int>();
52             for (int i = 0; i < str.Length; i++)
53             {
54                 if (char.IsLetter(str[i]))//如果字符中的字符是字母的话
55                 {
56                     if (!dic.ContainsKey(str[i]))
57                     {
58                         dic.Add(str[i], 1);//数字1表示的是str[i]第一次出现了就赋值为1
59                     }
60                     else
61                     {
62                         dic[str[i]]++;//如果出现的话就将键对应的数值进行加加操作;;;
63                     }
64 
65                 }
66 
67             }
68             foreach (KeyValuePair<char, int> item in dic)//将键值对进行成对的输出,,用KeyValuePair关键字
69             {
70                 Console.WriteLine("字母{0}出现的次数是{1}次", item.Key, item.Value);
71             }
72             Console.ReadKey(); 
73             #endregion
74            
75         }
76     }
77 }

C3第 2 节英汉翻译案例

代码语言:javascript
复制
1 C3第 2 节英汉翻译案例
 2 [code]
 3 using System;
 4 using System.Collections.Generic;
 5 using System.ComponentModel;
 6 using System.Data;
 7 using System.Drawing;
 8 using System.IO;
 9 using System.Linq;
10 using System.Text;
11 using System.Threading.Tasks;
12 using System.Windows.Forms;
13 
14 namespace 英汉词典
15 {
16     public partial class Form1 : Form
17     {
18         public Form1()
19         {
20             InitializeComponent();
21         }
22 
23         Dictionary<string, string> dic = new Dictionary<string, string>();
24         private void Form1_Load(object sender, EventArgs e)
25         {
26             //加载窗体
27             //读取文件english.txt
28             string[] lines = File.ReadAllLines("english.txt", Encoding.Default);
29 
30             for (int i = 0; i < lines.Length; i++)//遍历其中的每一行的字符串并且掉空格
31             {
32                 string[] words = lines[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
33                 //接下来建立一个泛型的键值对存储单词和意思
34 
35                 string chinese = "";
36                 //合并中文的意思;;因为一个单词可能有多个意思
37                 for (int j = 1; j < words.Length; j++)//   china   n 中国 n 陶器
38                 {
39                     chinese += words[j];
40                 }
41                 if (!dic.ContainsKey(words[0]))//??????????????有异常
42                 {
43                     dic.Add(words[0], chinese);
44                 }
45                 else
46                 {
47                     dic[words[0]] += chinese;//将第二个意思也附加上来//dic["小明"]===130000000
48                 }
49             }
50         }
51 
52         private void button1_Click(object sender, EventArgs e)
53         {
54             if (dic.ContainsKey(txtEnglish.Text))
55             {
56                  txtChinese.Text=dic[txtEnglish.Text];
57             }
58             else 
59             {
60                 Console.WriteLine("没有该单词的记录");
61             }
62             Console.ReadKey();
63         }
64         
65 
66     }
67 }
68 [/code]

C3第 3 节: 日期转换案例

代码语言:javascript
复制
1 C3第 3 节: 日期转换案例
 2 [code]using System;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace 日期转换案例
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             
15             Console.WriteLine("请输入日期:");
16             string strDate = Console.ReadLine();
17             string result = getDate(strDate);
18             Console.WriteLine(result);
19             Console.ReadKey();
20         }
21 
22         private static string getDate(string strDate)
23         {
24 
25             string ziDian = "零0 一1 二2 三3 四4 五5 六6 七7 八8 九9";
26             string[] txt = ziDian.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
27             Dictionary<char, char> dic = new Dictionary<char, char>();
28             for (int i = 0; i < txt.Length; i++)
29             {
30                 if (!dic.ContainsKey(txt[i][0]))
31                 {
32                     dic.Add(txt[i][0], txt[i][1]); 
33                 }
34 
35             }
36 
37             //将输入的字符划分为字符数组
38             //二零一二年十二月二十一日   
39             StringBuilder sb = new StringBuilder();
40             char[] chs = strDate.ToCharArray();//字符串转字符数组
41             for (int i = 0; i < chs.Length; i++)
42             {
43                 if (chs[i] == '十')
44                 {
45                     //对字符十进行操作
46                     //try
47                     //{
48 
49                     //}
50                     //catch (Exception)
51                     //{
52                         
53                     //    throw;
54                     //}//如果输入十月二十四日会异常???????????
55 
56 
57                     if (!dic.ContainsKey(chs[i - 1]) && !dic.ContainsKey(chs[i + 1]))//.*月十日
58                     {
59                         sb.Append("10");  //’十’→10
60 
61                     }
62                     else if (!dic.ContainsKey(chs[i - 1]) && dic.ContainsKey(chs[i + 1]))//.*月十三日
63                     {
64 
65                         sb.Append("1");  //’十’→1
66                     }
67                     else if (dic.ContainsKey(chs[i - 1]) && dic.ContainsKey(chs[i + 1]))//.*月二十三日
68                     {
69                         //sb.Append();  //’十’不用翻译
70                     }
71                     else//.*月三十日
72                     {
73                         sb.Append("0");   //’十’→0
74                     }
75                 }
76                 else//没有十       
77                 {
78                     //查看字典中是否包含 这个字符,
79                     if (dic.ContainsKey(chs[i]))
80                     {
81                         sb.Append(dic[chs[i]]);//包含则根据键值对翻译
82                     }
83                     else//
84                     {
85                         sb.Append(chs[i]);//不包含原样输出
86                     }
87                 }
88             }
89             return sb.ToString();
90         }
91 
92     }
93 }
94 [/code] 
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档