2016-11-22 45 views
-1

我想製作一個程序,用戶首先輸入一個矩陣的大小,然後是每個單元格是否被佔用(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"); 
    } 
} 
+1

歡迎來到Stack Overflow!請說明迄今爲止的研究/調試工作。請先閱讀[問]頁面。 –

回答

1

考慮下面的例子:

#include <stdio.h> 
#include <stdlib.h> 

int main(void) 
{ 
    char** matrix; // now it is a pointer and memory will be allocated after getting values of rows and columns 
    int rows = 0, cols = 0; 
    while (rows < 1) 
    { 
     printf("What is the number of rows (should be >= 1)? "); 
     scanf("%d", &rows); 
    } 
    while (cols < 1) 
    { 
     printf("What is the number of columns (should be >= 1)? "); 
     scanf("%d", &cols); 
    } 
    // part 1 for memory allocation 
    matrix = (char**)malloc(sizeof(char*) * rows); 
    // fill the matrix 
    printf("Please, fill the matrix by entering o for occupied or . for unoccupied cell (other chars will be ignored)\n"); 
    int r, c, ch; 
    for (r = 0; r < rows; r++) 
    { 
     // part 2 for memory allocation - memory for each row 
     matrix[r] = (char*)malloc(sizeof(char) * cols); 
     for (c = 0; c < cols; c++) 
     { 
      // read while apropriate character was found 
      while ((ch = getchar()) != 'o' && ch != '.'); 
      // save character to matrix 
      matrix[r][c] = ch; 
     } 
    } 
    // output 
    printf("\nResult is:\n"); 
    for (r = 0; r < rows; r++) 
    { 
     for (c = 0; c < cols; c++) 
     { 
      printf("%c", matrix[r][c]); 
     } 
     printf("\n"); 
    } 
    // free the memory 
    for (r = 0; r < rows; r++) 
    { 
     free(matrix[r]); 
    } 
    free(matrix); 
} 

在這裏數據被存儲在堆(動態內存,由malloc分配,或其他類似的功能),儘管使用相同的嵌套循環進入我不同的。你看,它是如何工作的:

What is the number of rows (should be >= 1)? 3 
What is the number of columns (should be >= 1)? 3 
Please, fill the matrix by entering o for occupied or . for unoccupied cell (other chars will be ignored) 
o.o 
.o. 
o.o 

Result is: 
o.o 
.o. 
o.o 

用戶可以使用回車鍵被排進行啓動新行,或只是一個爲所有單元格中鍵入o.一個(填寫將在任何情況下,逐行) 。不必要的元素以及不合適的字符(不同於o.)將被忽略。例如:

What is the number of rows (should be >= 1)? 4 
What is the number of columns (should be >= 1)? 5 
Please, fill the matrix by entering o for occupied or . for unoccupied cell (other chars will be ignored) 
ooZooCooXooOoo 
...... 
........XXX 

Result is: 
ooooo 
ooooo 
..... 
..... 
+0

嗨,靜態分配也是一個可行的選擇?此外,我的代碼假設用戶將輸入正確的輸入(數量和正確的字符),那麼如何改變這個例子?我是C的初學者,並想知道代碼是否可以簡化。 – Zhasan

+0

我已經添加了一個問題的例子,其中數組是靜態的。你可以使用相同的方法。如果您有任何問題,請在獲得代碼後用新帖發問 – VolAnd

+0

即使用戶總是正確,也應該跳過某些字符(例如'\ n')。所以要小心代碼最小化。如果這個答案是有用的,接受它 – VolAnd