【问题标题】:C Reading Matrix into Dynamic ArrayC将矩阵读入动态数组
【发布时间】:2014-04-07 15:12:45
【问题描述】:

我正在尝试编写用于读取矩阵读取器的代码,该读取器将从重定向文件中读取矩阵(因此 FILE 函数将不会在我的程序中使用),将其存储在动态创建的数组中,然后打印到控制台.

重要提示 数组是动态的(意味着通过读取整个文件并计算行数和列数来获得维度。

我已尝试编写代码 2 种不同的方式来执行此操作,但都导致错误的输出:

版本 A:

while(ch != EOF) {
  ch = fgetc(stdin);

  if(ch == ' ') {
    fields++;
  }
  if(ch == '\n') {
    rows++;
  }

}

B 版:

do {
  c=getchar();

  if(c == ' '){
    fields++;
  }

} while (c != EOF); 

问题

  1. while(ch != EOF)while(c=getchar() != EOF) 是否意味着它没有到达 LINE 结尾或 FILE 结尾?

我对上面显示的版本 B 运气不佳。用于测试文件时:

10 20 30 40
50 60 70 80
90 10 20 30
40 50 60 70

我得到输出:

50 60 70 80
90 10 20 30
40 50 60 70
70 70 70 70

我认为我在这里遇到的问题是,每当我读取我的文件时,一旦它遇到 EOF,它就会跳出循环,然后它位于文件的第 2 行,因此为什么输出从第 2 行开始行,然后将最后一个数字复制 X 次以填充矩阵的其余部分。

用我目前的方法可以实现我的目标吗? 这是我所有的代码,谢谢。

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

int main() {
  int rows=0;
  int fields=1;
  int i=0;
  int j=0;
  int c=0;
  char ch = '\0';

/*  while(ch != EOF) {
    ch = fgetc(stdin);
    if(ch == ' ') {
      fields++;
    }
    if(ch == '\n') {
      rows++;
    }
  } */

   do {
    c=getchar();
    if(c == ' '){
      fields++;
    }

  } while (c != 10);


  int **array;
  array = malloc(fields * sizeof(int *));

  if(array == NULL) {
    printf("Out of memory\n");
    exit(1);
  }

  for(i = 0; i < fields; i++) {
    array[i] = malloc(fields * sizeof(int));
    if(array[i] == NULL) {
      printf("Out of memory\n");
      exit(1);
    }

  }

  for(i = 0; i < fields; i++) {
    for(j = 0; j < 4; j++) {
      int k;
      scanf("%d", &k);
      array[i][j] = k;
      printf("%d ", k);
    }
    printf("\n");
  }
} 

【问题讨论】:

  • 其实while(c=getchar() != EOF)应该是while((c=getchar()) != EOF)加括号。首先分配然后比较,您将比较结果分配给 C 然后比较返回 0 您的循环中断
  • btw 再次检查您的问题,您的 while 条件与您显示的代码片段不对应。
  • 是的,我最近将while (c != EOF) 更改为while(c != 10) 但是我相信这些在技术上是在做同样的事情。对?此外,添加一个括号会导致我的输出全为 0,并且它输出一个 4x13 矩阵,所以是的,知道现在发生了什么吗?
  • No first EOF != 10, second my second comment表示您没有使用“while(c=getchar() != EOF)”
  • 使用代码:while((c=getchar()) != EOF),如果c == ' ' 则使用fields++,如果c == '\n' 使用rows++,则会产生一个只有0 的巨大矩阵。

标签: c arrays dynamic matrix malloc


【解决方案1】:
#include <stdio.h>
#include <stdlib.h>

int main(){
    int rows = 0;
    int cols = 0;
    int fields = 0;
    int i, j, c, n, status;;
    int **array;
    char ch;

    array = malloc((rows+1) * sizeof(int*));//check omit
    array[rows] = NULL;

    while(0<(status = scanf("%d%c", &n, &ch))){
        if(status>1){
            if(cols == fields){
                array[rows] = realloc(array[rows], (fields=cols+1)*sizeof(int));
            }
            array[rows][cols++] = n;
            if (ch == '\n'){
                array = realloc(array, (++rows+1) * sizeof(int*));
                array[rows] = malloc(fields*sizeof(int));
                cols = 0;
            }
        } else {
            array[rows][cols++] = n;
            break;
        }
    }
    if(cols == 0){
        free(array[rows--]);
    }
    for(i=0;i<=rows;++i){
        for(j=0;j<fields;++j){
            printf("%d ", array[i][j]);
        }
        printf("\n");
        free(array[i]);
    }
    free(array);
    return 0;
}

【讨论】:

    猜你喜欢
    • 2016-08-13
    • 2019-11-28
    • 1970-01-01
    • 1970-01-01
    • 2011-12-15
    • 1970-01-01
    • 2022-01-05
    • 2023-03-03
    相关资源
    最近更新 更多