2024年12月完成(补充提交)
This commit is contained in:
commit
4e2bf47db0
39 files changed
+2106
No files matched your search
@@ -0,0 +1,88 @@
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <iterator>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "pics.h"
|
||||
|
||||
using std::cout;
|
||||
using std::copy;
|
||||
using std::endl;
|
||||
using std::ostream_iterator;
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<string> p;
|
||||
p.push_back("this is an");
|
||||
p.push_back("example");
|
||||
p.push_back("to");
|
||||
p.push_back("illustrate");
|
||||
p.push_back("framing");
|
||||
|
||||
ostream_iterator<string>ofile(cout, "\n");
|
||||
copy(p.begin(), p.end(), ofile);
|
||||
cout << endl;
|
||||
|
||||
vector<string> f = frame(p);
|
||||
copy(f.begin(), f.end(), ofile); cout << endl;
|
||||
|
||||
vector<string> h = hcat(p,frame(p));
|
||||
copy(h.begin(), h.end(), ofile);
|
||||
cout << endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
#include <algorithm> // 引入标准算法库(用于std::copy等算法)
|
||||
#include <iostream> // 引入标准输入输出流库
|
||||
#include <iterator> // 引入迭代器库(用于std::ostream_iterator)
|
||||
#include <string> // 引入字符串库
|
||||
#include <vector> // 引入动态数组(vector)容器库
|
||||
|
||||
#include "pics.h" // 引入自定义头文件,提供frame和hcat函数的声明
|
||||
|
||||
using std::cout; // 引入标准输出对象
|
||||
using std::copy; // 引入标准拷贝算法
|
||||
using std::endl; // 引入标准换行符
|
||||
using std::ostream_iterator; // 引入输出流迭代器
|
||||
using std::string; // 引入标准字符串类
|
||||
using std::vector; // 引入标准向量类
|
||||
|
||||
int main()
|
||||
{
|
||||
// 定义一个存储字符串的vector,并向其中添加多行字符串
|
||||
vector<string> p;
|
||||
p.push_back("this is an"); // 添加字符串到vector
|
||||
p.push_back("example");
|
||||
p.push_back("to");
|
||||
p.push_back("illustrate");
|
||||
p.push_back("framing");
|
||||
|
||||
// 定义输出流迭代器,用于将vector内容输出到标准输出
|
||||
ostream_iterator<string> ofile(cout, "\n");
|
||||
|
||||
// 输出vector p的内容到终端
|
||||
copy(p.begin(), p.end(), ofile);
|
||||
cout << endl; // 输出一个空行分隔内容
|
||||
|
||||
// 调用frame函数为vector p添加边框,并返回新vector
|
||||
vector<string> f = frame(p);
|
||||
|
||||
// 输出带边框的vector内容
|
||||
copy(f.begin(), f.end(), ofile);
|
||||
cout << endl; // 输出一个空行分隔内容
|
||||
|
||||
// 调用hcat函数,将原始vector p与带边框的vector f水平连接
|
||||
vector<string> h = hcat(p, frame(p));
|
||||
|
||||
// 输出水平连接后的vector内容
|
||||
copy(h.begin(), h.end(), ofile);
|
||||
cout << endl; // 输出一个空行分隔内容
|
||||
|
||||
return 0; // 程序结束
|
||||
}
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "pics.h"
|
||||
|
||||
using std::string;
|
||||
using std::vector;
|
||||
using std::max;
|
||||
|
||||
#ifdef _MSC_VER
|
||||
//#include "../minmax.h"
|
||||
#else
|
||||
//using std::max;
|
||||
#endif
|
||||
|
||||
string::size_type width(const vector<string>& v)
|
||||
{
|
||||
string::size_type maxlen = 0;
|
||||
#ifdef _MSC_VER
|
||||
for(std::vector<string>::size_type i = 0; i != v.size(); ++i)
|
||||
#else
|
||||
for(vector<string>::size_type i = 0; i != v.size(); ++i)
|
||||
#endif
|
||||
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 an asterisk and a space
|
||||
#ifdef _MSC_VER
|
||||
for (std::vector<string>::size_type i = 0; i != v.size(); ++i) {
|
||||
#else
|
||||
for (vector<string>::size_type i = 0; i != v.size(); ++i) {
|
||||
#endif
|
||||
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
|
||||
#ifdef _MSC_VER
|
||||
for (std::vector<string>::const_iterator it = bottom.begin();
|
||||
#else
|
||||
for (vector<string>::const_iterator it = bottom.begin();
|
||||
#endif
|
||||
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' and `right' respectively
|
||||
#ifdef _MSC_VER
|
||||
std::vector<string>::size_type i = 0, j = 0;
|
||||
#else
|
||||
vector<string>::size_type i = 0, j = 0;
|
||||
#endif
|
||||
string s;
|
||||
// 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 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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
#ifndef GUARD_pics_h
|
||||
#define GUARD_pics_h
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
std::string::size_type width(const std::vector<std::string>& v);
|
||||
|
||||
std::vector<std::string> frame(const std::vector<std::string>& v);
|
||||
|
||||
std::vector<std::string> vcat(const std::vector<std::string>& top,
|
||||
const std::vector<std::string>& bottom);
|
||||
|
||||
std::vector<std::string> hcat(const std::vector<std::string>& left,
|
||||
const std::vector<std::string>& right);
|
||||
|
||||
#endif
|
||||
Reference in new issue
Block a user