如何用用户输入填充2D阵列?

时间:2018-11-12 04:24:06

标签: c++ arrays

我是Stack Overflow的新手,我想知道我是否可以获得有关C ++代码的帮助。

我想用用户输入来填充2D数组,例如:

  • 请输入16个字符:(abcdabcdabcdabcd)

输出一个4x4的网格,在这种情况下,该网格充满了用户的输入(abcdabcdabcdabcd)。

a b c d
a b c d 
a b c d
a b c d

这是我到目前为止的进展:

#include <iostream>
#include <stdlib.h>

using namespace std;

//Constants
const int SM_GRID = 3;
const int LG_GRID = 4;
const char FILL_1 = 'X';
const char FILL_2 = 'O';
const int FORWARD = 1;
const int REVERSE = 2;
const int MAXLEN = 128;

//displays an overview of the program
void displayOverview();

//fills a small array with x's and o's
void fillSmallArray(char grid[][SM_GRID]);

//prompts the user for characters, and fills a 4x4 array with them
//either fills it from top to bottom, left to right (if direction is 1)
//or bottom to top, right to left (if direction is 2)
void fillLgArray(char grid[][LG_GRID], int direction);

//outputs the small array
void outputSmallArray(char grid[][SM_GRID]);

//outputs the large array
void outputLgArray(char grid[][LG_GRID]);

//prompts for a command, and calls the appropriate function based on it
//returns true if command was valid
bool doCommand(char grid[][LG_GRID]);


int main(){

  //Overview of the program
  displayOverview();

  //set up arrays
  char myGrid[SM_GRID][SM_GRID];
  fillSmallArray (myGrid);
  outputSmallArray(myGrid);

  //declare a large array
  //declare variable: Play or Not?
  //As long as user wants to play
  //call doCommand
  char myGrid1[LG_GRID][LG_GRID];
  doCommand (myGrid1);

    //fill the small array and output it

    //fill the large array and output it, as many times as the user wants
    //1 for forward, 2 for reverse, any other character ends the game

return 0;
}


//displays an overview of the program
void displayOverview(){
  cout << "Welcome to my grid-o-matic game !! :)\n"
  << "Here is your small grid: \n";

}


//fills a small array with x's and o's
void fillSmallArray(char grid[][SM_GRID]){
  //logic: if both row and col are even, or both odd, put in a X
  //otherwise put in a O
  //loop through the grid put in a X or O as above
  for (int row = 0; row < SM_GRID; row++){
    for(int col = 0; col < SM_GRID; col++){
      if((row %2) == (col%2)){
grid [row][col] = FILL_1;
      }else{
grid [row][col] = FILL_2;
      }//if else
    }//inner for
  }//outer for
}//function



//prompts the user for characters, and fills a 4x4 array with them
//either fills it from top to bottom, left to right (if direction is 1)
//or bottom to top, right to left (if direction is 2)

void fillLgArray(char grid[][LG_GRID], int direction){


    string userInput;

    cout << "Please enter 16 characters!\n";
    cin >> userInput;

    if(userInput.length() > 16){

        userInput = userInput.substr(0, 16);
    }

    cout << "You entered " << userInput << endl;

    for (int row = 0; row < LG_GRID; row++){
        for (int col = 0; col < LG_GRID; row++){
                grid [row][col] = userInput.at(col+row*LG_GRID);
        }
    }
}//Function


//outputs the small array
void outputSmallArray(char grid[][SM_GRID]){

  for (int row=0;row <SM_GRID;row++){
    for(int col=0;col <SM_GRID;col++){
      cout << grid[row][col]<<" ";
    }
    cout << endl;
  }
}


//outputs the large array
void outputLgArray(char grid[][LG_GRID]){

    for (int row=0;row <LG_GRID;row++){
    for(int col=0;col <LG_GRID;col++){
      cout << grid[row][col]<<" ";
    }
    cout << endl;
  }
}


