?? 5-9.cpp
字號:
#include<iostream>
#include<string>
#include<vector>
#include<cctype>
#include<algorithm>
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)
{
vector<string>::size_type l=maxlen-v[i].size();
ret.push_back("* " + string(l/2,' ')+ v[i] + string(l-l/2,' ') + " *");
}
//write the bottom border
ret.push_back(border);
return ret;
}
bool ugrade(const string s)
{
return s<"a";
}
vector<string> upper(vector<string>& v)
{
vector<string>::iterator iter=stable_partition(v.begin(),v.end(),ugrade);
vector<string> u(iter,v.end());
v.erase(iter,v.end());
return u;
}
int main()
{
string s;
getline(cin,s); //int put a line string
vector<string> v=split(s); //split string to words
vector<string> u=upper(v);
vector<string> w1=frame(u); //add a border to words
vector<string> w2=frame(v);
for(vector<string>::size_type i=0;i<w1.size();++i) //output
cout<<w1[i]<<endl;
for(i=0;i<w2.size();++i)
cout<<w2[i]<<endl;
return 0;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -