- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我已经做了很多阅读,但没有找到任何适合 C++ 控制台输出的格式。我正在打印一个带环的三角形,我知道如何打印它,但我想在控制台中将其移动,这样我就可以在 cathetus 旁边显示文本。
所以我希望我的输出像
*
* *
8.5 inches * *
* *
*********
这是绘制三角形的函数:
void Triangle::DisplayTriangle() {
for (int x = 0; x <= 10; x++) {
setprecision(2);
for (int j = x; j > 10; j++) cout << " ";
for (int z = 0; z <= x; z++) {
if (x == 10) {
cout << '*';
}
else if (z == 0 || z == x) cout << "*";
else cout << " ";
}
for (int y = 0; y < x; y++) {
cout << " ";
}
cout << endl;
}
}
最佳答案
当您的目标输出变得越来越复杂时,手动循环绘制形状变得太困难了。
所以在某些时候,您会想要使用文本绘图库(例如 K. Shores 提到的 ncurses)。
但是,滚动您自己的简单绘图 API 并不是脑部手术:
#include <iostream>
namespace auto_list_exploit {
static auto li = {1,2};
using initializer_list = decltype(li);
}
template <typename CT=char>
class TextSurface {
using initializer_list = auto_list_exploit::initializer_list;
public:
struct point {
int x,y;
point(int x=0, int y=0) : x(x), y(y) {}
point(initializer_list list) : x((list.begin())[0]), y((list.begin())[1]) {}
};
private:
int m_width, m_height, m_data_count;
CT* m_data;
int OffsetOf(int x, int y) const { return y * m_width + x; }
bool IsInside(int x, int y) { return x >= 0 && y>= 0 && x < m_width && y < m_height; }
CT& at(int x, int y) { return m_data[OffsetOf(x,y)]; }
CT at(int x, int y) const { return m_data[OffsetOf(x,y)]; }
int Abs(int x) { return x < 0 ? -x : x; }
void Poly(const point& pfirst, CT value, const point& plast) {
Line(plast, pfirst, value);
}
template <typename ...Ts>
void Poly(const point& pfirst, CT value, const point& p0, const point& p1, Ts...args) {
Line(p0, p1, value);
Poly(pfirst, value, p1, args...);
}
public:
TextSurface(int width=0, int height=0) :
m_width(height ? width : 0),
m_height(width ? height : 0),
m_data_count(width * height)
{
m_data = m_data_count ? new CT[m_data_count] : nullptr;
Clear();
}
// Cannot copy
TextSurface(const TextSurface&) =delete;
TextSurface& operator = (const TextSurface&) =delete;
// Can return from functions
TextSurface& operator = (TextSurface&& src) {
delete [] m_data;
m_width = src.m_width; m_height = src.m_height; m_data_count = src.m_data_count;
m_data = src.m_data;
src.m_data = nullptr;
return *this;
}
TextSurface(TextSurface&& src) : m_data(nullptr) { operator = ((TextSurface&&)src); }
~TextSurface() { delete [] m_data; }
int Width() const { return m_width; }
int Height() const { return m_height; }
void Clear(CT value=' ') {
CT* p = m_data;
CT* pe = p + m_data_count;
while(p != pe) *(p++) = value;
}
void Line(int x0, int y0, int x1, int y1, CT value) {
const int dx = x1-x0;
const int dy = y1-y0;
if(!dx && !dy) {
if(IsInside(x0, y0)) { at(x0, y0) = value; }
return;
}
const bool ymajor = Abs(dy) > Abs(dx);
int x=x0, y=y0;
int &mj = ymajor ? y : x;
int &mn = ymajor ? x : y;
const int mje = ymajor ? y1 : x1;
const int mjst = mj < mje ? 1 : -1;
double mnd = mn;
double mnst = ymajor ? double(dx)/Abs(dy) : double(dy)/Abs(dx);
do {
mn = int(mnd + 0.5);
if(IsInside(x,y)) { at(x,y) = value; }
mj += mjst;
mnd += mnst;
} while(mj != mje+mjst);
}
void Line(const point& p0, const point& p1, CT value) { Line(p0.x, p0.y, p1.x, p1.y, value); }
template <typename ...Ts>
void Poly(CT value, const point& p0, const point& p1, Ts...args) {
Line(p0, p1, value);
Poly(p0, value, p1, args...);
}
// init-list overloads for triangle and quad (initializer_list can't be packed)
void Poly(CT value, initializer_list l0, initializer_list l1, initializer_list l2) { Poly(value, point(l0), point(l1), point(l2)); }
void Poly(CT value, initializer_list l0, initializer_list l1, initializer_list l2, initializer_list l3) { Poly(value, point(l0), point(l1), point(l2), point(l3)); }
void Rect(int x, int y, int width, int height, CT value) {
Poly(value, point(x,y), point(x+width-1, y), point(x+width-1, y+height-1), point(x, y+height-1));
}
// Label places text. xloc selects the meaning of the x coordinate.
// Its value is between -1 and 1: -1 for x at the left of the text,
// 0 for x in the middle, and 1 for x at the end of the text.
void Label(const CT* text, int x, int y, double xloc=-1) {
int len = 0;
while(text[len]) { ++len; }
x -= int((xloc + 1)/2 * (len - 1));
for(int i=0; i<len; ++i) {
if(IsInside(x,y)) { at(x,y) = text[i]; }
++x;
} }
void Present() const {
const CT* p = m_data;
for(int j=0; j<m_height; ++j) {
std::cout.write(p, m_width);
std::cout << '\n';
p += m_width;
} }
};
typedef TextSurface<char> CharSurface;
int main() {
CharSurface surf(60,20);
surf.Poly('*', {20,2}, {34,16}, {20,16});
surf.Label("14 ft", 18, 10, 1);
surf.Label("9 ft", 26, 17, 0);
surf.Present();
}
main()
中的示例输出:
*
**
* *
* *
* *
* *
* *
* *
14 ft * *
* *
* *
* *
* *
* *
***************
9 ft
关于c++ - 在控制台中打印时如何将循环打印的形状移到文本上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37062427/
我有一个 jqgrid,在设置多重检查时,我在第一列上获得复选框,我希望该复选框列成为最后一列。 我没有找到任何选项,因此我正在编写一个自定义 jquery 方法来将 tr 的第一个 td 移动到最后
在编程类(class)中,我被要求创建一个 iCalendar 应用程序的副本。我正在使用 JAVA 对其进行编码,并使用 JFrame 和 JPanel 对其进行绘制。这是我的问题的 SSCCEE:
想把我的 snackbar 移到 bottomnavigationview 上面
我正在用 Objective-C 创建一个聊天应用程序。我的对话面板有两个项目,文本字段和将显示消息的表格。我正在为文本字段及其按钮使用 UIView,此 View 位于屏幕底部。每当我开始打字时,键
该方法用于选择User和TargetUser之间的 Action 数,例如就在两个用户之间。此方法的结果值取决于 GetTotalOfPossibleActions() 动态返回值(每个用户都有自己的
我有一个关于 owl api 用法的快速问题。 假设我有一个名为 Species 的类,它有一个哺乳动物子类,它有一个灵长类动物子类,它有一个人类子类。 物种 -> 哺乳动物 -> 灵长类动物 ->
我需要 eclipse 内容帮助将“java.lang.String”放置在以“string”开头的类型之上(见附图)。那可能吗? 请注意,对于此问题,“string”必须首先输入非大写的“s”。大写
我正在研究 pdf 页面上的收缩和收缩功能。我的捏合和平移(移动)工作正常,但是当用户连续移动缩放 View 时,缩放 View 超出了 super View 边界。是这样的: 我如何限制平移移动,以
一般 typescript 问题说我迭代一个我知道它的内容的数组并应用一个reduce来取回一个我知道类型的对象 例如: interface IMyInterface { a: number;
我有一个仪表板,您可以在其中使用小部件选择多个数据集。我使用 Python 中的 Altair 库从这些数据集中制作经典折线图,x 轴和 y 轴均从零开始。显示的数据是标准化数据,这意味着根据定义,所
首先,我没有这样做的经验。但是就像任何好的程序的开始一样,我有需要解决的问题,所以我愿意学习。 你们中的许多人可能已经熟悉 pdftk,它是处理各种 pdf 相关任务的便捷实用程序。据我所知,这些功能
我是一名优秀的程序员,十分优秀!