代码拉取完成,页面将自动刷新
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Text;
using System.IO;
using System.Windows.Forms;
using Microsoft.CSharp;
namespace SchoolTool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public List<int>[] info;
List<int>[] boydivclass;
List<int>[] girldivclass;
int[] boybelongclass;
int[] girlbelongclass;
Student[] stulist;
int boynum;
int girlnum;
int classnum;
int[] boyclassarray;
int[] girlclassarray;
List<Student>[] stuinclass;
List<Student> stulist1;
public bool isNumber(string str)
{
return System.Text.RegularExpressions.Regex.IsMatch(str, @"^[0-9]*$");
}
private void genDivInfoBtn_Click(object sender, EventArgs e)
{
boynum = Int32.Parse(boyNumBox.Text.ToString());
girlnum = Int32.Parse(girlNumBox.Text.ToString());
if (classNumBox.Text.Equals(""))
{
MessageBox.Show("请输入班级数");
return;
}
if (!isNumber(classNumBox.Text))
{
MessageBox.Show("请输入正确的数字(1-100)");
return;
}
classnum = Int32.Parse(classNumBox.Text.ToString());
if (classnum <= 0 || classnum > 100)
{
MessageBox.Show("请输入合理的班级数值");
classNumBox.Text = "";
return;
}
infoWinTextBox.Text = "";
boyclassarray = new int[classnum];
girlclassarray = new int[classnum];
Random ra = new Random(unchecked((int)DateTime.Now.Ticks));
int boyperclass = boynum / classnum;
int girlperclass = girlnum / classnum;
for (int i = 0; i < classnum; i++)
{
boyclassarray[i] = boyperclass;
}
for (int i = 0; i < boynum - boyperclass * classnum; i++)
{
int tmp = ra.Next(classnum);
while (boyclassarray[tmp] > boyperclass)
{
tmp = ra.Next(classnum);
}
boyclassarray[tmp]++;
}
for (int i = 0; i < classnum; i++)
{
girlclassarray[i] = girlperclass;
}
for (int i = 0; i < girlnum - girlperclass * classnum; i++)
{
int tmp = ra.Next(classnum);
while (girlclassarray[tmp] > girlperclass)
{
tmp = ra.Next(classnum);
}
girlclassarray[tmp]++;
}
int maxcount = 0;
int mincount = 0;
for (int i = 0; i < classnum; i++)
{
int tmp = girlclassarray[i] + boyclassarray[i];
if (tmp == girlperclass + boyperclass + 2)
{
maxcount++;
}
if (tmp == girlperclass + boyperclass)
{
mincount++;
}
}
if (maxcount > 0 && mincount > 0)
{
int mmin = Math.Min(maxcount, mincount);
int addcount = 0;
int subcount = 0;
for (int i = 0; i < classnum; i++)
{
int tmp = girlclassarray[i] + boyclassarray[i];
if (tmp == girlperclass + boyperclass + 2 && subcount < mmin)
{
boyclassarray[i]--;
subcount++;
}
if (tmp == girlperclass + boyperclass && addcount < mmin)
{
boyclassarray[i]++;
addcount++;
}
if (subcount == mmin && addcount == mmin)
break;
}
}
stulist = new Student[boynum + girlnum];
for (int i = 0; i < boynum + girlnum; i++)
{
stulist[i] = new Student();
}
for (int i = 0; i < boynum; i++)
{
stulist[i].num = i;
stulist[i].orinum = boyorinum[i];
stulist[i].oriname = boyoriname[i];
stulist[i].sex = true;
stulist[i].bannum = 0;
}
for (int i = 0; i < girlnum; i++)
{
stulist[i + boynum].num = i + 1;
stulist[i + boynum].orinum = girlorinum[i];
stulist[i + boynum].oriname = girloriname[i];
stulist[i + boynum].sex = false;
stulist[i + boynum].bannum = 0;
}
bool sucflag = false;
for(int ll = 0; ll < 20; ll++)
{
bool ff = maingen();
if (ff)
{
sucflag = true;
break;
}
}
if (sucflag)
{
infoWinTextBox.AppendText("#########################################\r\n");
infoWinTextBox.AppendText("产生分配信息成功,请按相应按钮保存至文件\r\n");
infoWinTextBox.AppendText("#########################################\r\n");
}
else
{
infoWinTextBox.AppendText("#########################################\r\n");
infoWinTextBox.AppendText("产生分配信息失败,请按相应按钮重新分配\r\n");
infoWinTextBox.AppendText("#########################################\r\n");
infoWinListBox.Items.Clear();
classComboBox.Items.Clear();
return;
}
classComboBox.Items.Clear();
for (int i = 0; i < classnum; i++)
{
stuinclass[i].Sort();
classComboBox.Items.Add(i + 1);
}
//for (int i = 0; i < classnum; i++)
//{
// boydivclass[i].Sort();
// girldivclass[i].Sort();
//}
stulist1=new List<Student>();
for (int i = 0; i < classnum; i++)
{
for (int j = 0; j < stuinclass[i].Count; j++)
{
stuinclass[i][j].bannum = i + 1;
stulist1.Add(stuinclass[i][j]);
}
}
stulist1.Sort();
for (int i = 0; i < boynum + girlnum - 1; i++)
{
if (stulist1[i + 1].oriname.Equals(stulist1[i].oriname))
{
List<int> repeat = new List<int>();
repeat.Add(stulist1[i].bannum);
repeat.Add(stulist1[i + 1].bannum);
int j = i + 2;
while (j < boynum + girlnum && stulist1[j].oriname.Equals(stulist1[i].oriname))
{
repeat.Add(stulist1[j].bannum);
j++;
};
int orilen = repeat.Count;
for (int ii = orilen - 1; ii >= 0; --ii)
{
if (repeat.IndexOf(repeat[ii]) != ii)
{
repeat.RemoveAt(ii);
}
}
if (orilen > classnum)
{
string lline = string.Format("{0} 有{1}位重名,大于班级数,由抽屉原理,不可能出现班内无同名现象\r\n但您仍可使用本次分班信息作为参考\r\n", stulist1[i].oriname, orilen);
infoWinTextBox.AppendText(lline);
}
else if (orilen == repeat.Count)
{
string lline = string.Format("{0} 有{1}位重名,均已分至不同班级\r\n", stulist1[i].oriname, orilen);
infoWinTextBox.AppendText(lline);
}
else if (orilen > repeat.Count)
{
string lline = string.Format("{0} 有{1}位重名,没有分至不同班级,建议重新分配!\r\n", stulist1[i].oriname, orilen);
infoWinTextBox.AppendText(lline);
//suc = false;
}
i = j - 1;
}
}
for (int i = 0; i < classnum; i++)
{
string a = String.Format("班级 {0}\t 总人数 {1} 男生 {2} 女生 {3}\n", i + 1, boyclassarray[i] + girlclassarray[i], boyclassarray[i], girlclassarray[i]);
infoWinTextBox.AppendText(a);
}
//for (int i = 0; i < girlnum + boynum; i++)
//{
// string a = string.Format("{0}\r\n", stulist[i].oriname);
// infoWinTextBox.AppendText(a);
//}
label4.Visible = true;
label5.Visible = true;
classComboBox.Visible = true;
classComboBox.SelectedIndex = 0;
fileOutBtn.Visible = true;
}
private bool maingen()
{
Random ra = new Random(unchecked((int)DateTime.Now.Ticks));
bool sucflag = true;
List<Student> final = new List<Student>();
int curboynum = boynum;
int curgirlnum = girlnum;
stuinclass = new List<Student>[classnum];
for (int i = 0; i < stulist.Length; i++)
{
final.Add(stulist[i]);
}
for (int i = 0; i < classnum - 1; i++)
{
stuinclass[i] = new List<Student>();
int tmp;
for (int j = 0; j < boyclassarray[i]; j++)
{
while (true)
{
tmp = ra.Next(curboynum);
bool hasdup = false;
for (int k = 0; k < stuinclass[i].Count; k++)
{
if (stuinclass[i][k].oriname.Equals(final[tmp].oriname))
{
hasdup = true;
break;
}
}
if (!hasdup)
break;
};
stuinclass[i].Add(final[tmp]);
final.RemoveAt(tmp);
curboynum--;
}
}
stuinclass[classnum - 1] = new List<Student>();
for (int j = 0; j < curboynum; j++)
{
Student tt = final[0];
//final.RemoveAt(0);
for (int k = 0; k < stuinclass[classnum - 1].Count; k++)
{
if (stuinclass[classnum - 1][k].oriname.ToString().Equals(tt.oriname.ToString()))
{
sucflag = false;
return sucflag;
}
}
if (sucflag)
{
stuinclass[classnum - 1].Add(tt);
final.RemoveAt(0);
}
else
{
return false;
}
}
for (int i = 0; i < classnum - 1; i++)
{
//stuinclass[i] = new List<Student>();
int tmp;
for (int j = 0; j < girlclassarray[i]; j++)
{
while (true)
{
tmp = ra.Next(curgirlnum);
bool hasdup = false;
for (int k = 0; k < stuinclass[i].Count; k++)
{
if (stuinclass[i][k].oriname.Equals(final[tmp].oriname))
{
hasdup = true;
break;
}
}
if (!hasdup)
break;
};
stuinclass[i].Add(final[tmp]);
final.RemoveAt(tmp);
curgirlnum--;
}
}
//stuinclass[classnum - 1] = new List<Student>();
for (int j = 0; j < curgirlnum; j++)
{
Student tt = final[0];
//final.RemoveAt(0);
for (int k = 0; k < stuinclass[classnum - 1].Count; k++)
{
if (stuinclass[classnum - 1][k].oriname.ToString().Equals(tt.oriname.ToString()))
{
sucflag = false;
return sucflag;
}
}
if (sucflag)
{
stuinclass[classnum - 1].Add(tt);
final.RemoveAt(0);
}
else
{
return false;
}
}
return true;
}
private void classComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int num = Int32.Parse(classComboBox.SelectedItem.ToString())-1;
infoWinListBox.Items.Clear();
for (int j = 0; j < stuinclass[num].Count; j++)
{
string a = String.Format("{0}\t{1}\t{2}", j + 1, stuinclass[num][j].oriname, stuinclass[num][j].orinum);
infoWinListBox.Items.Add(a);
}
}
private void Form1_Load(object sender, EventArgs e)
{
label4.Visible = false;
label5.Visible = false;
boyNumBox.Enabled = false;
girlNumBox.Enabled = false;
classComboBox.Visible = false;
genDivInfoBtn.Visible = false;
fileOutBtn.Visible = false;
//fileNameInputTextBox.Enabled = false;
//folderNameOutputTextBox.Enabled = false;
}
private void fileOutBtn_Click(object sender, EventArgs e)
{
fileWriteOutCsv();
fileWriteOutTxt();
}
private void fileWriteOutTxt()
{
string rootdir = folderNameOutputTextBox.Text;
if (!Directory.Exists(rootdir))
{
MessageBox.Show("输出目录不存在");
return;
}
System.IO.FileInfo f;
//infoWinTextBox.Text = "";
String instrutionline;
FileStream fs;
try
{
fs = new FileStream(rootdir + "\\zonghe.txt", FileMode.Create);
}
catch (Exception err)
{
String se = String.Format("创建文件错误,可能的原因是文件名重名且原文件为只读\r\n请换个输出目录");
MessageBox.Show(se);
return;
}
String tt = String.Format("男生总数 {0} 女生总数 {1} 学生总数 {2} 班级数 {3}\r\n", boynum, girlnum, boynum + girlnum, classnum);
byte[] data = new UTF8Encoding().GetBytes(tt);
fs.Write(data, 0, data.Length);
for (int i = 0; i < classnum; i++)
{
String str = String.Format("班级 {0}: 男生 {1} 女生 {2} 总人数 {3} \r\n", i + 1, boyclassarray[i], girlclassarray[i], boyclassarray[i] + girlclassarray[i]);
byte[] data1 = new UTF8Encoding().GetBytes(str);
fs.Write(data1, 0, data1.Length);
}
fs.Flush();
fs.Close();
f = new FileInfo(rootdir + "\\zonghe.txt");
f.Attributes = FileAttributes.ReadOnly;
instrutionline = String.Format("班级分配综合表已保存至:\r\n{0}\r\n", rootdir + "\\zonghe.txt");
infoWinTextBox.AppendText(instrutionline);
FileStream zb;
try
{
zb = new FileStream(rootdir + "\\xiangxi.txt", FileMode.Create);
}
catch (Exception err)
{
String se = String.Format("创建文件错误,可能的原因是文件名重名且原文件为只读\r\n请换个输出目录");
MessageBox.Show(se);
return;
}
String fss = String.Format("班级,报名序号,姓名,性别\r\n");
byte[] data4 = new UTF8Encoding().GetBytes(fss);
zb.Write(data4, 0, data4.Length);
for (int i = 0; i < stulist1.Count; i++)
{
String ffss = String.Format("{0},{1},{2},{3}\r\n", stulist1[i].bannum, stulist1[i].orinum, stulist1[i].oriname, (stulist1[i].sex == true) ? "男" : "女");
byte[] data5 = new UTF8Encoding().GetBytes(ffss);
zb.Write(data5, 0, data5.Length);
}
zb.Flush();
zb.Close();
f = new FileInfo(rootdir + "\\xiangxi.txt");
f.Attributes = FileAttributes.ReadOnly;
instrutionline = String.Format("班级分配详细表已保存至:\r\n{0}\r\n", rootdir + "\\xiangxi.txt");
infoWinTextBox.AppendText(instrutionline);
for (int i = 0; i < classnum; i++)
{
string fn = string.Format("{0}\\class{1}.txt", rootdir, i + 1);
StreamWriter sw;
try
{
sw = new StreamWriter(fn, false, Encoding.GetEncoding("utf-8"));
}
catch (Exception err)
{
String se = String.Format("创建文件错误,可能的原因是文件名重名且原文件为只读\r\n请换个输出目录");
MessageBox.Show(se);
return;
}
String ss = String.Format("班级,报名序号,姓名,性别\r\n");
//byte[] data2 = new UTF8Encoding().GetBytes(ss);
//ff.Write(data2, 0, data2.Length);
sw.Write(ss);
for (int j = 0; j < stuinclass[i].Count; j++)
{
String sss = String.Format("{0},{1},{2},{3}\r\n", stuinclass[i][j].bannum, stuinclass[i][j].orinum, stuinclass[i][j].oriname, stuinclass[i][j].sex == true ? "男" : "女");
//byte[] data3 = new UTF8Encoding().GetBytes(sss);
//ff.Write(data3, 0, data3.Length);
sw.Write(sss);
}
sw.Flush();
sw.Close();
f = new FileInfo(fn);
f.Attributes = FileAttributes.ReadOnly;
instrutionline = String.Format("班级{0}学生名单已保存至:\r\n{1}\r\n", i + 1, fn);
infoWinTextBox.AppendText(instrutionline);
}
}
private void fileWriteOutCsv()
{
string rootdir = folderNameOutputTextBox.Text;
if (!Directory.Exists(rootdir))
{
MessageBox.Show("输出目录不存在");
return;
}
System.IO.FileInfo f;
infoWinTextBox.Text = "";
String instrutionline;
FileStream fs;
try
{
fs = new FileStream(rootdir + "\\zonghe.csv", FileMode.Create);
}
catch (Exception err)
{
String se = String.Format("创建文件错误,可能的原因是文件名重名且原文件为只读\r\n请换个输出目录");
MessageBox.Show(se);
return;
}
String tt = String.Format("男生总数 {0} 女生总数 {1} 学生总数 {2} 班级数 {3}\r\n", boynum, girlnum, boynum + girlnum, classnum);
byte[] data = new UTF8Encoding().GetBytes(tt);
fs.Write(data, 0, data.Length);
for (int i = 0; i < classnum; i++)
{
String str = String.Format("班级 {0}: 男生 {1} 女生 {2} 总人数 {3} \r\n", i + 1, boyclassarray[i], girlclassarray[i], boyclassarray[i] + girlclassarray[i]);
byte[] data1 = new UTF8Encoding().GetBytes(str);
fs.Write(data1, 0, data1.Length);
}
fs.Flush();
fs.Close();
f = new FileInfo(rootdir + "\\zonghe.csv");
f.Attributes = FileAttributes.ReadOnly;
instrutionline = String.Format("班级分配综合表已保存至:\r\n{0}\r\n", rootdir + "\\zonghe.csv");
infoWinTextBox.AppendText(instrutionline);
FileStream zb;
try
{
zb = new FileStream(rootdir + "\\xiangxi.csv", FileMode.Create);
}
catch (Exception err)
{
String se = String.Format("创建文件错误,可能的原因是文件名重名且原文件为只读\r\n请换个输出目录");
MessageBox.Show(se);
return;
}
String fss = String.Format("班级,报名序号,姓名,性别\r\n");
byte[] data4 = new UTF8Encoding().GetBytes(fss);
zb.Write(data4, 0, data4.Length);
for (int i = 0; i < stulist1.Count; i++)
{
String ffss = String.Format("{0},{1},{2},{3}\r\n", stulist1[i].bannum, stulist1[i].orinum, stulist1[i].oriname, (stulist1[i].sex == true) ? "男" : "女");
byte[] data5 = new UTF8Encoding().GetBytes(ffss);
zb.Write(data5, 0, data5.Length);
}
zb.Flush();
zb.Close();
f = new FileInfo(rootdir + "\\xiangxi.csv");
f.Attributes = FileAttributes.ReadOnly;
instrutionline = String.Format("班级分配详细表已保存至:\r\n{0}\r\n", rootdir + "\\xiangxi.csv");
infoWinTextBox.AppendText(instrutionline);
for (int i = 0; i < classnum; i++)
{
string fn = string.Format("{0}\\class{1}.csv", rootdir, i + 1);
StreamWriter sw;
try
{
sw = new StreamWriter(fn, false, Encoding.GetEncoding("utf-8"));
}
catch (Exception err)
{
String se = String.Format("创建文件错误,可能的原因是文件名重名且原文件为只读\r\n请换个输出目录");
MessageBox.Show(se);
return;
}
String ss = String.Format("班级,报名序号,姓名,性别\r\n");
//byte[] data2 = new UTF8Encoding().GetBytes(ss);
//ff.Write(data2, 0, data2.Length);
sw.Write(ss);
for (int j = 0; j < stuinclass[i].Count; j++)
{
String sss = String.Format("{0},{1},{2},{3}\r\n", stuinclass[i][j].bannum, stuinclass[i][j].orinum, stuinclass[i][j].oriname, stuinclass[i][j].sex == true ? "男" : "女");
//byte[] data3 = new UTF8Encoding().GetBytes(sss);
//ff.Write(data3, 0, data3.Length);
sw.Write(sss);
}
sw.Flush();
sw.Close();
f = new FileInfo(fn);
f.Attributes = FileAttributes.ReadOnly;
instrutionline = String.Format("班级{0}学生名单已保存至:\r\n{1}\r\n", i + 1, fn);
infoWinTextBox.AppendText(instrutionline);
}
}
List<string> boyorinum;
List<string> girlorinum;
List<string> boyoriname;
List<string> girloriname;
private void readFileBtn_Click(object sender, EventArgs e)
{
string filename = fileNameInputTextBox.Text;
infoWinTextBox.Text = "";
if (!File.Exists(filename))
{
MessageBox.Show("输入文件不存在");
return;
}
infoWinTextBox.AppendText("正在打开文件...\r\n");
DataSet myset = LoadDataFromExcel(filename);
boyorinum = new List<string>();
boyoriname = new List<string>();
girlorinum = new List<string>();
girloriname = new List<string>();
for (int i = 0; i < myset.Tables[0].Rows.Count; i++ )
{
var row = myset.Tables[0].Rows[i];
var s0 = row[0].ToString();
var s1 = row[1].ToString();
var s2 = row[2].ToString();
s1 = s1.Replace(" ", "");
if (s2.Trim().Equals("男"))
{
boyorinum.Add(s0);
boyoriname.Add(s1);
}
if (s2.Trim().Equals("女"))
{
girlorinum.Add(s0);
girloriname.Add(s1);
}
String aa = String.Format("{0}\t{1}\t{2}", s0, s1, s2);
infoWinListBox.Items.Add(aa);
}
boynum = boyorinum.Count;
girlnum = girlorinum.Count;
boyNumBox.Text = boynum.ToString();
girlNumBox.Text = girlnum.ToString();
genDivInfoBtn.Visible = true;
infoWinTextBox.AppendText("文件已读入\r\n");
string infoline = string.Format("男生数\t{0}\r\n女生数\t{1}\r\n总人数\t{2}\r\n", boynum, girlnum, boynum + girlnum);
infoWinTextBox.AppendText(infoline);
}
private void chooseInputFileBtn_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "Excel文件(*.xls)|*.xls";
ofd.ShowDialog();
fileNameInputTextBox.Text = ofd.FileName.ToString();
}
private void chooseOutputFolderBtn_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "请选择一个输出文件夹";
fbd.ShowDialog();
folderNameOutputTextBox.Text = fbd.SelectedPath;
}
public static DataSet LoadDataFromExcel(string filePath)
{
try
{
string strConn;
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";
OleDbConnection OleConn = new OleDbConnection(strConn);
OleConn.Open();
String sql = "SELECT * FROM [Sheet1$]";//可是更改Sheet名称,比如sheet2,等等
OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
DataSet OleDsExcle = new DataSet();
OleDaExcel.Fill(OleDsExcle, "Sheet1");
OleConn.Close();
return OleDsExcle;
}
catch (Exception err)
{
MessageBox.Show("数据绑定Excel失败!失败原因:" + err.Message, "提示信息",
MessageBoxButtons.OK, MessageBoxIcon.Information);
return null;
}
}
private void label4_Click(object sender, EventArgs e)
{
}
private void label3_Click(object sender, EventArgs e)
{
}
//private void button1_Click(object sender, EventArgs e)
//{
//}
}
public class CodeFormat
{
/// <summary>
/// 根据文件名称判断文件的编码格式
/// </summary>
/// <param name="FILE_NAME"></param>
/// <returns></returns>
public static System.Text.Encoding GetType(string FILE_NAME)
{
FileStream fs = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read);
System.Text.Encoding r = GetType(fs);
fs.Close();
return r;
}
/// <summary>
/// 根据文件流判断文件编码格式
/// </summary>
/// <param name="fs"></param>
/// <returns></returns>
public static System.Text.Encoding GetType(FileStream fs)
{
BinaryReader r = new BinaryReader(fs, System.Text.Encoding.Default);
byte[] ss = r.ReadBytes((int)fs.Length);
r.Close();
if (ss == null || ss.Length <= 0)
return null;
//Encoding Representation
//UTF-8 EF BB BF
//UTF-16 Big Endian FE FF
//UTF-16 Little Endian FF FE
//UTF-32 Big Endian 00 00 FE FF
//UTF-32 Little Endian FF FE 00 00
//编码类型 Coding=编码类型.ASCII;
if (ss.Length >= 3 &&
ss[0] == 0xEF && ss[1] == 0xBB && ss[2] == 0xBF)
{//UTF-8 EF BB BF
return System.Text.Encoding.UTF8;
}
else if (ss.Length >= 2 &&
ss[0] == 0xFE && ss[1] == 0xFF)
{//UTF-16 Big Endian FE FF
return System.Text.Encoding.BigEndianUnicode;
}
else if (ss.Length >= 2 &&
ss[0] == 0xFF && ss[1] == 0xFE)
{//UTF-16 Little Endian FF FE
if (ss.Length >= 4 && ss[2] == 0x00 && ss[3] == 0x00)
{//UTF-32 Little Endian FF FE 00 00
return System.Text.Encoding.UTF32;
}
else
{//UTF-16 Little Endian FF FE
return System.Text.Encoding.Unicode;
}
}
//else if (ss.Length >= 4 &&
// ss[0] == 0x00 && ss[1] == 0x00 && ss[2] == 0xFE && ss[3] == 0xFF)
//{//UTF-32 Big Endian 00 00 FE FF
// return System.Text.Encoding.UTF32; //没有找到?
//}
else
{//无BOM和其他编码
return GetNoBomType(ss, ss.Length);
}
}
/// <summary>
/// 针对无BOM的内容做判断,不是分准确--主要是有可能没分析所有的字节
/// </summary>
/// <param name="buf"></param>
/// <param name="len"></param>
/// <returns></returns>
public static System.Text.Encoding GetNoBomType(byte[] buf, int len)
{
//UNICODE UTF-8
//00000000 - 0000007F 0xxxxxxx
//00000080 - 000007FF 110xxxxx 10xxxxxx
//00000800 - 0000FFFF 1110xxxx 10xxxxxx 10xxxxxx
//00010000 - 001FFFFF 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
//00200000 - 03FFFFFF 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
//04000000 - 7FFFFFFF 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
for (int i = 0; i < len; i++)
{
////////////////////////////////////////////////
//另一种判断方法,只判断2个或3个字节的utf8,只要找到一个符合的就返回了
//0xf0 1111 0000
//0xe0 1110 0000
//0xc0 1100 0000
//0x80 1000 0000
if (buf[i] < 0x80)
{//0xxxxxxx
//ascii字符 或 一个字节的utf8
}
else if (buf.Length >= i + 2 && //有后续的字节
(buf[i] & 0xE0) == 0xC0 //110xxxxx
&& (buf[i + 1] & 0xC0) == 0x80) //10xxxxxx
{//110xxxxx 10xxxxxx
return System.Text.Encoding.UTF8; //2个字节的utf8
}
else if (buf.Length >= i + 3 && //有后续的字节
(buf[i] & 0xF0) == 0xE0 //1110xxxx
&& (buf[i + 1] & 0xC0) == 0x80 //10xxxxxx
&& (buf[i + 2] & 0xC0) == 0x80) //10xxxxxx
{//1110xxxx 10xxxxxx 10xxxxxx
return System.Text.Encoding.UTF8; //3个字节的utf8
}
else if (buf.Length >= i + 4 && //有后续的字节
(buf[i] & 0xF8) == 0xF0 //11110xxx
&& (buf[i + 1] & 0xC0) == 0x80 //10xxxxxx
&& (buf[i + 2] & 0xC0) == 0x80 //10xxxxxx
&& (buf[i + 3] & 0xC0) == 0x80) //10xxxxxx
{//11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
return System.Text.Encoding.UTF8; //4个字节的utf8
}
else if (buf.Length >= i + 5 && //有后续的字节
(buf[i] & 0xFC) == 0xF8 //111110xx
&& (buf[i + 1] & 0xC0) == 0x80 //10xxxxxx
&& (buf[i + 2] & 0xC0) == 0x80 //10xxxxxx
&& (buf[i + 3] & 0xC0) == 0x80 //10xxxxxx
&& (buf[i + 4] & 0xC0) == 0x80) //10xxxxxx
{//111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
return System.Text.Encoding.UTF8; //5个字节的utf8
}
else if (buf.Length >= i + 6 && //有后续的字节
(buf[i] & 0xFE) == 0xFC //1111110x
&& (buf[i + 1] & 0xC0) == 0x80 //10xxxxxx
&& (buf[i + 2] & 0xC0) == 0x80 //10xxxxxx
&& (buf[i + 3] & 0xC0) == 0x80 //10xxxxxx
&& (buf[i + 4] & 0xC0) == 0x80 //10xxxxxx
&& (buf[i + 5] & 0xC0) == 0x80) //10xxxxxx
{//1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
return System.Text.Encoding.UTF8; //6个字节的utf8
}
else
return System.Text.Encoding.Default;
////////////////////////////////////////////////
}
return System.Text.Encoding.Default;
}
}
public class Student : IComparable
{
public int num;
public string orinum;
public string oriname;
public bool sex; //boy: true, girl: false
public int bannum;
public int CompareTo(object obj)
{
Student p = obj as Student;
return this.oriname.CompareTo(p.oriname);
}
};
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。