A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

本帖最后由 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();
        }
    }
}

yemp.jpg (53.96 KB, 下载次数: 0)

QQ上问题的截图

QQ上问题的截图

50 个回复

正序浏览
这个考试题,看了搞了好久才有些思路,基础视频也看了,但涉及多了就搞混了,看来还是基础不牢。。。
回复 使用道具 举报
回复 使用道具 举报
zhangcheng5468 发表于 2013-7-9 14:08
这些IP是手动拷贝到txt文件还是通过程序写入txt文件?

手动写,是给出来的
回复 使用道具 举报
  1. static void Main(string[] args)
  2.         {
  3.             List<string> s = File.ReadLines(@"ip.txt", Encoding.Default).ToList<string>();

  4.             for (int i = 0; i < s.Count - 1; i++)
  5.             {
  6.                 for (int j = 0; j < s.Count - 1 - i; j++)
  7.                 {
  8.                     if (ToNumber(s[j]) > ToNumber(s[j + 1]))
  9.                     {
  10.                         string temp = s[j];
  11.                         s[j] = s[j + 1];
  12.                         s[j + 1] = temp;
  13.                     }
  14.                 }
  15.             }
  16.             foreach (string str in s)
  17.             {
  18.                 Console.WriteLine(str);
  19.             }
  20.             Console.ReadKey();
  21.         }
  22.         private static long ToNumber(string s)
  23.         {
  24.             string resault = "";
  25.             for (int i = 0; i < 4; i++)
  26.             {
  27.                 resault += s.Split('.')[i].PadLeft(3, '0');
  28.             }
  29.             return long.Parse(resault);
  30.         }
复制代码
回复 使用道具 举报
这些IP是手动拷贝到txt文件还是通过程序写入txt文件?
回复 使用道具 举报
面试不是基础题么?怎么这么难的!
回复 使用道具 举报
好有难度,被打击了,
回复 使用道具 举报
本帖最后由 sym544135698 于 2013-7-5 13:33 编辑


你的这段代码是不行的,总是说字符串有问题,需要trycatch
  1. try
  2. {
  3. sum = sum + int.Parse(p[i]) * 256 ^ (i);
  4. }
  5. catch
  6. { }
复制代码
sum那里也不是的
回复 使用道具 举报

你的string[] str = File.ReadAllLines("IP地址.txt", Encoding.Default);最后一个Encoding.Default是什么意思啊
回复 使用道具 举报

网上看到的一种写法:用移位操作,将ip地址转换为long型整数,排序后,再将long整数转为string。
不过这里移位后累加,最后得出一个long整数的原理不太明白,还想请教一下。
代码:
  1. #region IP地址转换为long型整数
  2.         public static long ip2long(string strIP)
  3.         {
  4.             IPAddress ip = IPAddress.Parse(strIP);
  5.             int x = 3;
  6.             long o = 0;
  7.             foreach (byte f in ip.GetAddressBytes())
  8.             {
  9.                 o += (long)f << 8 * x--;
  10.             }
  11.             return o;
  12.         }
  13.         #endregion

  14.         #region long型整数转换为IP地址
  15.         public static string long2ip(long l)
  16.         {
  17.             var b = new Byte[4];
  18.             for (int i = 0; i < 4; i++)
  19.             {
  20.                 b[3 - i] = (byte)(l >> 8 * i & 255);
  21.             }
  22.             return new IPAddress(b).ToString();
  23.         }
  24.         #endregion
复制代码
回复 使用道具 举报

排序好了的  可以不用再比较了
回复 使用道具 举报
蔡志涛 发表于 2013-7-3 16:35
你的想法很精妙,不知道你是怎么得来的这个思路。另外,你的第一步冒泡排序的内层循环可以再优化下,可以 ...

差别也不大。
回复 使用道具 举报

