我如何分成功能?

时间:2019-11-30 14:55:59

标签: c

我有大量的代码可以进行矩阵运算,而且我不知道如何通过制作一些函数来使代码更优雅。这是代码,感谢您的帮助。 (这不是完整的代码):

int main(int argc, char *argv[]) {
  if (!(scanf("%d %d", &row1, &column1))) {
    fprintf(stderr, "Error\n");
    return 100;
  }
  matrixA = (int **) malloc(row1 * sizeof(int *));
  for (int i = 0; i < row1; i++) {
    matrixA[i] = (int *) calloc(column1, sizeof(int));
  }
  for (int i = 0; i < row1; i++) {
    for (int j = 0; j < column1; j++) {
      if (!(scanf("%d ", &matrixA[i][j]))) {
        fprintf(stderr, "Error\n");
        return 100;
      }
    }
  }
  while (scanf("%c", &operation) != EOF) { // operation ('+', '-')
    if (!(scanf("%d %d", &row2, &column2))) {
      fprintf(stderr, "Error\n");
      return 100;
    }
    matrixB = (int **) malloc(row2 * sizeof(int *));
    for (int i = 0; i < row2; i++) {
      matrixB[i] = (int *) calloc(column2, sizeof(int));
    }
    for (int i = 0; i < row2; i++) {
      for (int j = 0; j < column2; j++) {
        if (!(scanf("%d ", &matrixB[i][j]))) {
          fprintf(stderr, "Error\n");
          return 100;
        }
      }
    }
//operations with matrix
  if (operation == '+')
  {
    if (row1 != row2 || column1 != column2)
    {
      fprintf(stderr, "Error\n");
      return 100;
    }
    for (int i = 0; i < row1; i++)
    {
      for (int j = 0; j < column1; j++)
      {
        matrixA[i][j] += matrixB[i][j];
      }
    }
  }
}

正如我所说的,我不知道如何在函数中实现它) 喜欢:

int sum(){
}

0 个答案:

没有答案