本帖最后由 wanghuailin1030 于 2013-6-26 11:39 编辑
把以下IP存入一个txt文件,编写程序把这些IP按数值大小,从小到达排序并打印出来。
61.54.231.245
61.54.231.9
61.54.231.246
61.54.231.48
61.53.231.249
参考大家的意见,这是最后的源码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Collections;
namespace temp
{
class Program
{
//把一下IP地址存入一个TXT文件,编写程序把这些IP按数值大小,从小到大排序并打印出来IP地址排序文件操作
//61.52.231.245
//60.54.231.9
//61.54.234.246
//61.54.231.48
//60.53.231.249
static void Main(string[] args)
{
FileStream fs = new FileStream(@"text.txt", FileMode.Open, FileAccess.Read);//把文件方法放在debug里
StreamReader sr = new StreamReader(fs, Encoding.Default);
ArrayList strIP = new ArrayList();
while (sr.Peek() >= 0)
{ //读到最后一行
strIP.Add(sr.ReadLine());
}
sr.Close();
double[] temp = new double[5];
for (int i = 0; i < strIP.Count; i++)
{
string[] p = strIP.ToString().Split('.');
for (int j = 0; j < p.Length; j++)
{
temp += (double.Parse(p[j])) * Math.Pow((double)256,(double)(p.Length - 1 - j));
}
}
for (int i = 0; i < strIP.Count - 1; i++)
{
for (int j = 0; j < strIP.Count - 1; j++)
{
if (temp[j] > temp[j + 1])
{
double tempd = temp[j + 1];
temp[j + 1] = temp[j];
temp[j] = tempd;
string strBu = (string)strIP[j];
strIP[j] = strIP[j + 1];
strIP[j + 1] = strBu;
}
}
}
for (int i = 0; i < strIP.Count; i++)
{
//Console.WriteLine(temp);
Console.WriteLine(strIP);
}
Console.ReadKey();
}
}
}
|
|