根据用户输入和C中的模式填充Matrix

时间:2013-04-19 03:24:26

标签: c string matrix populate

我有一个开发计划,但我在一个部分遇到了一些困难。 我必须阅读一些将要进行的测试(t)。之后我必须读取一个数字(n)的列和行来制作一个方形矩阵²(nxn)。在矩阵的实例之后,程序必须从用户的输入填充它。用户将键入.bw。基于这种模式,我必须填充矩阵。用户将键入的每一行必须包含n个字符(.bw),并且他将键入n次。这将填充矩阵(n行为n行)。你们能帮我一把吗?

这是我的代码:

int main(void)
{
    //vars
    int n = 0, t = 1, x = -1, y = -1, teste = 1;
    int i,j;
    //Start
    scanf(" %d %*c",&t);//scans t
    while (t-- > 0) {
        scanf(" %d", &n);//scans n
        if(n>0 && n < 100){
            int table[n][n];//the matrix n x n
            for (i = 0; (i < n);++i) {//iterator to lines
                char l[n];
                scanf ("%s", l); //scans a line
                for (j = 0; j < n; ++j) {//iterator to colums
        //these ifs are to identfy the input
                   if (l[j] == 'b'){
                        table[i][j]=1;
                    }else if(l[j] == 'w'){
                        table[i][j]=2;
                        x=j;y=i;
                    }else{
                        table[i][j]=0;
                    }
                }
        }
    }
    return 0;
}

我在Java中做了完全相同的事情并且它有效。我在哪里失败?

1 个答案:

答案 0 :(得分:1)

您的变量l不允许足够的空间在字符串的末尾存储空值。因此,你会溢出到其他变量中,这可能会影响各种各样的事情。

你应该把这条线读成一个更大的字符串,并确保它是正确的长度。您还应该对每次读取操作进行错误检查;您还应该在输入中报告无效字符。

此代码适用于我。注意它回显数据的方式,这样就可以看出出了什么问题。错误报告应该是标准错误;我一直很懒。

#include <stdio.h>
#include <string.h>

static void dump_board(FILE *fp, const char *tag, int n, int table[n][n])
{
    fprintf(fp, "%s: (%d x %d)\n", tag, n, n);
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (table[i][j] == 0)
                putc('=', fp);
            else if (table[i][j] == 1)
                putc('B', fp);
            else if (table[i][j] == 2)
                putc('W', fp);
            else
                putc('?', fp);
        }
        putc('\n', fp);
    }
}

int main(void)
{
    int n = 0, t = 1, x = -1, y = -1;

    if (scanf(" %d %*c", &t) != 1)
    {
        printf("Failed to read t\n");
        return 1;
    }
    printf("%d data sets\n", t);

    while (t-- > 0)
    {
        if (scanf(" %d", &n) != 1)
        {
            printf("Failed to read n\n");
            return 1;
        }

        printf("Size of data set: %d x %d\n", n, n);
        int c;
        while ((c = getchar()) != EOF && c != '\n')
            ;

        if (n > 0 && n < 100)
        {
            int table[n][n];
            for (int i = 0; i < n; i++)
            {
                char line[4096];
                if (fgets(line, sizeof(line), stdin) == 0)
                    break;
                int len = strlen(line);
                if (line[len-1] != '\n')
                {
                    printf("Format error: line too long (%d bytes)\n", len);
                    return 1;
                }
                line[--len] = '\0';
                if (len != n)
                {
                    printf("Format error: line <<%s>> is not length %d\n", line, n);
                    return 1;
                }
                for (int j = 0; j < n; ++j)
                {
                    if (line[j] == 'b')
                        table[i][j] = 1;
                    else if (line[j] == 'w')
                    {
                        table[i][j] = 2;
                        x = j;
                        y = i;
                    }
                    else if (line[j] == '.')
                        table[i][j] = 0;
                    else
                    {
                        printf("Format error: invalid character %c\n", line[j]);
                        return 1;
                    }
                }
            }
            dump_board(stdout, "Input", n, table);
            printf("Last white piece at (%d,%d)\n", x, y);
        }
    }
    return 0;
}

输入

2x
4
b..w
.bw.
.b.b
w.w.
8
b.w.b.w.
.w.b.w.b
bbwwbbww
b......w
ww....bb
bwb..wbw
bbbbwwww
........

输出

2 data sets
Size of data set: 4 x 4
Input: (4 x 4)
B==W
=BW=
=B=B
W=W=
Last white piece at (2,3)
Size of data set: 8 x 8
Input: (8 x 8)
B=W=B=W=
=W=B=W=B
BBWWBBWW
B======W
WW====BB
BWB==WBW
BBBBWWWW
========
Last white piece at (7,6)