-
Notifications
You must be signed in to change notification settings - Fork 0
/
framing.cpp
79 lines (63 loc) · 1.79 KB
/
framing.cpp
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
79
#include <iostream>
#include <vector>
#include <algorithm>
using std::vector;
using std::string;
using std::max;
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 ansterisk 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;
ret.insert(ret.end(), bottom.begin(), bottom.end());
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 and right
//respectively
vector<string>::size_type i = 0, j = 0;
while(i != left.size() || j != right.size())
{
//construct new string to hold characters from
//both pictures
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 row from the right-hand side, if there is one
if( j != right.size())
s += right[j++];
//add s to the picture we're creating
ret.push_back(s);
}
return ret;
}