本帖最后由 wanghuailin1030 于 2013-6-25 10:06 编辑
你看看我实现的源码,不知道什么原因,转换进制时算出来的数和实际的数差个一两百
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();
int[] temp = new int[5];
for (int i = 0; i < strIP.Count; i++)
{
string[] p = strIP.ToString().Split('.');
for(int j=0;j<p.Length;j++)
{
//Console.WriteLine(60 * 256 ^ 3 + 53 * 256 ^ 2 + 231 * 256 ^ 1 + 249 * 256 ^ 0);
temp += (int.Parse(p[j])) * (256 ^ (p.Length - 1 - j));
}
}
for (int i = 0; i < strIP.Count; i++)
{
Console.WriteLine(temp);
Console.WriteLine(strIP);
}
Console.WriteLine();
for (int i = 0; i < strIP.Count-1; i++)
{
for (int j = 0; j < strIP.Count-1; j++)
{
if (temp[j] > temp[j+1])
{
int 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();
}
}
}
|