• ZOJ 1243 URLs


    /*
    In the early nineties, the World Wide Web (WWW) was invented. Nowadays, most people think that the WWW simply consists of all the pretty (or not so pretty) HTML-pages that you can read with your WWW browser. But back then, one of the main intentions behind the design of the WWW was to unify several existing communication protocols.

    Then (and even now), information on the Internet was available via a multitude of channels: FTP, HTTP, E-Mail, News, Gopher, and many more. Thanks to the WWW, all these services can now be uniformly addressed via URLs (Uniform Resource Locators). The syntax of URLs is defined in the Internet standard RFC 1738. For our problem, we consider a simplified version of the syntax, which is as follows:

    “://” [ ":" ] [ "/" ]

    The square brackets [] mean that the enclosed string is optional and may or may not appear. Examples of URLs are the following:

    http://www.informatik.uni-ulm.de/acm

    ftp://acm.baylor.edu:1234/pub/staff/mr-p
    gopher://veryold.edu

    More specifically,

    is always one of http, ftp or gopher.

    is a string consisting of alphabetic (a-z, A-Z) or numeric (0-9) characters and points (.).

    is a positive integer, smaller than 65536.

    is a string that contains no spaces.

    You are to write a program that parses an URL into its components.

    Input

    The input starts with a line containing a single integer n, the number of URLs in the input. The following n lines contain one URL each, in the format described above. The URLs will consist of at most 60 characters each.

    Output

    For each URL in the input first print the number of the URL, as shown in the sample output. Then print four lines, stating the protocol, host, port and path specified by the URL. If the port and/or path are not given in the URL, print the stringinstead. Adhere to the format shown in the sample output.

    Print a blank line after each test case.

    Sample Input

    3
    ftp://acm.baylor.edu:1234/pub/staff/mr-p

    http://www.informatik.uni-ulm.de/acm

    gopher://veryold.edu

    Sample Output

    URL #1
    Protocol = ftp
    Host = acm.baylor.edu
    Port = 1234
    Path = pub/staff/mr-p

    URL #2
    Protocol = http
    Host = www.informatik.uni-ulm.de
    Port = 
    Path = acm

    URL #3
    Protocol = gopher
    Host = veryold.edu
    Port = 
    Path = 
    */

    #include <string>
    #include <iostream>
     
     using namespace std;
     
     int main()
     {
         int n; string s;
         
         cin >> n;
         for(int i = 1; i <= n; i++)
         {
             int p;
             cin >> s;
             
             p = s.find("://");
             
             string protocol = s.substr(0, p);
             string host = s.substr(p + 3, s.size());
             string port;
             string path;
             
             p = host.find('/');
             if(p != string::npos)
             {
                 path = host.substr(p + 1, host.size());
                 host = host.substr(0, p);
             }
            
             p = host.find(':');
             if(p != string::npos)
             {
                 int k;
                 for(k = p + 1; isdigit(host[k]); k++)
                     port += host[k];
                 host = host.substr(0, p);
             }
             
             cout << "URL #" << i << endl
                  << "Protocol = " << protocol << endl
                  << "Host     = " << host     << endl
                  << "Port     = " << (port == "" ? "<default>" : port) << endl
                  << "Path     = " << (path == "" ? "<default>" : path) << endl << endl;
         }
         
         return 0;
     }
  • 相关阅读:
    sql:除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询
    [转]sql:除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询
    [转]IIS6 伪静态 IIS文件类型映射配置方法 【图解】
    IIS6 伪静态 IIS文件类型映射配置方法 【图解】
    [转]正则表达式的多行模式与单行模式
    正则表达式的多行模式与单行模式
    [原]MS SQL表字段自增相关的脚本
    MS SQL表字段自增相关的脚本
    angular学习笔记(六)-非入侵式javascript
    angular学习笔记(五)-阶乘计算实例(3)
  • 原文地址:https://www.cnblogs.com/naive/p/3568821.html
Copyright © 2020-2023  润新知