集合类List与Dictonary实例练习

2019-05-20 12:58:45王振洲

string date = "二零一二年二十月二十一日" ;
date = newstr(date);
string nullYear = "" ;
//money.Add('年', '-');
//money.Add('月', '-');
for (int i = 0; i < date.Length; i++) {
if (money.ContainsKey(date[i])) {
nullYear += money[date[i]];
} else if (date[i] == '年' || date[i] == '月' ) {
nullYear += '-' ;
}
}
Console .WriteLine(nullYear);
Console .WriteLine(date);//二零一二年一二月二一日
Console .ReadKey();
}
//十左右字符都在字典中 那么十消失 如果左边不在右边在则变1 如果左边在右边不在则变0 如果左右都不在则变10
public static string newstr( string str) {
string newstr = "" ;
for (int i = 0; i < str.Length; i++) {
if (str[i] == '十' ) {
bool left = money.ContainsKey(str[i - 1]);
bool right = money.ContainsKey(str[i + 1]);
if (left && right) {
newstr += "" ;
} else if (right) { //!left &&
newstr += "一" ;
} else if (left) { //&& !right
newstr += "零" ;
} else {//if (!left && !right)
newstr += "一零" ;
}
} else {
newstr += str[i];
}
}
return newstr;
}
}
}

f、泛型集合练习之翻译软件

View Code
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _05_翻译软件 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
Dictionary <string , string> dic = new Dictionary < string, string >();
private void btnChange_Click( object sender, EventArgs e) {
if (dic.ContainsKey(txtEnglish.Text)) {
txtChina.Text = dic[txtEnglish.Text];
} else {
MessageBox .Show("请购买新的字典" );
}
}
private void Form1_Load( object sender, EventArgs e) {
string [] filecon = File .ReadAllLines( "英汉词典TXT格式.txt" , Encoding .Default);//格式遵循abandon v.抛弃,放弃
for (int i = 0; i < filecon.Count(); i++) {
string [] arr = filecon[i].Split(new char[] { ' ' }, StringSplitOptions .RemoveEmptyEntries);
if (!dic.ContainsKey(arr[0])) {
dic.Add(arr[0], arr[1]);
}
}
}
}
}