如何在数组表中输出水平字母

How to output horizontal letters in an array table

本文关键字:输出 水平 数组      更新时间:2023-10-16

我正在制作一个 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