#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

string ip[500];
int N;
string ipx;
string resz[8];

void kiir(int sorszam)
{
    cout << sorszam << ". feladat" << endl;
}

void F1()
{
    ifstream be;
    be.open("ip.txt");
    int i=0;
    while(!be.eof())
    {
        be >> ip[i]; cout << ip[i] << endl;
        i++;
    }
    N=i-1;
    be.close();
}

void F2()
{
    cout << "Az állományban " << N << " darab adatsor van" << endl;
}

void F3()
{
    ofstream ki2;
    ki2.open("rendezett.txt");
    string ip2[500];
    for(int i=0;i<N;i++) { ip2[i]=ip[i]; }
    for(int i=0;i<N-1;i++)
    {
        for(int j=i+1;j<N;j++)
        {
            if(ip2[i]>ip2[j])
            {
                swap(ip2[i],ip2[j]);
            }
        }
    }
    for(int i=0;i<N;i++) ki2 << ip2[i] << endl;
    ki2.close();
    cout << "A legalacsonyabb tárolt IP-cím:" << endl;
    cout << ip2[0] << endl;

}

void F4()
{
    string d="2001:0db8";
    string g="2001:0e";
    string h1="fc";
    string h2="fd";
    string s;
    int ddb=0, gdb=0, hdb=0;
    for(int i=0;i<N;i++)
    {
        if(ip[i].substr(0,9)==d) ddb++;
        if(ip[i].substr(0,7)==g) gdb++;
        if(ip[i].substr(0,2)==h1||ip[i].substr(0,2)==h2) hdb++;
    }
    cout << "Dokumentációs cím: " << ddb << endl;
    cout << "Globális egyedi cím: " << gdb << endl;
    cout << "Helyi egyedi cím: " << hdb << endl;
}

void F5()
{
    ofstream ki;
    ki.open("sok.txt");
    int nulla;
    int hatar=18;
    string ssz;
    for(int i=0;i<N;i++)
    {
        nulla=0;
        for(int j=0;j<ip[i].length();j++)
        {
            if(ip[i][j]=='0') nulla++;
        }
        if(nulla>=hatar)
        {
            ki << i+1 << " " << ip[i] << endl;
            cout << i+1 << " " << ip[i] << endl;
        }
    }
    ki.close();
}

void F6()
{
    int ssz;
    cout << "Kérek egy sorszámot: ";
    cin >> ssz; int i=ssz-1;
    int index=0;
    cout << ip[i] << endl;

    for(int j=0;j<8;j++)
    {
        resz[j]=ip[i].substr(j*5,4);
    }
    cout << endl;
    for(int i=0;i<8;i++)
    {
        while(resz[i][0]=='0')
        {
            resz[i]=resz[i].substr(1,4);

        }
        if(resz[i].length()==0) resz[i]="0";
        if(i<7)
        {
            ipx+=resz[i]+":";
        }
        else
        {
            ipx+=resz[i];
        }
    }
    cout << "A rövidített IP-cím:" << endl;
    cout << ipx << endl;
}

void F7()
{
    int max=0;
    int db=0, hely;
    for(int i=0;i<8;i++)
    {
        if(resz[i]=="0")
        {
            int j=i+1;
            while(resz[j]=="0")
            {
                j++;
            }
            db=j-i;
            if(db>max)
            {
                max=db; db=0;
                hely=i;
            }
            i=j;
        }
    }
    cout << "Lehetséges rövidítés helye: " << hely << ", száma: " << max << endl;
    for(int i=hely;i<hely+max;i++)
    {
        resz[i]="";
    }
    ipx="";
    for(int i=0;i<8;i++)
    {
        if(resz[i]!="")
        {
            if(i<7) ipx+=resz[i]+":";
            else ipx+=resz[i];
        }
        if(resz[i]==""&&hely%5==i) ipx+=":";
    }

    cout << endl << ipx << endl;
}

int main()
{
    setlocale(LC_ALL, "hun");
    kiir(1);
    F1();
    kiir(2);
    F2();
    kiir(3);
    F3();
    kiir(4);
    F4();
    kiir(5);
    F5();
    kiir(6);
    F6();
    kiir(7);
    F7();
    return 0;
}
