可变格式

发布于 2025-01-13 09:02:20 字数 1463 浏览 0 评论 0原文

我写了一个程序来计算平方有限差分矩阵,您可以在其中输入行数(等于列数)->这存储在变量矩阵中。该程序工作正常:

program fin_diff_matrix

  implicit none

  integer, dimension(:,:), allocatable :: A     
  integer :: matrix,i,j

  print *,'Enter elements:'
  read *, matrix

  allocate(A(matrix,matrix))

  A = 0
  A(1,1) = 2
  A(1,2) = -1
  A(matrix,matrix) = 2
  A(matrix,matrix-1) = -1

  do j=2,matrix-1
    A(j,j-1) = -1
    A(j,j) = 2
    A(j,j+1) = -1
  end do

  print *, 'Matrix A: '
  write(*,1) A

  1 format(6i10)

end program fin_diff_matrix

对于输出,我希望矩阵被格式化为输出,例如,如果用户输入 6 行,输出也应该如下所示:

         2        -1         0         0         0         0
        -1         2        -1         0         0         0
         0        -1         2        -1         0         0
         0         0        -1         2        -1         0
         0         0         0        -1         2        -1
         0         0         0         0        -1         2

格式的输出也应该是可变的,例如,如果用户输入 10,则输出也应格式化为 10 列。互联网上的研究给出了带有尖括号的格式语句的以下解决方案:

  1 format(<matrix>i<10)

如果我在 Linux 中使用 gfortran 进行编译,我总是在终端中收到以下错误:

       fin_diff_matrix.f95:37.12:

     1 format(<matrix>i10)
            1
   Error: Unexpected element '<' in format string at (1)
   fin_diff_matrix.f95:35.11:

     write(*,1) A
           1
   Error: FORMAT label 1 at (1) not defined

这不起作用,我的错误是什么?

I wrote a program to calculate a square finite difference matrix, where you can enter the number of rows (equals the number of columns) -> this is stored in the variable matrix. The program works fine:

program fin_diff_matrix

  implicit none

  integer, dimension(:,:), allocatable :: A     
  integer :: matrix,i,j

  print *,'Enter elements:'
  read *, matrix

  allocate(A(matrix,matrix))

  A = 0
  A(1,1) = 2
  A(1,2) = -1
  A(matrix,matrix) = 2
  A(matrix,matrix-1) = -1

  do j=2,matrix-1
    A(j,j-1) = -1
    A(j,j) = 2
    A(j,j+1) = -1
  end do

  print *, 'Matrix A: '
  write(*,1) A

  1 format(6i10)

end program fin_diff_matrix

For the output I want that matrix is formatted for the output, e.g. if the user enters 6 rows the output should also look like:

         2        -1         0         0         0         0
        -1         2        -1         0         0         0
         0        -1         2        -1         0         0
         0         0        -1         2        -1         0
         0         0         0        -1         2        -1
         0         0         0         0        -1         2

The output of the format should also be variable, e.g. if the user enters 10, the output should also be formatted in 10 columns. Research on the Internet gave the following solution for the format statement with angle brackets:

  1 format(<matrix>i<10)

If I compile with gfortran in Linux I always get the following error in the terminal:

       fin_diff_matrix.f95:37.12:

     1 format(<matrix>i10)
            1
   Error: Unexpected element '<' in format string at (1)
   fin_diff_matrix.f95:35.11:

     write(*,1) A
           1
   Error: FORMAT label 1 at (1) not defined

What doesn't that work and what is my mistake?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪菊怪哟 2025-01-20 09:02:20

您尝试使用的语法是非标准的,它仅在某些编译器中有效,我不鼓励使用它。

另外,请永远忘记 FORMAT() 语句,它们已经过时了。

当您自己从多个部分构建矩阵时,您可以在格式字符串中获取自己的数字

character(80) :: form
form = '(          (i10,1x))'
write(form(2:11),'(i10)') matrix

write(*,form) A

您还可以在每行循环中编写矩阵,然后您可以在中使用任意大的计数或 * Fortran 2008。

do i = 1, matrix
  write(*,'(999(i10,1x))') A(:,i)
end do

do i = 1, matrix
  write(*,'(*(i10,1x))') A
end do

只需检查我是否无意中转置了矩阵。

The syntax you are trying to use is non-standard, it works only in some compilers and I discourage using it.

Also, forget the FORMAT() statements for good, they are obsolete.

You can get your own number inside the format string when you construct it yourself from several parts

character(80) :: form
form = '(          (i10,1x))'
write(form(2:11),'(i10)') matrix

write(*,form) A

You can also write your matrix in a loop per row and then you can use an arbitrarily large count number or a * in Fortran 2008.

do i = 1, matrix
  write(*,'(999(i10,1x))') A(:,i)
end do

do i = 1, matrix
  write(*,'(*(i10,1x))') A
end do

Just check if I did not transpose the matrix inadvertently.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文