⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 5-0-1.cpp

📁 Accelerated C++ 课后练习题 本人自己完成、可供参考
💻 CPP
字号:
#include<iostream>
#include<string>
#include<vector>
#include<cctype>
using namespace std;
/**/
vector<string> split (const string& s)
{
	vector<string> ret;
	typedef string::size_type string_size;
	string_size i=0;

	//ivarinant: we have processed characters [original value of i, i)
	while(i!=s.size())
	{
		//ignore leading blanks
		//invariant: characters in range [original i, current i) are all spaces
		while(i!=s.size()&&isspace(s[i]))
			++i;
		//find end of next word
		string_size j=i;
		//invariant: none of the characters in range [original j,current j)is a space
		while(j!=s.size()&&!isspace(s[j]))
			j++;
		//if we found some nonwhitespace characters
		if(i!=j)
		{
			//copy from s starting at i and taking j-i chars
			ret.push_back(s.substr(i,j-i));
			i=j;
		}
	}
	return ret;
}

string::size_type width(const vector<string>& v)
{
	string::size_type maxlen=0;
	for(vector<string>::size_type i=0;i!=v.size();++i)
		maxlen=_MAX(maxlen,v[i].size());
	return maxlen;
}

vector<string> frame(const vector<string>& v)
{
	vector<string> ret;
	string::size_type maxlen=width(v);
	string border(maxlen+4,'*');

	//write the top border
	ret.push_back(border);

	//write each interior row, bordered by asterisk and a space
	for(vector<string>::size_type i=0;i!=v.size();++i)
		ret.push_back("* " + v[i] + string(maxlen-v[i].size(),' ') + " *");

	//write the bottom  border
	ret.push_back(border);
	return  ret;
}

vector<string> vcat(const vector<string>& top,const vector<string>&bottom)
{
	//copy the top picture
	vector<string> ret=top;

	//copy entire bottom picture
	for(vector<string>::const_iterator it=bottom.begin();it!=bottom.end();++it)
		ret.push_back(*it);

	return ret;	
}

vector<string> hcat(const vector<string>& left,const vector<string>& right)
{
	vector<string> ret;

	//add 1 to leave a space between pictures
	string::size_type width1=width(left)+1;

	//indices to look at elements from left both pictures
	vector<string>::size_type i=0,j=0;

	//continue until we've seen all rows from both pictures
	while(i!=left.size()||j!=right.size())
	{
		//construct new string to hold characters from both pictrues
		string s;

		//copy a row from the left_hand side, if there is one 
		if(i!=left.size())
			s=left[i++];

		//pad to full width
		s+=string(width1-s.size(),' ');

		//copy a fow from the right-hand size,if there is one
		if(j!=right.size())
			s+=right[j++];

		//adds to the picture we're creating
		ret.push_back(s);
	}
	return ret;
}

int main()
{
	string s;
	getline(cin,s);
	vector<string> v=split(s);
	//write each word in v
	for(vector<string>::size_type i=0;i<v.size();++i)
		cout<<v[i]<<endl;

	vector<string> u=frame(v);
	for(vector<string>::size_type j=0;j<u.size();++j)
		cout<<u[j]<<endl;

	vector<string> w=vcat(u,v);
	for(vector<string>::size_type k=0;k<w.size();++k)
		cout<<w[k]<<endl;

	vector<string> w1=hcat(v,u);
	for(i=0;i<w1.size();++i)
		cout<<w1[i]<<endl;
	return 0;
}




⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -