未处理的“System.IO.IOException”类型的异常出现在 mscorlib.dll 中。

ww0427401078 2007-06-10 01:56:43
/Program.cs文件
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;

namespace PinYin
{
/// <summary>
/// haha 的摘要说明。
/// </summary>
public class Program
{
public Program()
{

}


private static string[] S = new string[24]{"0","b","c","ch","d","f","g","h","j","k","l","m","n","p","q","r","s","sh",
"t","w","x","y","z","zh"};

private static string[] Y = new string[36]{"a","ai","an","ang","ao","e","ei","en","eng","er","i","ia","ian","iang","iao",
"ie","in","ing","iong","iu","o","ong","ou","u","ua","uai","uan","uang","ueng","ui","un","uo",
"v","van","ue","vn"};

private static int[,] Index = new int[864, 2];



//=======================================================
// 搜索生母对应的压缩码
// 如果不存在,返回-1
//=======================================================

private static int getSmcode(string Sm)
{
int i;

for (i = 1; i < 24; i++)
{
if (Sm == S[i])
return i;
}
return -1;
}

//========================================================
// 搜索韵母对应的压缩码
// 如果不存在,返回-1
//========================================================

private static int getYmcode(string Ym)
{
int i;
for (i = 0; i < 36; i++)
{
if (Ym == Y[i])
return i;
}
return -1;
}

//============================================================
// 生成拼音的压缩码,参数为拼音字符串
// 返回值为1--864之间的整数
//============================================================

public static int getPYcode(string PY)
{
int Scode, Ycode;

if( PY=="")
return -1;

if (PY.StartsWith("a") || PY.StartsWith("e") || PY.StartsWith("o"))//0声母
{
Scode = 0;
Ycode = getYmcode(PY);
}
else if (PY.Length>1 && PY[1] == 'h')//声母为ch,sh,zh
{
Scode = getSmcode(PY.Substring(0, 2));
Ycode = getYmcode(PY.Substring(2));
}
else
{
Scode = getSmcode(PY.Substring(0, 1));
Ycode = getYmcode(PY.Substring(1));
}
if (-1 == Scode || -1 == Ycode)
return -1;

return Scode * 36 + Ycode;
}

//==============================================================
// 生成索引和码表
// 码表中将所有汉字连续写在一起
// 索引采用二维数组,index[Pycode,0]存放起始偏移,
// index[Pycode,1]存放结束偏移
//==============================================================

public static void genIndex()
{
string pypath = "py.txt";//源文件

string list = "pylist.txt";//码表文件

FileStream fs = new FileStream(list,FileMode.Create, FileAccess.Write, FileShare.None);

StreamWriter ls = new StreamWriter(fs, System.Text.Encoding.Unicode,512);

string buffer = " ";

string py = " ";

int count = 0;

int oldPycode = 0;

int Pycode;

using (StreamReader sr = new StreamReader(pypath, System.Text.Encoding.GetEncoding(936), true))
{
while (sr.Peek() >= 0)
{
buffer = sr.ReadLine();//读取一行

ls.Write(buffer.Substring(0,1));//将汉字写入码表文件

py = buffer.Substring(1);//提取拼音

Pycode = getPYcode(py);

if (Pycode != oldPycode)
{
Index[oldPycode, 1] = count;

oldPycode = Pycode;

Index[Pycode,0] = count;

}
count++;
}
}
ls.Close();
fs.Close();
}


public static string getHz(int PYcode)
{
string pyls = "pylist.txt";

FileStream list = new FileStream(pyls, FileMode.Open, FileAccess.Read, FileShare.None);

StreamReader ls = new StreamReader(list, System.Text.Encoding.Unicode, true);

int n = Index[PYcode, 1] - Index[PYcode, 0];

if (n == 0)
return "error!";

list.Seek((long)Index[PYcode, 0] * 2 + 2, SeekOrigin.Begin);

char[] buf = new char[n];

ls.Read(buf, 0, n);

ls.Close();

list.Close();

string st = new string(buf);

return st;
}

// private static void readIndex()
// {
// if (!File.Exists("Index.txt") || !File.Exists("pylist.txt"))
// {
// genIndex();
// }
// if (Index[1, 0] == 0)
// {
// string index = "Index.txt";
//
// FileStream fIndex = new FileStream(index, FileMode.Open, FileAccess.Read, FileShare.None);
//
// BinaryReader r = new BinaryReader(fIndex);
//
// for (int i = 0; i < 864; i++)
// {
// Index[i, 0] = r.ReadInt32();
//
// Index[i, 1] = r.ReadInt32();
// }
//
// r.Close();
// fIndex.Close();
// }
// }

}
}
...全文
1562 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
ww0427401078 2007-06-10
  • 打赏
  • 举报