//prompts for a command, and calls the appropriate function based on it
//returns true if command was valid
bool doCommand(char grid[][LG_GRID]){

  bool valid = true;

  char input [MAXLEN];
  int command;

  cout << "Please enter 1 for FORWARDS or 2 for reverse!\n";
  cin.getline(input,MAXLEN);

  command = atoi(input);
  switch (command){
  case FORWARD:
    cout << "Going Forwards !!!\n";
    fillLgArray(grid,command);

    outputLgArray(grid);
    break;

  case REVERSE:
    cout << "Going backwards !!\n";
    fillLgArray(grid, command);

    outputLgArray(grid);
    break;

  default:
    return false;
  }
 return valid;
}

3 个答案:

答案 0 :(得分:1)

在您的fillLgArray function中,您说过:You didn't know what to put用于索引到用户输入字符串中,有一个公式用于索引一维数组,以将其视为2D数组。此公式有两种变体,一种是Row-Colum major,另一种是Col-Row major。在您的特定情况下,如果用户从控制台输入以下内容:

abcdabcdabcdabcd

您可以指望std::stringstd::vector非常相似,只是std::string将其内容存储为char_type并具有用于处理字符串操作的特殊成员函数除了核心std::string以外,基本上是std::vector的专用版本。 stl中的这两个容器都是包装在分配给动态内存的连续内存块周围的包装器。这些容器环绕type [size]数组,并且它们动态增长。

因此知道了这一点,我们可以将来自控制台"abcdabcdabcdabcd"的文本视为一维字符数组,如下所示:

 0   1   2   3   4   5   6   7   8   9  10  11  12  13  14  15
[a] [b] [c] [d] [a] [b] [c] [d] [a] [b] [c] [d] [a] [b] [c] [d]

因此,当您遍历double for循环以索引到2D数组时,您必须在每次迭代中认真地完成循环。

在C和C ++等C语言中,表示一维2D数组的平面一维数组的索引是以行为主的,因此公式如下:

for ( row = 0; row < 4; row++ ) {
    for ( col = 0; col < 4; col ++ ) {
        2darray[row][col] = 1darray[(row * row_size or width) + col];
    }
}

这已经在这里被询问和回答了! Map a 2D array onto a 1D

这是一个代码示例,它是一个最小的,完整的可验证示例:

#include <string>
#include <iostream>
#include <exception>

#define LG_GRID = 4;

void fillArray( char grid[][LG_GRID] ) {
    std::string userInput;
    std::cout << "Please enter 16 characters\n";
    std::cin >> userInput;

    if( userInput.length() < 16 )
        throw std::runtime_error( "Not enough characters" );
    if( userInput.length() > 16 )
        userInput = userInput.substr( 0, 16 );

    std::cout << "You entered " << userInput << '\n';

    for( int row = 0; row < LG_GRID; row++ ) {
        for( int col = 0; col < LG_GRID; col++ ) {
            // grid[row][col] = userInput[row * LG_GRID + col];
            grid[row][col] = userInput.at( row * LG_GRID + col );
            // both lines above will work however `string.at()` will throw an out of range exception.
        }
    }
}

void displayArray( char grid[][LG_GRID] ) {
    for( int row = 0; row < LG_GRID; row++ ) {
        for( int col = 0; col < LG_GRID; col++ ) {
            std::cout << grid[row][col] << " ";
        }
        std::cout << '\n';
    }
}

int main() {
    try {
        char myGrid[LG_GRID][LG_GRID];
        fillArray( myGrid );
        displayArray( myGrid );    

    } catch( const std::runtime_error& e ) {
        std::cerr << e.what() << '\n';
        return EXIT_FAILURE;
    }

    return EXIT_SUCCESS;
}

并使用您描述的输入:"abcdabcdabcdabcd"在该程序的控制台上的输出为:

Please enter 16 characters
abcdabcdabcdabcd
You entered abcdabcdabcabcd
a b c d
a b c d
a b c d
a b c d

答案 1 :(得分:0)

在您的“ IDK应该放在这里”这一行中,您可能应该使用charAt。像这样:

grid [row][col] = userInput.at(col+row*4);

您想要在用户输入处输入字符,点0、1、2、3、4、5 ...等

答案 2 :(得分:-1)

send()