C++ 如何在数组表中输出水平字母
我正在做一个6x6的骰子游戏,我需要能够把行放在桌子的y轴上,但我不知道如何用我的代码做到这一点 创建一个6x6 2D表格,该表格使用值1到6保存行和列的总和 我一直在阅读第九版的C++从控制结构开始的Tony Gaddis的对象,我只是找不到我在寻找什么。C++ 如何在数组表中输出水平字母,c++,C++,我正在做一个6x6的骰子游戏,我需要能够把行放在桌子的y轴上,但我不知道如何用我的代码做到这一点 创建一个6x6 2D表格,该表格使用值1到6保存行和列的总和 我一直在阅读第九版的C++从控制结构开始的Tony Gaddis的对象,我只是找不到我在寻找什么。 //System Libraries #include <iostream> //Input/Output Library #include <iomanip> //Format Library using n
//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 Table\n";
cout<<" C o l u m n s\n";
cout<<" | 1 2 3 4 5 6\n";
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 Micro-tweak打印行值和任何间距。endl是一个换行符和流刷新。流式冲洗可能非常昂贵,因此您只想在必要时冲洗。只需使用换行符“\n”,而不是endl。
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