回复
//PingYin.cs文件
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;

namespace PinYin
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ListBox listBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.listBox1 = new System.Windows.Forms.ListBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(24, 8);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(280, 152);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(24, 184);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(136, 21);
this.textBox2.TabIndex = 1;
this.textBox2.Text = "";
this.textBox2.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox2_KeyPress);
this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);
//
// label1
//
this.label1.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.Location = new System.Drawing.Point(0, 8);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(24, 48);
this.label1.TabIndex = 2;
this.label1.Text = "汉字";
//
// label2
//
this.label2.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label2.Location = new System.Drawing.Point(0, 176);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(16, 40);
this.label2.TabIndex = 3;
this.label2.Text = "拼音";
//
// listBox1
//
this.listBox1.ImeMode = System.Windows.Forms.ImeMode.Katakana;
this.listBox1.ItemHeight = 12;
this.listBox1.Location = new System.Drawing.Point(232, 176);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(64, 196);
this.listBox1.TabIndex = 4;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(328, 222);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{


Program.genIndex();

listBox1.Hide();

//int PYcode=Program.getPYcode(textBox2.Text);

// if(PYcode != -1)
//textBox1.Text=Program.getHz(PYcode);
}

private void textBox2_TextChanged(object sender, System.EventArgs e)
{

string buffer = " ";

string text="";

buffer = textBox2.Text;

if (buffer.Length == 0)
{
listBox1.Hide();
}
else if (buffer.Length != 0)
{
int pyCode = Program.getPYcode(buffer);

if (pyCode >= 0 && pyCode <= 864)
{
text = Program.getHz(pyCode);

if (text != "error!")
{
int Len = text.Length;
int x = Len / 10;
int y = Len % 10;
string[] str = new string[x + 1];
if (y == 0)
{
for (int i = 0; i < x; i++)
{
str[i] = text.Substring(10 * i, 10);

}
}
else if (y != 0)
{
for (int i = 0; i < x; i++)
{
str[i] = text.Substring(10 * i, 10);
}
str[x] = text.Substring(10 * x, y);
}
listBox1.Items.Clear();

for (int i = 0; i < str[0].Length; i++)
listBox1.Items.Add(i + ". " + str[0][i]);

listBox1.Show();
}
}
}

}

private void textBox2_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{

if (e.KeyChar >= 48 && e.KeyChar <= 59)
{
e.Handled=true;
switch (e.KeyChar)
{
case (char)48: textBox1.Text = listBox1.Items[0].ToString().Substring(3, 1); break;
case (char)49: textBox1.Text = listBox1.Items[1].ToString().Substring(3, 1); break;
case (char)50: textBox1.Text = listBox1.Items[2].ToString().Substring(3, 1); break;
case (char)51: textBox1.Text = listBox1.Items[3].ToString().Substring(3, 1); break;
case (char)52: textBox1.Text = listBox1.Items[4].ToString().Substring(3, 1); break;
case (char)53: textBox1.Text = listBox1.Items[5].ToString().Substring(3, 1); break;
case (char)54: textBox1.Text = listBox1.Items[6].ToString().Substring(3, 1); break;
case (char)55: textBox1.Text = listBox1.Items[7].ToString().Substring(3, 1); break;
case (char)56: textBox1.Text = listBox1.Items[8].ToString().Substring(3, 1); break;
case (char)57: textBox1.Text = listBox1.Items[9].ToString().Substring(3, 1); break;
}


}
else if (e.KeyChar ==(char) Keys.Space)
{

//richTextBox1.AppendText(textBox1.Text);
textBox1.Clear();
listBox1.Hide();
textBox1.Focus();
}


}

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{

}


}


}

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