1

我想知道有人知道如何将字符串转换为二维数组吗?这是我的尝试:

string w;
char s[9][9];
int p=0;
getline(cin, w);
while(p != w.size())
{
  for (int k = 0; k < 9; k++)
  {
    for(int j = 0; j < 9; j++)
    {
      s[k][j] = w[p];
      p++;
    }
  }
}
  cout << "nums are: " << endl;
  for(int k = 0; k < 9; k++)
  {
    for(int j = 0; j <9; j++)
    {
      cout << s[k][j];
    }
  }

但是数字没有正确打印出来。我希望 s[k][j] 打印出 w 中的所有内容,但它只是打印出乱码。我还注意到,如果我执行 string[81],那么我会收到一大堆错误。有人可以帮我吗?谢谢。

4

2 回答 2

1

试试这个:

const int NUM_ROWS = 9;
const int NUM_COLS = 9;

string w;
char s[NUM_ROWS][NUM_COLS];

getline(cin, w);

if (w.size() != (NUM_ROWS * NUM_COLS))
{
    cerr << "Error! Size is " << w.size() << " rather than " << (NUM_ROWS * NUM_COLS) << endl;
    exit(1);
}

for (int count = 0; count < w.size(); count++)
{
    if (!isdigit(w[count]) && w[count] != '.')
    {
        cerr << "The character at " << count << " is not a number!" << endl;
    }
}

for (int row = 0; row < NUM_ROWS; row++)
{
    for(int col = 0; col < NUM_COLS; col++)
    {
        s[row][col] = w[col + (row * NUM_COLS)];
    }
}

cout << "Nums are: " << endl;

for(int row = 0; row < NUM_ROWS; row++)
{
    for(int col = 0; col < NUM_COLS; col++)
    {
        cout << s[row][col] << " ";
    }

    cout << endl;
}

根据我们的聊天记录,您可能想要:

const int NUM_ROWS = 9;
const int NUM_COLS = 9;

string w;
char s[NUM_ROWS][NUM_COLS];

while (!cin.eof())
{
    bool bad_input = false;

    getline(cin, w);

    if (w.size() != (NUM_ROWS * NUM_COLS))
    {
        cerr << "Error! Size is " << w.size() << " rather than " << (NUM_ROWS * NUM_COLS) << endl;
        continue;
    }

    for (int count = 0; count < w.size(); count++)
    {
        if (!isdigit(w[count]) && w[count] != '.')
        {
            cerr << "The character at " << count << " is not a number!" << endl;
            bad_input = true;
            break;
        }
    }

    if (bad_input)
        continue;

    for (int row = 0; row < NUM_ROWS; row++)
    {
        for(int col = 0; col < NUM_COLS; col++)
        {
            s[row][col] = w[col + (row * NUM_COLS)];
        }
    }

    cout << "Nums are: " << endl;

    for(int row = 0; row < NUM_ROWS; row++)
    {
        for(int col = 0; col < NUM_COLS; col++)
        {
            cout << s[row][col] << " ";
        }

        cout << endl;
    }
}
于 2012-10-20T21:27:47.240 回答
0

您没有很好地描述您正在尝试做的事情,您还没有描述您遇到的问题,因此以下内容只是基于猜测。

所以看起来你想要做的是采用如下字符串:

敏捷的棕色狐狸跳过了懒惰的狗。

并将其放入二维数组中,例如:

  0 1 2 3 4 5 6 7 8
0 T h e   q u i c k
1   b r o w n   f o
2 x   j u m p e d  
3 o v e r   t h e  
4 l a z y   d o g s
5 . x x x x x x x x
6 x x x x x x x x x
7 x x x x x x x x x
8 x x x x x x x x x

您的代码有一个问题是,当您将值复制ws其中时,您并不能确保索引p实际上在范围内。您似乎已尝试在以下行中处理此问题while(p != w.size());但这是一个外部循环,不能防止p超出范围并在内部循环中使用。相反,您必须p++; if (p==w.size()) break;在最里面的循环中放置类似的东西 increment p。或者更好的是,您应该遍历字符串而不是数组。类似于以下伪代码的内容将替换您的整个while(p){for(k){for(j){}}}循环集。:

for(size_t i=0; i<w.size(); ++i) {
    int k = compute target row from i
    int j = compute target column from i
    s[k][j] = w[i]
}

此外,这里有一些代码可以在您调试时更好地可视化数组。

#include <iostream>

int main() {
    char s[9][9] = {"The","quick","brown","fox","jumped","over","the","lazy","dogs."};

    // your code to get input and copy it into the array goes here
    //
    // for(size_t i=0; i<w.size(); ++i) {
    //     int k = compute target row from i
    //     int j = compute target column from i
    //     s[k][j] = w[i]
    // }

    std::cout << "  0 1 2 3 4 5 6 7 8\n";
    for (int i=0; i<9; ++i) {
        std::cout << i;
        for (int j=0; j<9; ++j)
            std::cout << ' ' << s[i][j];
        std::cout << '\n';
    }
}

如果您在不做任何更改的情况下运行此程序,则输出应如下所示:

  0 1 2 3 4 5 6 7 8
0 T h e      
1 q u i c k    
2 b r o w n    
3 f o x      
4 j u m p e d   
5 o v e r     
6 t h e      
7 l a z y     
8 d o g s .    
于 2012-10-20T21:33:43.387 回答