实例介绍
【实例简介】C#版字典生成器
【实例截图】
【核心代码】
【实例截图】

【核心代码】
/*
* 由SharpDevelop创建。
* 用户: 以夕阳落款
* 日期: 2017/4/21
* 时间: 14:37
*
* 要改变这种模板请点击 工具|选项|代码编写|编辑标准头文件
*/
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace 字典生成
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
private List<string> info;
private List<string> result;
// 保存文本文件里面的内容
private List<string> textFile;
public MainForm()
{
InitializeComponent();
}
public void Button1Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter="txt文件(*.txt)|*.txt";
if(DialogResult.OK == saveFileDialog.ShowDialog()){
string Name = saveFileDialog.FileName;
//MessageBox.Show(Name);
StreamWriter sw = new StreamWriter(Name);
for(int i = 0; i < result.Count; i )
sw.WriteLine(result[i]);
sw.Close();
}
}
private void show(){
textBox17.Text = "";
for(int i = 0; i < result.Count; i ){
textBox17.AppendText(result[i] "\r\n");
}
}
public void Button2Click(object sender, EventArgs e)
{
info = new List<string>();
foreach (Control ctl in this.groupBox1.Controls){
if (ctl is TextBox){
string s = ((TextBox)ctl).Text;
if(s != "") info.Add(s);
}
}
if(!textBox17.Visible){
textBox17.Location = new Point(288, 12);
textBox17.Visible = true;
textBox17.BringToFront();
button2.Text = "关闭预览";
button1.Enabled = true;
// 去重
result = getResult(info).Distinct().ToList();
show();
}
else{
textBox17.Visible = false;
button2.Text = "直接预览";
}
}
private List<string> getResult(List<string> list){
List<string> listall = new List<string>();
for(int i = 0; i < list.Count; i ){
for(int j = 0; j < list.Count; j ){
if(i != j) listall.Add(info[i] info[j]);
}
}
for(int i = 0; i < info.Count; i ){
listall.Add(info[i]);
}
if(checkBox1.Checked){
listall.AddRange(getAlways());
}
if(checkBox6.Checked){
try{
listall.AddRange(textFile);
}
catch{}
}
if(checkBox2.Checked) listall = removenum(listall);
if(checkBox3.Checked) listall = removestring(listall);
if(checkBox4.Checked){
try{
int n = Int32.Parse(textBox27.Text);
listall = removetooshort(listall, n);
}
catch{
}
}
if(checkBox5.Checked){
try{
int n = Int32.Parse(textBox28.Text);
listall = removetoolong(listall, n);
}
catch{
}
}
return listall;
}
private List<string> removenum(List<string> source){
List<string> temp = new List<string>();
for(int i = 0; i < source.Count; i ){
if(!Regex.IsMatch(source[i], @"^\d $")) temp.Add(source[i]);
}
return temp;
}
private List<string> removestring(List<string> source){
List<string> temp = new List<string>();
for(int i = 0; i < source.Count; i ){
if(!Regex.IsMatch(source[i], @"^[a-zA-Z] $")) temp.Add(source[i]);
}
return temp;
}
private List<string> removetoolong(List<string> source, int n){
List<string> temp = new List<string>();
for(int i = 0; i < source.Count; i ){
if(source[i].Length <= n) temp.Add(source[i]);
}
return temp;
}
private List<string> removetooshort(List<string> source, int n){
List<string> temp = new List<string>();
for(int i = 0; i < source.Count; i ){
if(source[i].Length >= n) temp.Add(source[i]);
}
return temp;
}
private List<string> getAlways(){
List<string> temp = new List<string>();
List<string> strs = new List<string>();
List<string> nums = new List<string>();
// 这个地方可以使左右组合,把这个取消注释就可以了
// foreach (Control ctl in this.groupBox1.Controls){
// if (ctl is TextBox){
// string s = ((TextBox)ctl).Text;
// if(Regex.IsMatch(s, @"[\d] ")) nums.Add(s);
// }
// }
foreach (Control ctl in this.panel1.Controls){
if (ctl is TextBox){
string s = ((TextBox)ctl).Text;
if(Regex.IsMatch(s, @"^\w \d $") || Regex.IsMatch(s, @"^\d \w $")){
// 分离数字和字符串
string str = Regex.Replace(s, @"\d", "");
string num = Regex.Replace(s, @"[^\d]*", "");
strs.Add(str);
nums.Add(num);
}
}
}
// 字母处理规则-顺序变更大小写
int strcount = strs.Count;
for(int i = 0; i < strcount; i ){
string[] tempstr = getStrings(strs[i]);
for(int j = 0; j < tempstr.Length; j ){
strs.Add(tempstr[j]);
temp.Add(tempstr[j] nums[i]);
temp.Add(nums[i] tempstr[j]);
}
}
// 数组规则-截取变化规则
for(int i = 0; i < strs.Count; i ){
for(int j = 0; j < nums.Count; j ){
temp.Add(strs[i] nums[j]);
temp.Add(nums[j] strs[i]);
}
}
// 字母数字处理规则-字符截取
int numcount = nums.Count;
for(int i = 0; i < numcount; i ){
string[] tempnum = getNums(nums[i]);
for(int j = 0; j < tempnum.Length; j ){
for(int k = numcount; k < strs.Count; k ){
temp.Add(strs[k] tempnum[j]);
temp.Add(tempnum[j] strs[k]);
}
}
}
return temp;
}
// 字符转大写
private string[] getStrings(string str){
char[] ch = str.ToCharArray();
string[] strs = new string[ch.Length];
for(int i = 0; i < ch.Length; i ){
if(ch[i] >= 'A' && ch[i] <= 'Z') strs[i] = str.Substring(0, i) (char)(ch[i] 32) str.Substring(i 1, ch.Length - i - 1);
if(ch[i] >= 'a' && ch[i] <= 'z') strs[i] = str.Substring(0, i) (char)(ch[i] - 32) str.Substring(i 1, ch.Length - i - 1);
}
return strs;
}
//数字截一部分
private string[] getNums(string str){
int len = str.Length;
int num = (int)Math.Pow(2.0, (double)len) - 2;
char[] ch = str.ToCharArray();
string[] nums = new string[num];
for (int i = 1; i < num 1; i ){
string temp = "";
for (int j = 0; j < len; j ){
if((i & (1 << j)) != 0) temp = ch[j];
}
nums[i - 1] = temp;
}
return nums;
}
private void TextBox10Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.Filter = "txt文件(*.txt)|*.txt";
if(open.ShowDialog() == DialogResult.OK){
textBox10.Text = open.FileName;
}
}
private void Button3Click(object sender, EventArgs e)
{
if(!File.Exists(textBox10.Text)){
MessageBox.Show("文件不存在!");
return ;
}
string line = string.Empty;
StreamReader sr = new StreamReader(textBox10.Text);
textFile = new List<string>();
line = sr.ReadLine();
textFile.Add(line);
while(!sr.EndOfStream){
line = sr.ReadLine();
textFile.Add(line);
}
MessageBox.Show("导入完成!");
sr.Close();
}
}
}
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论