Qt 实现 IP 地址排序

对一个保存 IP 地址的 txt 文件进行排序,保存成新的 txt 文件。

参考:

IP地址排序、IP区段去重 - 优快云博客
https://blog.youkuaiyun.com/whz_zb/article/details/7409320

lastb.txt

36.2.209.189
113.108.44.11
113.172.123.18
41.236.206.196
171.228.163.78
177.125.22.129
86.57.137.167
14.191.233.250
14.169.216.75
111.40.119.29
36.110.10.178
177.125.21.200
14.207.171.207
186.101.208.89
113.161.61.8
113.174.40.131
200.34.232.202
180.183.247.237
186.67.220.29
111.85.220.242
14.169.221.117
181.39.198.40
190.214.218.22
197.50.81.77
184.82.192.118
197.251.179.206
124.232.146.42

main.cpp

#include "mainwindow.h"
#include <QApplication>
#include <QFile>
#include <QTextStream>
#include <QTextCodec>

bool compare(const int &num1, const int &num2)
{
    if (num1 < num2)
    {
        return true;
    }

    return false;
}

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();

    QFile file("lastb.txt");
    file.open(QIODevice::ReadOnly | QIODevice::Text);

    QStringList ips;
    QTextStream in(&file);
    while (!in.atEnd())
    {
        QString line = in.readLine();
        ips << line;
    }

    QList<int> first;
    QList<int> second;
    QList<int> third;
    QList<int> fourth;

    foreach (QString str, ips)
    {
        QStringList ip = str.split(".");
        int num1 = ip[0].toInt();
        int num2 = ip[1].toInt();
        int num3 = ip[2].toInt();
        int num4 = ip[3].toInt();

        if (!first.contains(num1))
        {
            first.append(num1);
        }

        if (!second.contains(num2))
        {
            second.append(num2);
        }

        if (!third.contains(num3))
        {
            third.append(num3);
        }

        if (!fourth.contains(num4))
        {
            fourth.append(num4);
        }
    }

    bool compare(const int &num1, const int &num2);
    qSort(first.begin(), first.end(), compare);
    qSort(second.begin(), second.end(), compare);
    qSort(third.begin(), third.end(), compare);
    qSort(fourth.begin(), fourth.end(), compare);

    QStringList ret;
    for (int index = 0; index < first.length(); ++index)
    {
        for (int index2 = 0; index2 < second.length(); ++index2)
        {
            for (int index3 = 0; index3 < third.length(); ++index3)
            {
                for (int index4 = 0; index4 < fourth.length(); ++index4)
                {
                    QString ip = QStringLiteral("%1.%2.%3.%4").arg(first[index]).arg(second[index2]).arg(third[index3]).arg(fourth[index4]);
                    if (ips.contains(ip))
                    {
                        ret << ip;
                    }
                }
            }
        }
    }

    QFile file2("lastb_sorted.txt");
    file2.open(QIODevice::WriteOnly | QIODevice::Text);

    QTextStream out(&file2);
    out.setCodec(QTextCodec::codecForName("UTF-8"));
    foreach (QString str, ret)
    {
        out << str << endl;
    }

    return a.exec();
}

lastb_sorted.txt

14.169.216.75
14.169.221.117
14.191.233.250
14.207.171.207
36.2.209.189
36.110.10.178
41.236.206.196
86.57.137.167
111.40.119.29
111.85.220.242
113.108.44.11
113.161.61.8
113.172.123.18
113.174.40.131
124.232.146.42
171.228.163.78
177.125.21.200
177.125.22.129
180.183.247.237
181.39.198.40
184.82.192.118
186.67.220.29
186.101.208.89
190.214.218.22
197.50.81.77
197.251.179.206
200.34.232.202

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值