你的想法很精妙,不知道你是怎么得来的这个思路。另外,你的第一步冒泡排序的内层循环可以再优化下,可以这么写for (int j = 0; j < str.Length - 1- i ; j++)
回复 使用道具 举报
逆_水_寒 发表于 2013-6-21 21:37
这是一个TXT文本,并且是一行一行写的,用  Line 能读出 “/r/n”,一行一行读出来放入数组再遍历吧。
第九 ...

哪个第九天的基础视频啊,能再说细一些吗,这道题不会做。。。
回复 使用道具 举报
本帖最后由 曾大鹏 于 2013-6-26 22:17 编辑
  1. static void Main(string[] args)
  2.         {
  3.             string[] str = File.ReadAllLines("IP地址.txt", Encoding.Default);
  4.             for (int i = 0; i < str.Length - 1; i++)
  5.             {
  6.                 for (int j = 0; j < str.Length - 1; j++)
  7.                 {
  8.                     if (ToNumber(str[j]) > ToNumber(str[j + 1]))
  9.                     {
  10.                         string strBu = str[j];
  11.                         str[j] = str[j + 1];
  12.                         str[j + 1] = strBu;
  13.                     }
  14.                 }
  15.             }
  16.             for (int i = 0; i < str.Length; i++)
  17.             {
  18.                 Console.WriteLine(str[i]);
  19.             }
  20.             Console.ReadKey();
  21.         }
  22.         /*Ip地址格式为:a.b.c.d
  23.         每个数字范围在0~255之间 那么我们可以把它们看成一个四位的256进制数
  24.         然后转换成十进制 =a*256^3+b*256^2+c*256^1+d*256^0
  25.         然后根据对应的十进制大小排序就OK了。。*/
  26.         private static int ToNumber(string str)
  27.         {
  28.             string[] p = str.Split('.');
  29.             int sum = 0;
  30.             for (int i = 0; i < p.Length; i++)
  31.             {
  32.                 sum = sum * 256 + int.Parse(p[i]);
  33.             }
  34.             return sum;
  35.         }
复制代码
回复 使用道具 举报 3 0
string path = @"E:\IP地址.txt";
            string[] strs = System.IO.File.ReadAllLines(path,Encoding.Default);
            List<int> numIP = new List<int>();
            string[] strQ = strs[0].Split('.');
            string s = strQ[0] +"."+ strQ[1] +"."+ strQ[2]+".";//取字符串中的相同部分
            for (int i = 0; i < strs.Length; i++)
            {
                numIP.Add(Convert .ToInt32 ( strs[i].Split ('.')[3]));
            }
            for (int i = 0; i < numIP.Count; i++)//比较最后一位的大小
            {
                for (int j = i; j < numIP.Count; j++)
                {
                    if (numIP[i] > numIP[j])
                    {
                        int temp = numIP[i];
                        numIP[i] = numIP[j];
                        numIP[j] = temp;
                    }
                }
                Console.WriteLine(s + numIP[i].ToString());//输出结果
            }
            

QQ图片20130625163213.jpg (11.33 KB, 下载次数: 0)

QQ图片20130625163213.jpg
回复 使用道具 举报
本帖最后由 曾大鹏 于 2013-6-25 14:31 编辑
wanghuailin1030 发表于 2013-6-25 10:03
你看看我实现的源码,不知道什么原因,转换进制时算出来的数和实际的数差个一两百
namespace temp
{

temp += (int.Parse(p[j])) * (256 ^ (p.Length - 1 - j));
这里错了。。
^ 在C#中表示异或,不是次方
计算次方 可以用Math.Pow()函数
改成这样
  1. temp[i]=0;
  2. for(int j=0;j<p.Length;j++)
  3. {
  4.   temp[i]=temp[i]*256+int.Parse(p[j]);
  5. }
复制代码
回复 使用道具 举报
^   这个东西在程序里是按位异或的意思 。
回复 使用道具 举报
123下一页
您需要登录后才可以回帖 登录 | 加入黑马