C++ output * table
C++ output * table
我正在尝试在C++中创建一个表格,要求您提供边的长度并输出一个星形表。
有人可以指出我应该做什么才能让表格开始正确显示的正确方向吗?
输入:
Enter length of side: 5
想要的输出:
*****
* *
* *
* *
* *
*****
到目前为止,我输出 * 减去 1 的第一行,然后显示您输入的数字。
#include "main.h"
using namespace std;
int main () {
int sideLength;
cout << "Enter lengh of side: ";
cin >> sideLength;
cout.fill('*');
cout.width(sideLength);
cout << sideLength << endl;
return 0;
}
感谢高级,只是学习C ++
我还不太可爱。也许我可以想出更好的东西...
#include <iostream>
#include <algorithm>
#include <iterator>
int main()
{
int size;
if (std::cin >> size) {
auto to(std::ostreambuf_iterator<char>(std::cout));
auto line([=](decltype(to) to){ return std::fill_n(to, size, '*'); });
auto box([=](decltype(to) to){
*to++ = '*'; std::fill_n(to, size - 2, ' '); *to++ = '*'; return to;
});
*(to = line(to))++ = 'n';
for (int i(1); ++i < size; ) {
*(to = box(to))++ = 'n';
}
*(to = line(to))++ = 'n';
}
}
你可以这样做:
#include <iostream>
using namespace std;
int main () {
int sideLength;
cout << "Enter lengh of side: ";
cin >> sideLength;
for (int i = 0; i < sideLength; i++)
cout<<"*";
cout<<"n";
for (int i = 0; i < sideLength-2; i++)
{
cout<<"*";
for (int j = 0; j < sideLength-2; j++)
cout<<" ";
cout<<"*"<<"n";
}
for (int i = 0; i < sideLength; i++)
cout<<"*";
cout<<"n";
return 0;
}
您还应该添加一个循环来打印侧面和底部。
建议:
for (int i=0; i <= sideLength; i++)
{
cout<<endl <<"*";
for (int j=0; j < sideLength;j++)
{
cout<<" ";
cout<<"*";
}
cout.fill('*');
在 cout.fill('*') 行之后添加这些行。
也许它会起作用。
相关文章:
- ( [ ]( ){ output;}); 是什么意思?
- Lua table in C++ dll
- diff: output.txt: 没有这样的文件或目录
- 从生成文件编译错误:"Unable to open output file" ..."No such file or directory"
- 'Cannot output file filename.exe Permission denied' C++ 代码::块中的错误
- R data.table 和 STRING_ELT() 错误:如何修复"字符向量"与"字符"错误?
- 如何修复"(Song Title) is not recognized in output screen"
- C++ library output
- 我需要用c ++ 98重写c ++ 11代码,c ++ 11正在使用lambda函数[&](const Output & o
- 调用 QSqlTableModel setTable 方法时出现"Unable to find table"错误
- gradle see output for model.components.binaries.all
- 我在下面的代码上收到错误"Output Limit Exceeded"
- Output UTF-8 (u8) std::string
- 在 Eclipse.It 中运行C++代码时出错,'Your program cannot output..'
- JNI 在使用 NewGlobalRef 时"local reference table overflow"
- LuaJIT 和 C++ - 调用 Table.Method() 在 loadstring/pcall 中不起作用
- 为什么我会得到"You must feed a value for placeholder tensor 'output' with dtype int64"?
- Metis - Block output
- 如何在C++中调试"cannot open output file allocating memory.exe: Permission denied"?
- C++ output * table