gpt4 book ai didi

C - 用户输入矩阵

转载 作者:行者123 更新时间:2023-11-30 19:11:03 24 4
gpt4 key购买 nike

我正在尝试编写一个程序,其中用户首先输入矩阵的大小,然后输入每个单元格是否被占用(o)或未被占用(.)。我该如何编写它,以便用户输入一整行占用/未占用的输入,而不是使用嵌套 for 循环逐个单元格输入?

更新

下面是一个逐项输入项目的示例:

#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");
}
}

最佳答案

考虑以下示例:

#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

用户可以使用 Enter 键逐行输入以开始新行,或者只需键入 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
.....
.....

关于C - 用户输入矩阵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40735070/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com