2024年12月完成(补充提交)

This commit is contained in:
Cx330 committed 2025-03-29 23:02:48 +08:00
commit 4e2bf47db0
39 files changed
+2106

No files matched your search

@@ -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
@@ -0,0 +1,85 @@
#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> // 引入算法库,提供常用的算法
#include <iostream> // 引入标准输入输出流库
#include <iterator> // 引入迭代器库
#include <string> // 引入字符串库
#include <vector> // 引入动态数组(vector)库
#include "pics.h" // 引入自定义的 pics.h 库(假设此库包含框架生成和拼接相关的功能)
using std::cout; // 使用标准输出流
using std::copy; // 引入copy算法
using std::endl; // 引入换行符
using std::ostream_iterator; // 引入输出流迭代器,用于输出容器的内容
using std::string; // 使用字符串类型
using std::vector; // 使用vector容器
int main() {
// 创建一个vector<string>类型的容器 p,存储字符串
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"); // 添加一个字符串
// 创建输出流迭代器,指定输出到 cout,并且每个元素输出后换行
ostream_iterator<string> ofile(cout, "\n");
// 使用copy算法,将vector p中的内容逐个输出
copy(p.begin(), p.end(), ofile);
cout << endl; // 输出换行符
// 对vector p应用框架操作,生成一个新的vector f
vector<string> f = frame(p);
// 使用copy算法,将vector f中的内容逐个输出
copy(f.begin(), f.end(), ofile);
cout << endl; // 输出换行符
// 将vector p和vector f进行横向拼接,生成一个新的vector h
vector<string> h = hcat(p, frame(p));
// 使用copy算法,将vector h中的内容逐个输出
copy(h.begin(), h.end(), ofile);
cout << endl; // 输出换行符
return 0; // 程序正常结束
}