c# 读取文本中的一行用逗号连接数据,并对其进行排序
2009-07-09 21:28
615 查看
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace px3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{//用来打开文件的按钮
OpenFileDialog o = new OpenFileDialog();
o.Filter = "文本文件(*.txt)|*.txt";
if (o.ShowDialog()== DialogResult.Cancel) return;
this.textBox1.Text = o.FileName;//在文本框中写入打开的文件的文件名
}
private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
button1_Click(sender,e);//双击文本框也能打开文件
}
private void button3_Click(object sender, EventArgs e)
{
dataHandle();
}
private void dataHandle()
{
if (this.textBox1.Text == "")
return;
if (!System.IO.File.Exists(this.textBox1.Text))
{
MessageBox.Show(textBox1.Text + " 文件不存在");
return;
}
string result = "";
using(System.IO.StreamReader sr=new System.IO.StreamReader(textBox1.Text))
{
string s = sr.ReadLine();//读取文本中的一行数据
string []ss = s.Split(",".ToCharArray());//按逗号讲数据分割成一个数组
int[] si = new int[ss.Length];//定义一个新的数组的长度等于分割了的数组长度
for (int k = 0; k < si.Length; k++)//将字符数组转化为整数数组
{
si[k] = Convert.ToInt32(ss[k]);
}
result = px(si);
sr.Close();
}
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(textBox1.Text,true))
{
sw.WriteLine();
sw.WriteLine(result);
sw.Close();
}
}
private string px(int [] si)
{
//冒泡排序!
int zz;
for (int i = 0; i < si.Length-1; i++)
for (int j = 0; j <si.Length-i-1; j++)
{
if (si[j] > si[j + 1])
{
zz = si[j]; si[j] = si[j + 1]; si[j + 1] = zz;
}
}
string r = "";
for (int i = 0; i < si.Length; i++)
{
r = r + si[i].ToString() + ",";
}
r = r.Remove(r.Length - 1, 1);//将最好一个","去掉
return r;
}
}
}
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace px3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{//用来打开文件的按钮
OpenFileDialog o = new OpenFileDialog();
o.Filter = "文本文件(*.txt)|*.txt";
if (o.ShowDialog()== DialogResult.Cancel) return;
this.textBox1.Text = o.FileName;//在文本框中写入打开的文件的文件名
}
private void textBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
button1_Click(sender,e);//双击文本框也能打开文件
}
private void button3_Click(object sender, EventArgs e)
{
dataHandle();
}
private void dataHandle()
{
if (this.textBox1.Text == "")
return;
if (!System.IO.File.Exists(this.textBox1.Text))
{
MessageBox.Show(textBox1.Text + " 文件不存在");
return;
}
string result = "";
using(System.IO.StreamReader sr=new System.IO.StreamReader(textBox1.Text))
{
string s = sr.ReadLine();//读取文本中的一行数据
string []ss = s.Split(",".ToCharArray());//按逗号讲数据分割成一个数组
int[] si = new int[ss.Length];//定义一个新的数组的长度等于分割了的数组长度
for (int k = 0; k < si.Length; k++)//将字符数组转化为整数数组
{
si[k] = Convert.ToInt32(ss[k]);
}
result = px(si);
sr.Close();
}
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(textBox1.Text,true))
{
sw.WriteLine();
sw.WriteLine(result);
sw.Close();
}
}
private string px(int [] si)
{
//冒泡排序!
int zz;
for (int i = 0; i < si.Length-1; i++)
for (int j = 0; j <si.Length-i-1; j++)
{
if (si[j] > si[j + 1])
{
zz = si[j]; si[j] = si[j + 1]; si[j + 1] = zz;
}
}
string r = "";
for (int i = 0; i < si.Length; i++)
{
r = r + si[i].ToString() + ",";
}
r = r.Remove(r.Length - 1, 1);//将最好一个","去掉
return r;
}
}
}
相关文章推荐
- c#连接Redis---(2)Redis存读取数据
- C#删除文本文档的任意一行数据
- [C#]重写文本控件的OnKeyPress和OnTextChanged进行录入数据校验
- 在C#中进行数据纵向不定行转横向列,多条信息成一行,例如员工薪资信息
- 读取文本数据文件的某一行的方法
- C#读取Excel数据动态生成对象并进行序列化
- C# datagridview 实现单元格内容进行回车换行而不是换另一行数据
- c# 解决读取Excel混合文本类型,数据读取失败的解决方法
- 一个实用的从文本文件读取数据进行排序的程序
- C#读取excel数据时,文本格式和数值格式的内容无法正常全部读取
- Matlab 读取dat文件(包含几行多余文本,数据带有数字和字符串,且以逗号分隔)
- C语言从txt文本中读取多行用逗号分隔的数据
- C语言从文本文件中用字符串读取一行数据,然后再依次遍历字符串将字符串中数据分配。
- MATLAB处理txt文本文件---数据格式要有规律性,否则要用编写特定方式进行读取
- C#连接内存数据库redis【1、Redis存读取数据】
- C#控制台基础 file.readalllines一行一行的读取一个txt文本,把txt中的内容放到一个string数组里
- C#窗体中,textbox中数据保存到txt文本中,从文本读取数据到textbox
- 第14周 项目2-3 读取数据后进行排序
- c#操作Access,对MDB数据的table进行操作,包括新增,读取
- C#读取从数据库读取数据判断每一行每一列