未处理的“System.IO.IOException”类型的异常出现在 mscorlib.dll 中。
/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();
// }
// }
}
}