C++ program do skracania URL pomocy !

0

Cześć , mam do napisania na zaliczenie program w c++ który skraca url , coś podobnego jak np tiny.pl
funkcjonalność :
generowanie skrótu , weryfikacja czy dla danego linku jest już skrót , zwracanie linku dla wskazanego skrótu , wyświetlanie pełnej bazy skrótów.
Program ma skracać do 5 znaków ( 0,9 a-z A-Z ) np po wpisaniu adresu www.testetstetste.pl/gole/pl ma zamienic na 0awA5
Czy ktoś z Was wie w jaki sposób to napisać? Bardzo proszę o pomoc

0

Dziękuję , spróbuje przerobić ten kod , zarys juz mam :)

0

niestety ponizszy kod z gita nie dziala , nie komplikuje sie :( proszę o pomoc !

#include <iostream>
#include <algorithm>
#include <string>
#include <cstdlib>

using namespace std;

string id_to_short_url(unsigned int n) {
    
    char map[] = "abcdefghijklmnopqrstuvwxyzABCDEF"
                 "GHIJKLMNOPQRSTUVWXYZ0123456789";

    string short_url;

    
    while (n) {
        short_url.push_back(map[n % 62]);
        n /= 62;
    }

    
    reverse(short_url.begin(), short_url.end());

    return short_url;
}



unsigned int short_url_to_id(string short_url) {
    unsigned int id = 0;

    
    for (int i = 0; i < short_url.length(); ++i) {
        if ('a' <= short_url[i] && short_url[i] <= 'z') {
            id = id * 62 + short_url[i] - 'a';
        }
        if ('A' <= short_url[i] && short_url[i] <= 'Z') {
            id = id * 62 + short_url[i] - 'A' + 26;
        }
        if ('0' <= short_url[i] && short_url[i] <= '9') {
            id = id * 62 + short_url[i] - '0' + 52;
        }
    }
    return id;
}

int main(int argc, char** argv) {
    if (argc == 1) {
        cerr << "error: missing required parameter: id" << endl;
        return 1;
    }

    unsigned int n = atoi(argv[1]);

    if (n <= 0) {
        cerr << "error: invalid input value, an integer greater than 0 is required" << endl;
        return 1;
    }

    cout << "Input ID: " << n << endl << "---" << endl;

    string encoded_short_url = id_to_short_url(n);
    unsigned int decoded_id = short_url_to_id(encoded_short_url);

    cout << "Generated short URL: " << encoded_short_url << endl;
    cout << "ID decoded from URL: " << decoded_id << endl;
    return 0;
0

jesli jest na forum osoba ktora by chciala mi pomoc bardzo prosze o kontakt na prv

A ile płacisz?

0

Zobacz https://wandbox.org/permlink/Ooq2Ncnsolu8BXm4

#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <unordered_map>
#include <iomanip>
#include <ios>

using namespace std;

string idToShortURL( long int n )
{
    // Map to store 62 possible characters
    char map[] = "abcdefghijklmnopqrstuvwxyzABCDEF"
                 "GHIJKLMNOPQRSTUVWXYZ0123456789";

    string shorturl;

    // Convert given integer id to a base 62 number
    while (n)
    {
        // use above map to store actual character
        // in short url
        shorturl.push_back(map[n%62]);
        n = n/62;
    }

    // Reverse shortURL to complete base conversion
    reverse(shorturl.begin(), shorturl.end());

    return shorturl;
}

int main()
{
    unordered_map<string,string> databaseURL;
    unordered_map<string,string> databaseID; // use in finding the proper url for a given id

    int counter {1};

    vector<string> inputURL {
                               "www.test.pl/abc/aaa/dcf/ghhhc45/lii" ,
                               "www.test.pl/abc/aaa/dcf/ghhhc45/lii" ,
                               "www.test.pl/ab2/a45/dc1/344c45/lii" ,
                               "www.test.pl/abc/aaa/dcf/ghh/lii" ,
                               "www.test.pl/abc/a/dcf/ghhhc45/lii" ,
                               "www.test.pl/abc/a/dcf/ghhhc45/lii" ,
                               "www.test.pl/a/aaa/dcf/ghhhc45/lii" ,
                               "www.test.pl/abc/aaa/dcf/ghhhc/aa" ,
                               "www.test.pl/abc/aaa/dlaskdlsakdii" ,
                               "www.test.pl/aTbc/aaa/dc9478hjdhsdhc45/lii"
                            };

    for( auto& url : inputURL )
    {
        if( databaseURL.find(url) == databaseURL.end() )
        {
            databaseURL[url] = idToShortURL(counter++);
            databaseID[databaseURL[url]] = url;
        }
    }

    cout << left << setw(50)<< "url" << "id" << "\n";
    for( const auto& [url,id] : databaseURL )
    {
        cout << left << setw(50) << url << id << "\n";
    }

    // use databaseID to find url by a given id
    cout << "\nurl for id \"i\" is " << databaseID.find("i")->second << "\n";

    return 0;
}
0

dziekuje bardzo za podpowiedzi ! Juz wiem jak to zrobic. Temat zamykam.

1 użytkowników online, w tym zalogowanych: 0, gości: 1