forked from orazaro/accelerated
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06-01-frame-hcat.cpp
More file actions
78 lines (69 loc) · 1.86 KB
/
06-01-frame-hcat.cpp
File metadata and controls
78 lines (69 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <string>
#include <vector>
using namespace std;
void out(const std::vector<std::string>& v)
{
for(std::vector<std::string>::const_iterator iter = v.begin();
iter != v.end(); ++iter)
cout << *iter << endl;
}
std::string::size_type width(const std::vector<std::string>& v)
{
string::size_type maxlen = 0;
for(std::vector<std::string>::const_iterator iter = v.begin();
iter != v.end(); ++iter)
maxlen = max(maxlen, iter->size());
return maxlen;
}
std::vector<std::string> frame(const std::vector<std::string>& v)
{
vector<string> ret;
string::size_type maxlen = width(v);
string border(maxlen + 4, '*');
ret.push_back(border);
for(std::vector<std::string>::const_iterator iter = v.begin();
iter != v.end(); ++iter) {
ret.push_back("* " + (*iter) + string(maxlen - iter->size(),' ') + " *");
}
ret.push_back(border);
return ret;
}
std::vector<std::string> vcat(const std::vector<std::string>& top,
const std::vector<std::string>& bottom)
{
vector<string> ret = top;
ret.insert(ret.end(),bottom.begin(),bottom.end());
return ret;
}
std::vector<std::string> hcat(const std::vector<std::string>& left,
const std::vector<std::string>& right)
{
vector<string> ret;
string::size_type width1 = width(left) + 1;
vector<string>::const_iterator
i = left.begin(),
j = right.begin();
while(i != left.end() || j != right.end())
{
string s;
if(i != left.end())
s = *i++;
s += string(width1 - s.size(),' ');
if(j != right.end())
s += *j++;
ret.push_back(s);
}
return ret;
}
int main()
{
// read data
vector<string> v,f;
string line;
while(getline(cin, line))
v.push_back(line);
f = frame(v);
out(vcat(v,f));
out(hcat(v,f));
}