如何在数组表中输出水平字母
How to output horizontal letters in an array table
我正在制作一个 6x6 骰子游戏,我需要能够将 ROWS 放在桌子的 y 轴上,但我不明白如何使用我的代码做到这一点。
创建一个 6x6 2D 表,该表使用值 1 到 6 保存行和列的总和。
我一直在阅读托尼·加迪斯(Tony Gaddis(的第九版"从C++从控制结构到对象",我找不到任何关于我正在寻找的东西。
//System Libraries
#include <iostream> //Input/Output Library
#include <iomanip> //Format Library
using namespace std;
//User Libraries
//Global Constants, no Global Variables are allowed
//Math/Physics/Conversions/Higher Dimensions - i.e. PI, e, etc...
const int COLS=7;
//Function Prototypes
void fillTbl(int [][COLS],int);
void prntTbl(const int [][COLS],int);
//Execution Begins Here!
int main(int argc, char** argv) {
//Declare Variables
const int ROWS=6;
int tablSum[ROWS][COLS] ={{1,2,3,4,5,6,7},
{2,3,4,5,6,7,8},
{3,4,5,6,7,8,9},
{4,5,6,7,8,9,10},
{5,6,7,8,9,10,11},
{6,7,8,9,10,11,12}};
//Initialize or input i.e. set variable values
fillTbl(tablSum,ROWS);
cout<<"Think of this as the Sum of Dice Tablen";
cout<<" C o l u m n sn";
cout<<" | 1 2 3 4 5 6n";
cout<<"----------------------------------n";
//Display the outputs
prntTbl(tablSum,ROWS);
//Exit stage right or left!
return 0;
}
void fillTbl(int tablSum [][COLS],int ROWS)
{
cout<<"";
}
void prntTbl(const int tablSum [][COLS],int ROWS)
{
for(int x = 0; x < ROWS; x++)
{
for(int y = 0; y < COLS; y++)
{
cout<<setw(4)<<tablSum[x][y];
}
cout<<endl;
}
}
Your Output
Think·of·this·as·the·Sum·of·Dice·Table↵
···········C·o·l·u·m·n·s↵
·····|···1···2···3···4···5···6↵
----------------------------------↵
···1···2···3···4···5···6···7↵
···2···3···4···5···6···7···8↵
···3···4···5···6···7···8···9↵
···4···5···6···7···8···9··10↵
···5···6···7···8···9··10··11↵
···6···7···8···9··10··11··12↵
Expected Output
Think·of·this·as·the·Sum·of·Dice·Table↵
···········C·o·l·u·m·n·s↵
·····|···1···2···3···4···5···6↵
----------------------------------↵
···1·|···2···3···4···5···6···7↵
R··2·|···3···4···5···6···7···8↵
O··3·|···4···5···6···7···8···9↵
W··4·|···5···6···7···8···9··10↵
S··5·|···6···7···8···9··10··11↵
···6·|···7···8···9··10··11··12↵
我们可以更改您的 prntTbl 函数,使其具有包含行字符串的字符串文字: char* rows = " ROWS ";
然后在每次内部循环迭代之前,我们可以使用第一个循环索引打印字符串索引处的字符,以及行值和任何间距: cout << rows[x] << " " << x + 1 << " |";
我们的结束 prntTbl 方法如下所示:
void prntTbl(const int tablSum [][COLS],int ROWS)
{
char* rows = " ROWS ";
for(int x = 0; x < ROWS; x++)
{
cout << rows[x] << " " << x + 1 << " |";
for(int y = 0; y < COLS; y++)
{
cout<<setw(4)<<tablSum[x][y];
}
cout<<endl;
}
和输出:
C o l u m n s
| 1 2 3 4 5 6
----------------------------------
1 | 1 2 3 4 5 6 7
R 2 | 2 3 4 5 6 7 8
O 3 | 3 4 5 6 7 8 9
W 4 | 4 5 6 7 8 9 10
S 5 | 5 6 7 8 9 10 11
6 | 6 7 8 9 10 11 12
相关文章:
- 递归函数计算序列中的平方和(并输出过程)
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 请解释"函数1(p1,p2,p3);"的输出
- C++:将控制台输出存储在宏中更好吗
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 为什么我的代码在输出中增加了93天
- 如何从void函数输出字符串
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- AES加密到解密未正确输出
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 为什么我不能在不创建字符串变量的情况下使用函数的字符串输出
- C++格式化输出问题
- QTableView:endMoveRows在模型中重置水平页眉大小
- 将值从二维数组输出到文本文件
- 集合上的输出迭代器:assign和increment迭代器
- 如何水平排列程序的打印输出循环而不重复
- 如何在数组表中输出水平字母
- 如何在 c++ 中水平输出数组