FOJ 1033 URLs(水题)

Problem Description

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:

<protocol> "://" <host> [ ":" <port> ] [ "/" <path> ]

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,

<protocol> is always one of http, ftp or gopher.

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

<port> is a positive integer, smaller than 65536.

<path> 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 string <default> instead. Adhere to the format shown in the sample output.

Print a blank line after each test case.

 

Sample Input

3ftp://acm.baylor.edu:1234/pub/staff/mr-phttp://www.informatik.uni-ulm.de/acmgopher://veryold.edu

Sample Output

URL #1Protocol = ftpHost = acm.baylor.eduPort = 1234Path = pub/staff/mr-pURL #2Protocol = httpHost = www.informatik.uni-ulm.dePort = <default>Path = acmURL #3Protocol = gopherHost = veryold.eduPort = <default>Path = <default>

 

没有算法,照着做就对了

 

AC代码:

 

 

# include <iostream> 
# include <string>
# include <iomanip>
using namespace std;
void func(string s);
int t, m;
int main(){
	string s;
	cin>>t;
	m=0;
	while(t--){
		m++;
		cout<<"URL #"<<m<<endl;
		cin>>s;
		func(s);
		cout<<endl;
	} 
	return 0;
}
void func(string s){
	int k, i;
	string ss;
	ss="";
	for(i=0; i<=s.length()-1; i++){
		if(s[i]!=':'){
			ss=ss+s[i];
		}
		else{
			cout<<std::left<<setw(9)<<"Protocol";
			cout<<"= "<<ss<<'\n';
			break;
		}
	}	
	while(s[i]==':'||s[i]=='/'){
		i++;
	}
	ss="";
	for(k=i; k<=s.length()-1; k++){
		if(s[k]!=':'&&s[k]!='/')
		ss=ss+s[k];
		else{
			
			break;
		}
	}
	cout<<std::left<<setw(9)<<"Host";
	cout<<"= "<<ss<<'\n';
	if(k==s.length()){
		cout<<std::left<<setw(9)<<"Port";
		cout<<"= <default>\n";
		cout<<std::left<<setw(9)<<"Path";
		cout<<"= <default>\n";
	}
	else{
		if(s[k]==':'){
			ss="";
			for(i=k+1; i<=s.length()-1; i++){
				if(s[i]!='/'){
					ss=ss+s[i];
				}
				else{
					break;
				}
			}
			cout<<std::left<<setw(9)<<"Port";
			cout<<"= "<<ss<<'\n';
			if(i==s.length()){
				cout<<std::left<<setw(9)<<"Path";
				cout<<"= <default>\n";
			}
			else{
				ss="";
				for(k=i+1; k<=s.length()-1; k++){
					ss=ss+s[k];
				}
				cout<<std::left<<setw(9)<<"Path";
				cout<<"= "<<ss<<endl;
			}
		}
		else{
			cout<<std::left<<setw(9)<<"Port";
			cout<<"= <default>\n";
			ss="";
			for(i=k+1; i<=s.length()-1; i++){
				ss=ss+s[i];
			}
			cout<<std::left<<setw(9)<<"Path";
			cout<<"= "<<ss<<endl;
		}
	}
}

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值