您的位置:首页 > 其它

ZOJ-1243(parse URL)

2015-08-01 21:51 274 查看
刷刷水题练练手速



#include <iostream>
#include <string>
using namespace std;

struct URL{
string protocol;
string host;
string port;
string path;
};
void parseUrl(const string& s, URL& url)
{
size_t p = s.find("://");
url.protocol = s.substr(0, p);
size_t q = s.find_first_of(":/", p+3);
if(q == string::npos){
url.host = s.substr(p+3);
url.port = url.path = "<default>";
return;
}
else url.host = s.substr(p+3, q-p-3);

size_t r = q;
if(s[q] == ':'){
r = s.find('/', q+1);
if(r == string::npos) r = s.size();
url.port = s.substr(q+1, r-q-1);
}
else url.port = "<default>";
if(r < s.size()) url.path = s.substr(r+1);
else url.path = "<default>";
}
inline void printUrl(const URL& url)
{
cout << "Protocol = " << url.protocol << "\n";
cout << "Host     = " << url.host << "\n";
cout << "Port     = " << url.port << "\n";
cout << "Path     = " << url.path << "\n";
}

int main()
{
ios::sync_with_stdio(false);

URL url;
string s;
int i = 1, n;
for(cin >> n; i <= n; ++i){
cin >> s;
parseUrl(s, url);
cout << "URL #" << i << "\n";
printUrl(url);
cout << "\n";
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: