我想製作一個程序,用戶首先輸入一個矩陣的大小,然後是每個單元格是否被佔用(o)或未佔用(。)。我如何編寫它,以便用戶輸入一整行的佔用/未佔用的輸入,而不是使用嵌套for循環的單元格?C - 用戶輸入到矩陣
UPDATE
有其中數據項通過一個輸入的一個示例:
#include <stdio.h>
#define MAX_SIZE 100
int main(void)
{
char matrix[MAX_SIZE][MAX_SIZE] = { 0 };
int rows = 0, cols = 0;
while (rows < 1 || rows > MAX_SIZE)
{
printf("What is the number of rows? ");
scanf("%d", &rows);
}
while (cols < 1 || cols > MAX_SIZE)
{
printf("What is the number of columns? ");
scanf("%d", &cols);
}
// fill the matrix
printf("Please, fill the matrix by entering o for occupied or . for unoccupied cell (E for exit)\n");
int r, c, ch;
int fulfill = 1;
for (r = 0; r < rows && fulfill; r++)
{
for (c = 0; c < cols && fulfill; c++)
{
// clean the input bufer
while ((ch = getchar()) != '\n' && ch != EOF);
// read data
printf("cell [%d, %d] : ", r + 1, c + 1); // or just r, c if you prefer 0..(N-1) indexing
while (matrix[r][c] != 'o' && matrix[r][c] != '.' && matrix[r][c] != 'E')
{
scanf("%c", &matrix[r][c]);
}
if (matrix[r][c] == 'E')
{
fulfill = 0;
}
}
}
// output
printf("\nResult is:\n");
for (r = 0; r < rows; r++)
{
for (c = 0; c < cols; c++)
{
printf("%c", matrix[r][c]);
}
printf("\n");
}
}
歡迎來到Stack Overflow!請說明迄今爲止的研究/調試工作。請先閱讀[問]頁面。 –